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 8315099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:02:51+00:00 2026-06-08T21:02:51+00:00

I have this XML: <root> <items> <item1> <tag1>1</tag1> <sub> <sub1>10 </sub1> <sub2>20 </sub2> </sub>

  • 0

I have this XML:

<root>
  <items>
    <item1>
      <tag1>1</tag1>            
      <sub>
        <sub1>10 </sub1>
        <sub2>20 </sub2>
      </sub>
    </item1>

    <item2>
      <tag1>1</tag1>            
      <sub>
        <sub1> </sub1>
        <sub2> </sub2>
      </sub>        
    </item2>
  </items>
</root>

I want to get the item1 element and the name and values of the child elements.

That is, i want to get: tag1 – 1,sub1-10,sub2-20.

How can i do this? so far i can only get elements without children.

  • 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-08T21:02:53+00:00Added an answer on June 8, 2026 at 9:02 pm
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    /**
    * File: Ex1.java @author ronda
    */
    public class Ex1 {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = Factory.newDocumentBuilder();
        Document doc = builder.parse("myxml.xml");
    
        //creating an XPathFactory:
        XPathFactory factory = XPathFactory.newInstance();
        //using this factory to create an XPath object: 
        XPath xpath = factory.newXPath();
    
        // XPath Query for showing all nodes value
        XPathExpression expr = xpath.compile("//" + "item1" + "/*");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        System.out.println(nodes.getLength());
        for (int i = 0; i < nodes.getLength(); i++) {
    
            Element el = (Element) nodes.item(i);
    
            System.out.println("tag: " + el.getNodeName());
            // seach for the Text children
            if (el.getFirstChild().getNodeType() == Node.TEXT_NODE)
                System.out.println("inner value:" + el.getFirstChild().getNodeValue());
    
            NodeList children = el.getChildNodes();
            for (int k = 0; k < children.getLength(); k++) {
                Node child = children.item(k);
                if (child.getNodeType() != Node.TEXT_NODE) {
                    System.out.println("child tag: " + child.getNodeName());
                    if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
                        System.out.println("inner child value:" + child.getFirstChild().getNodeValue());;
                }
            }
        }
    }
    }
    

    I get this output loading the xml of your question in file named: myxml.xml:

    run:
    2
    tag: tag1
    inner value:1
    tag: sub
    inner value:
    
    child tag: sub1
    inner child value:10 
    child tag: sub2
    inner child value:20
    

    …a bit wordy, but allow us to understand how it works. PS: I found a good guide in here

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

Sidebar

Related Questions

i have this XML document: <?xml version=1.0 encoding=UTF-16?> <root> <items> <item1> <tag1>1</tag1> <tag2>2</tag2> <tag3>3</tag3>
Using XPath. I have this doc: <?xml version=1.0?> <root> <items> <item1> <tag1>1</tag1> <tag2>DFGGFDGF</tag2> <tag3>3</tag3>
I have this source XML: <?xml version=1.0?> <root> <item1> <name>test</name> <price>160</price> <stock>4</stock> <country>Belgium</country> </item1>
I have this XML: <root> <tab name=Detail> <section name=mysection> <items level=1> <Idx_name>9</Idx_name> <Type>mytype</Type> <item
I have the following XML: <funds> <fund name=A ITEM0=7% ITEM1=8% ITEM2=9% ITEM3=10% ITEM4=11% ITEM5=
I have a XML file, like this: <?xml version=1.0 encoding=utf-8 ?> <ROOT> <NAME> ItemName
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
Suppose I have the following XML structure: <root> <item> <item1>some text is <b>here</b></item1> <item2>more
I have this XML at http://localhost/file.xml : <?xml version=1.0 encoding=utf-8?> <val:Root xmlns:val=http://www.hw-group.com/XMLSchema/ste/values.xsd> <Agent> <Version>2.0.3</Version>
I have a RelativeLayout with one child as <include another.xml> like this root.xml :

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.