I guess this question of mine is pretty basic but since Ive never done it before or havent come across anything good on the internet while i searched for this, here goes …
I have an XML that I want my java code to read.
The sample of the XML would be as follows —
<getLabel labelId="BLAH">
<dataObject name="packageInfo">
<map>
<entry><string>shipment_id</string><string>143486104007</string></entry>
<entry><string>package_id</string><string>1</string></entry>
<entry><string>station_id</string><string>308</string></entry>
<entry><string>include_invoices</string><string>true</string></entry> </map>
</dataObject>
<dataObject name="pcsp">
<map>
<entry><string>shipment_id</string><string>143486104007</string></entry>
<entry><string>package_id</string><string>1</string></entry>
<entry><string>shipper_id</string><string>8429098020</string></entry>
<entry><string>scale_weight</string><string>3.01</string></entry>
<entry><string>bill_weight</string><string>4.0</string></entry>
<entry><string>package_type</string><string>BOX</string></entry>
<entry><string>station_id</string><string>308</string></entry>
</map>
</dataObject>
</getLabel>
Though, the size of the data within the XML is not fixed, my ultimate motive is to read the package_id and the shipment_id within the ‘pcsp’ dataobject section of the xml and store the values, ‘143486104007’ and ‘1’ in variables.
Is there any easy way to do that in java without having to use any external API/XMLReader ?
IF not, is there an easy to use external API – open source that would help my cause?
Regards
p1nG
You could try using XPath, it seems to fit your problem. For example:
prints 143486104007.
A little explanation on the xPath expression used:
The evaluation returns a NodeList of all the nodes that match the expression (in this case, just 1).
The classes are all included in the jdk.
and you’ll need to do some exception handling as well on the code.
Now, this only work if you need to get certain tags, which is what I understood you wanted. If you want to read all the values in the xml then you’re better off using something like SAX or DOM.