Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Newbie question about Java Updating and Inserting XML file

Thread view: 
ysongpsu@gmail.com - 17 Oct 2007 01:23 GMT
I searched the forum and see lots of examples, but I'm still confused.
I'm using DOM.

Say I've got an "a.xml" file, and have two methods, updateXML() that
updates an existing record, and insertXML() that inserts into this
file.
Anybody can give me a hint how to implement these two functions?
How to write back to the file? I've learned how to read it:

           InputStream aXML = getResourceAsStream("a.xml");
           DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           Document doc = db.parse(aXML);
           doc.getDocumentElement().normalize();
           NodeList nodeLst = doc.getElementsByTagName("record");

Thanks a lot.
Arne Vajhøj - 17 Oct 2007 02:43 GMT
> I searched the forum and see lots of examples, but I'm still confused.
> I'm using DOM.
[quoted text clipped - 12 lines]
>             doc.getDocumentElement().normalize();
>             NodeList nodeLst = doc.getElementsByTagName("record");

There are several options.

I like Xerces:

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

...

OutputFormat fmt = new OutputFormat();
fmt.setIndenting(true);
XMLSerializer ser = new XMLSerializer(new FileOutputStream(fnm), fmt);
ser.serialize(doc);

Arne
Rusty Wright - 17 Oct 2007 05:47 GMT
Here are some packages that I looked at for reading and writing XML
files.  Do a google search to find their home pages.

Apache XMLBeans
XStream
Apache Jakarta Commons Betwixt
Castor

>> I searched the forum and see lots of examples, but I'm still confused.
>> I'm using DOM.
[quoted text clipped - 28 lines]
>
> Arne
Arne Vajhøj - 18 Oct 2007 03:31 GMT
> Here are some packages that I looked at for reading and writing XML
> files.  Do a google search to find their home pages.
[quoted text clipped - 3 lines]
> Apache Jakarta Commons Betwixt
> Castor

I think they are XML serialization packages.

Not exactly the same as a simple writing of an XML Document.

Arne
Steve Sobol - 18 Oct 2007 06:38 GMT
> I think they are XML serialization packages.

Yes. XMLBeans and XStream are for sure; I've used them both (although I
prefer XStream).

Signature

Steve Sobol, Victorville, CA     PGP:0xE3AE35ED  www.SteveSobol.com

Wahoo! Indians beat New York to advance to the AL Championship Series!
http://www.latimes.com/sports/la-sp-al9oct09,1,5871580.story

Roedy Green - 17 Oct 2007 06:13 GMT
On Tue, 16 Oct 2007 17:23:23 -0700, "ysongpsu@gmail.com"
<ysongpsu@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>I searched the forum and see lots of examples, but I'm still confused.
>I'm using DOM.

There are a ton of things to try.  See
http://mindprod.com/jgloss/xml.html

However, XML is built into Java now, so I would use those tools unless
they are clearly just too awful for the task.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.