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

  • SEARCH
  • Home
  • 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 8729491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:50:38+00:00 2026-06-13T08:50:38+00:00

I’m having some trouble parsing an XML file in Java. The file takes the

  • 0

I’m having some trouble parsing an XML file in Java. The file takes the form:

<root>
  <thing>
    <name>Thing1</name>
    <property>
      <name>Property1</name>
    </property>
    ...
  </thing>
  ...
</root>

Ultimately, I would like to convert this file into a list of Thing objects, which will have a String name (Thing1) and a list of Property objects, which will each also have a name (Property1).

I’ve been trying to use xpaths to get this data out, but when I try to get just the name for ‘thing’, it gives me all of the names that appear in ‘thing’, including those of the ‘property’s. My code is:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(filename);
XPath xpath = XPathFactory.newInstance().newXPath();


XPathExpression thingExpr = xpath.compile("//thing");
NodeList things = (NodeList)thingExpr.evaluate(dom, XPathConstants.NODESET);
for(int count = 0; count < things.getLength(); count++)
{
    Element thing = (Element)things.item(count);
    XPathExpression nameExpr = xpath.compile(".//name/text()");
    NodeList name = (NodeList) nameExpr.evaluate(thing, XPathConstants.NODESET);
    for(int i = 0; i < name.getLength(); i++)
    {
        System.out.println(name.item(i).getNodeValue());    
    }
}

Can anyone help? Thanks in advance!

  • 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-06-13T08:50:39+00:00Added an answer on June 13, 2026 at 8:50 am

    You could try something like…

    public class TestXPath {
    
        public static void main(String[] args) {
            String xml =
                            "<root>\n"
                            + "    <thing>\n"
                            + "        <name>Thing1</name>\n"
                            + "        <property>\n"
                            + "            <name>Property1</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property2</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property3</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property4</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property5</name>\n"
                            + "        </property>\n"
                            + "    </thing>/n"
                            + "    <NoAThin>\n"
                            + "        <name>Thing2</name>\n"
                            + "        <property>\n"
                            + "            <name>Property1</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property2</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property3</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property4</name>\n"
                            + "        </property>\n"
                            + "        <property>\n"
                            + "            <name>Property5</name>\n"
                            + "        </property>\n"
                            + "    </NoAThin>/n"
                            + "</root>";
    
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
                Document dom = db.parse(bais);
                XPath xpath = XPathFactory.newInstance().newXPath();
    
                // Find the "thing" node...
                XPathExpression thingExpr = xpath.compile("/root/thing");
                NodeList things = (NodeList) thingExpr.evaluate(dom, XPathConstants.NODESET);
    
                System.out.println("Found " + things.getLength() + " thing nodes...");
    
                // Find the property nodes of thing
                XPathExpression expr = xpath.compile("property");
                NodeList nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);
    
                System.out.println("Found " + nodes.getLength() + " thing/property nodes...");
    
                // Find all the property "name" nodes under thing
                expr = xpath.compile("property/name");
                nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);
    
                System.out.println("Found " + nodes.getLength() + " name nodes...");
                System.out.println("Property value = " + nodes.item(0).getTextContent());
    
                // Find all nodes that have property nodes
                XPathExpression exprAll = xpath.compile("/root/*/property");
                NodeList nodesAll = (NodeList) exprAll.evaluate(dom, XPathConstants.NODESET);
                System.out.println("Found " + nodesAll.getLength() + " property nodes...");
    
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }
    

    Which will give you an output of something like

    Found 1 thing nodes...
    Found 5 thing/property nodes...
    Found 5 name nodes...
    Property value = Property1
    Found 10 property nodes...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an XML file, the creators of it stuck in a bunch social
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text

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.