I'm new to working with XML in Java. I have two questions...
* Where's the FAQ for this forum? I searched the forum for "faq" first, I swear.
* I am overwhelmed by the number of options. I think I have come across a least a dozen different ways to write/update xml from java. Which one should I use? My needs are very simple: updating (that is writing to already existing) xml files of small size and rather simple structure.
The link to the FAQ seems to have gone missing at some point; I've restored it to the forum home page. It doesn't have a whole lot content yet, though.
It's largely a matter of personal preference. Using DOM has the advantage of not requiring any additional libraries (it's built into the JRE), although that's not often an important consideration.
Joined: Oct 13, 2008
Ulf Dittmer wrote:It's largely a matter of personal preference. Using DOM has the advantage of not requiring any additional libraries (it's built into the JRE), although that's not often an important consideration.
Actually, I always like to go for the built-in solutions first, and abandon them only when my needs exceed their capabilities. So I will start with DOM.
Another question: Does writing XML with DOM mean I should be using the LSXxx classes? Like LSSerializer and LSOutput?
Joined: Mar 22, 2005
Yes. You'd obtain a DOM Document object from an XML file using JAXP, and would then use the DOM classes Document, Node and Element to modify the document.
For output, DOMImplementationLS.createLSOutput and LSOutput.setCharacterStream or LSOutput.setByteStream allow you to send the XML to a file or elsewhere.