aneuryzm
aneuryzm

Reputation: 64844

Java: parsing and writing xml files

I need to parse a xml document with Java and output another xml document.

More specifically, I have a list of items in the original xml file and I need to group them, averaging the values, and output few averaged items.

should I use any java library for that ?

thanks

Upvotes: 0

Views: 1808

Answers (5)

Adriaan Koster
Adriaan Koster

Reputation: 16209

Take a look at Xstream

Upvotes: 0

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46415

You need to use DOM or SAX parsers for parsing XML documents using Java.

The Java API for XML Processing(JAXP) , is one of the Java XML programming APIs. It provides the capability of validating and parsing XML documents.

But, it would be better if you follow these links before deciding the right approach.

Confused about DOM or SAX ?
Visit here : Should I use DOM or SAX

Also, recommended reading JAXB

Upvotes: 0

Christina
Christina

Reputation: 3732

You can either use DOM or SAX to parse an XML file. DOM allows you to navigate to each xml item, while SAX is based on events the parser emits while parsing the file. DOM is usually easier to work with, however since it loads the whole xml file into memory it won't work for large files, in which case you're better off using SAX. You can find both parsers in the javax.xml.parsers package. You can find some examples for both parsers here http://java.sun.com/developer/codesamples/xml.html

Upvotes: 0

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

The best way would be to make use of a library. Doing it without a library means you will have to code for parsing and creating your xml the hard way. I suggest you use JDom. Here is a list of Java XMl library: http://java-source.net/open-source/xml-parsers

Upvotes: 0

Uwe
Uwe

Reputation: 436

Parsing xml and writing to another XML document is best done with JDOM. However, grouping and averaging is gonna be painful with JDOM.

You could also use JAXB to create POJOs from the xml, then do your averaging and grouping on the POJO, and marshall it afterwards with JAXB back to XML.

If the target XML has a different schema then the source XML, and conversion between the POJOs is required, use dozer.

Upvotes: 1

Related Questions