Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing XML using JUST Java 1.5 (or earlier)

Tags:

java

xml

For reading XML, there is SAX and DOM built into Java 1.5. You can use JAXP and not need to know details about what parser is available... So, what are some prescribed APIs for one to write XML documents in Java 1.5 and earlier?

  • I don't want to use a third party binary
  • I don't want to assume a Sun VM or IBM VM etc and use some specialized class
  • Whatever means there is of writing the document, I would like to read in a complementary way.
  • Performance and suitability for large XML files is not particularly important

Ideally, a read-and-write with no changes is just a few lines of code.

like image 461
Dilum Ranatunga Avatar asked Mar 28 '09 16:03

Dilum Ranatunga


1 Answers

Java 1.4 comes with javax.xml.transform, which can take a DOMSource, SAXSource, etc:

// print document
InputSource inputSource = new InputSource(stream);
Source saxSource = new SAXSource(inputSource);
Result result = new StreamResult(System.out);
TransformerFactory transformerFactory = TransformerFactory
    .newInstance();
Transformer transformer = transformerFactory
    .newTransformer();
transformer.transform(saxSource, result);

If you want to go back to the J2SE 1.3 API, you're pretty much on your own (though if you're on the J2EE API of that era, there might be something - I don't recall).

like image 128
McDowell Avatar answered Oct 23 '22 00:10

McDowell