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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:30:08+00:00 2026-06-09T23:30:08+00:00

I am trying to parse XML file which is of the form. <parent tag>

  • 0

I am trying to parse XML file which is of the form.

<parent tag>
    <child tag>  
        <element key="property1">value</element> 
        <element key="property2">value</element>
    </child tag>
</parent tag>

How can I get the value of the element tag having property1? My code is as follows.

public static ArrayList<String> parseXML(URL url_str,URLConnection conn_str,String root_tag,String child_tag) throws ParserConfigurationException, SAXException, IOException 
{ 
    String s = null;
    ArrayList <String> List_value=new ArrayList<String>();       
    DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbF.newDocumentBuilder();
    Document doc = dBuilder.parse(conn_str.getInputStream());
    doc.getDocumentElement().normalize();
    System.out.println("Root : "+doc.getDocumentElement());
    System.out.println("****************");
    NodeList nList= doc.getElementsByTagName(root_tag);
    System.out.println("****************");

    for (int i = 0; i < nList.getLength(); i++) {
         Node node = nList.item(i);
         if (node.getNodeType() == Node.ELEMENT_NODE) {
             Element element = (Element) node;
             NodeList nodelist1 = element.getElementsByTagName(child_tag);
             for (int i1 = 0; i1 < nodelist1.getLength(); i1++) 
             {
                 Element element1 = (Element) nodelist1.item(i1);
                 NodeList fstNm = element1.getChildNodes();
                 s=fstNm.item(0).getNodeValue();

                List_value.add(s);
            }
            for(int c=0;c<List_value.size();c++)
            {
                    System.out.println(List_value.get(c));
            }

        }

    }
    return List_value;
}

I am using DOM parser for the same. Kindly help.

Thank you.

  • 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-09T23:30:09+00:00Added an answer on June 9, 2026 at 11:30 pm

    This did the job here (DOM + XPath), more documentation here: http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

    Here is a good explanation about xpath and how it works: http://www.xmlplease.com/axis

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    
    
    public class XmlParseTest {
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse("test.xml");
    
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            XPathExpression expr = xpath.compile("//element[@key='property1']/text()");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
    
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue()); 
            }
        }
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse specific XML file which is located in sub directories
I'm trying to parse an NZB file (which is XML) and sum the bytes
Hi I am trying to Parse a local xml file (which has database table
I am trying to parse an xml file, the parent and children use -
I am Trying To Use NSXMLParser to parse an xml file using cocoa which
I am trying to parse the xml file which has collection of nested tags.I
I am trying to parse this xml file, which has some nested nodes. I
I am trying to parse an XML file with python using lxml, but get
I'm trying parse the follow XML file: <root>Root <pai>Pai_1 <filho>Pai1,Filho1</filho> <filho>Pai1,Filho2</filho> </pai> <pai>Pai_2 <filho>Pai2,Filho1</filho>
I'm trying to parse an xml file with PHP. I'm using this code and

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.