AWT
AWT

Reputation: 3707

Which Java structure should I use to store XML records in?

Ok, so I am still relatively new to Java and I'm making pretty good progress. My task is this:

  1. Build a SOAP request to initiate communicates with a web services server (done)
  2. Retrieve the resulting XML which contains a unique session ID which must be used in step 3 (done)
  3. Create another SOAP request using the unique session ID that returns another set of XML containing 100 rows of records (done)
  4. Extract specific data from these results (in progress)

My question is, what is the best way to store this data in Java so that I can sort through it easily? The data portion of my XML looks like this:

<RawData>
<item value="1" anothervalue="2" yetanothervalue="3"/>
<item value="4" anothervalue="5" yetanothervalue="6"/>
<item value="7" anothervalue="8" yetanothervalue="9"/>
</RawData>

I have no problem using XPath and SimpleXPathEngine to retrieve specific values from the XML. But I would really love to be able to store it in some sort of ResultSet type structure so that I could easily retrieve and manipulate it. I've used ResultSet with SQL queries so I'm familiar and comfortable with it, however, I'm not sure how to use it outside of an actual DB connection and query. What would be the best way to handle this?

Thanks in advance!

Upvotes: 1

Views: 495

Answers (3)

Bozho
Bozho

Reputation: 597056

If the document is not that big, you can use a DOM parser to get all data in-memory. That's either org.w3c.dom, dom4j or jdom

Upvotes: 4

Grigoris Grigoriadis
Grigoris Grigoriadis

Reputation: 486

You can store the data in a memory-only database. hsqldb supports this functionality.

Upvotes: 2

Voo
Voo

Reputation: 30216

Well since your data seems simple and regular I'd personally use JAXB - you can either create the classes by hand or use some tool to generate them from the XML schema.

That way you can easily work with a List of your classes and can use your usual java to get at the data or manipulate it, also you can easily ignore fields you don't need in your java representation so your classes will only cover those parts of the XML file you're really interested in.

Upvotes: 2

Related Questions