I'm trying to insert a tag inside an XML object.
Tried the SO answer mentioned here in a different way, but couldnt achieve it.
This is what my request XML looks like.
<Root>
<Id>1234</Id>
<Type>2345</Type>
<Value>RootId</Value>
<IdNum>1234-2345</IdNum>
</Root>
And I had to add a tag to it.
<Root>
<Id>1234</Id>
<Type>2345</Type>
<Value>RootId</Value>
<IdNum>1234-2345</IdNum>
<RefId>1234-2345</RefId>
</Root>
My code looks like below:
...
Element rootEle = document.getElementsByTagName("Root").item(0);
NodeList idNode = rootEle.getElementsByTagName("IdNum");
Text refValue = document.createTextNode(rootEle.getElementsByTagName("IdNum").item(0).getTextContent());
Element refElement = document.createElement("RefId");
refElement.appendChild(refValue);
idNode.item(0).getParentNode().insertBefore(refElement,idNode.item(0));
Tried to print the IdNum like below and it is printing the expected result. But the additional tag alone is not getting appended.
rootEle.getElementsByTagName("IdNum").item(0).getTextContent()
Am I missing something?