aspose file tools
The moose likes XML and Related Technologies and the fly likes Problem with adding an XML node to an existent XML File Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "Problem with adding an XML node to an existent XML File" Watch "Problem with adding an XML node to an existent XML File" New topic
Author

Problem with adding an XML node to an existent XML File

Hugo Alberto Bedolla
Greenhorn

Joined: Nov 09, 2010
Posts: 17
I got a problem when trying to write a new node to an xml file, basically the new node isn't appended to the file. I tried to write to another file, but in both cases, only the old data appears... what am I doing wrong? :S


Here's my code:

Thanks in advance.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
Try appending the XmlDataElement created to the document.
Hugo Alberto Bedolla
Greenhorn

Joined: Nov 09, 2010
Posts: 17
John Jai wrote:Try appending the XmlDataElement created to the document.


Thanks for your reply... done, I got:

org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl.java:391)
at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:235)


Code is (look around line 35):
John Jai
Bartender

Joined: May 31, 2011
Posts: 1778
Then try where you have to append the XmlDataElement. If that should be under the root, then get the doc.getDocumentElement() and append the XmlDataElement to it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

See also this duplicate thread.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Rob Spoor wrote:See also this duplicate thread.
Which is in a far more appropriate location. Thank you for noticing, Rob. I am moving this thread.
Hugo Alberto Bedolla
Greenhorn

Joined: Nov 09, 2010
Posts: 17
John Jai wrote:Then try where you have to append the XmlDataElement. If that should be under the root, then get the doc.getDocumentElement() and append the XmlDataElement to it.


Hi John: Fine, that works; I was deducing when I call "Element XmlDataElement = doc.createElement("XmlData");" DOM knows both is a new node, and where I wanna put the new node (considering is the next new node inside the document) but it is not the case. Here's the new code calling the root node and appending a new XmlData node to it, that solution worked for me (Lines 34, 35):

Again, thanks so much for your help.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Problem with adding an XML node to an existent XML File
 
Similar Threads
Problem with adding an XML node to an existent XML File
unable to remove spaces in xml
problem in deleting the file
Help regarding how to check the element entered is equal or not in xml file
importing one xml in another