Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have an existing XML document with more number of nodes and I want to insert a new node, but at a certain position.

The document looks something like:

<a>...</a> <c>...</c> <e>...</e> </root>

... can be considered as xml tags a.../a, c.../c, e.../e. (formatting issue)

The new nodes should be inserted in alphabetical order in between the nodes, resulting in:

new node new node new node

How can I use XPath in TCL to find the existing node and insert new node before or after it.

I also want to preserve the order, since the existing tags in XML document are in alphabetical order.

At present I am using tdom package.

Does anyone have an idea on how to insert such a node?

I've formatted your question so that the content shows up correctly, but it appears to be talking about producing a document that isn't well-formed XML. I don't know the cause of this, but it would help tremendously if you edited your question to add in where you're looking to go. (Indentation by at least 4 spaces makes for a literal section.) Donal Fellows May 7, 2011 at 5:44 set insertPoint [$container selectNodes a] set toAdd [$doc createElement b] $toAdd appendChild [$doc createTextNode "234"] $container insertAfter $insertPoint $toAdd set insertPoint [$container selectNodes c] set toAdd [$doc createElement d] $toAdd appendChild [$doc createTextNode "456"] $container insertAfter $insertPoint $toAdd # Write back out set f [open $filename w] puts $f [$doc asXML -indent 4] close $f

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .