aspose file tools*
The moose likes XML and Related Technologies and the fly likes write in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "write in XML " Watch "write in XML " New topic
Author

write in XML

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Hi ,

I am using the next code in order to write in my XML but it doesn't works and no error is thrown and it retrieve the values well when I debug it all seems be working but when I open my XML file(from which I read without problem) nothing is written there:




Any idea , please?

Many Thanks,
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1044
    
  10

You haven't added the elements you have created to the document!
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Hi,

thanks for reply.

What way could I do it , please?

Regards,
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1044
    
  10

http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Hi,



I have write like this:



But I get the error:



I am trying to write wchi already exists not create a new one.

Any idea?

Thanks
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1044
    
  10

The reference I gave shows you exactly how to do this. You need to append a root Element and then append your Elements to the root element.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Hi,

when I try to do that, I get the error:



Any idea?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

One of the rules of XML is that an XML document must have a single root element. The structure of the DOM is such that the "document" has a single child, which is that root element. So you can't add anything to the document because that would result in its having more than one root element.

You should first find the element which you want to add that random stuff to, and then add it there. Don't just add it to the document.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Im my XMLWriter I have the method writeData which contains this code


Any idea?

Thanks,
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1044
    
  10

Angus Ferguson wrote:
Any idea?


It is difficult to know how to respond to this. I gave you a reference that presented code that seems to provide the EXACT form of output that you need but for some reason you choose to ignore it. Your latest posting shows that you still do not understand the basics of XML so all I can do is refer you to an XML tutorial - http://www.w3schools.com/xml/ . Although the early pages of the tutorial seem trivial I suggest that you don't skip them.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 336
Hi,

thanks for reply.

Now I have the code



I have used this code but in my xml file which is something like this:



Now it is woking it is writing but I want add one tag to an existing xml. When I write all the data I had disappear, and I don't want that.

What way could I maintain my old tags too.?


In order to do that I am using code like this but the doc is null it seems is not instantiating the file



I get the error

java.lang.NullPointerException

Any idea?

Thanks
 
 
subject: write in XML