Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 948015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:10:26+00:00 2026-05-15T23:10:26+00:00

I guess this question of mine is pretty basic but since Ive never done

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:10:27+00:00Added an answer on May 15, 2026 at 11:10 pm

    You could try using XPath, it seems to fit your problem. For example:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); // never forget this!
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("a.xml");
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xpath = xPathFactory.newXPath();
    XPathExpression expr = xpath.compile("//dataObject[@name='pcsp']/map/entry/string[text()='shipment_id']/../string[2]/text()");
    NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    System.out.println(result.item(0).getNodeValue());
    

    prints 143486104007.

    A little explanation on the xPath expression used:

    • //dataObject[@name=’pcsp’] – select from all dataObject tags with atribute name equals to ‘pcsp’
    • /map/entry – select child map and then child entry
    • /string[text() = ‘shipment_id’] – select child string tag with inner text equal to ‘shipment_id’
    • /.. – select parent of previous node (in this case the entry that has the shipment_id string
    • /string[2] – select second child string tag
    • / text() – select inner text of previous tag (143486104007)

    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.

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess the topic says it. I have tried googling this, but havent gotten
I guess this question isn't only specific to YUI, but that's the JS library
I guess this question was asked in one way or another, but I'm looking
I have a HashMap (although I guess this question applies to other collections) of
Ok another WPF question, well I guess this is just general .NET. I have
Okay, I guess this is entirely subjective and whatnot, but I was thinking about
Well, I guess this day had to come. My client's website has been compromised
I'm learning about POSIX threads right now, but I guess this is just a
This question is related to a previous post of mine Here . Basically, I
This site is pretty good but I'm wondering what other resources are out there.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.