Newbie question about Java Updating and Inserting XML file

Y

ysongpsu

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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");

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
 
R

Rusty Wright

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Rusty said:
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 think they are XML serialization packages.

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

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top