javaxmlxml-parsingxom

Inserting new node in XOM to an XML document at a specified Index; using insertChild() method


I am fairly new to XOM. I want to insert a child(<stmt> </stmt>) into the node <mast_stmt></mast_stmt> at a specified index but what I get in the XML document at the correct position is &lt;stmt &gt; &lt;/stmt&gt; and this is what I am really after <stmt> </stmt>

Here is my code section that handles insertion:

 parentNode1.insertChild("<stmt></stmt>", index + 1);

Solution

  • You are supposed to create new Element instance and pass it as parameter of insertChild() :

    Element newElement = new Element("stmt");
    parentNode1.insertChild(newElement, index + 1);