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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:43:08+00:00 2026-06-08T18:43:08+00:00

I have the following XML: <ConfigGroup Name=Replication> <ValueInteger Name=ResponseTimeout>10</ValueInteger> <ValueInteger Name=PingTimeout>2</ValueInteger> <ValueInteger Name=ConnectionTimeout>10</ValueInteger> <ConfigGroup

  • 0

I have the following XML:

     <ConfigGroup Name="Replication">
        <ValueInteger Name="ResponseTimeout">10</ValueInteger>
        <ValueInteger Name="PingTimeout">2</ValueInteger>
        <ValueInteger Name="ConnectionTimeout">10</ValueInteger>
        <ConfigGroup Name="Pool">
            <ConfigGroup Name="1">
                <ValueString Encrypted="false" Name="Host">10.20.30.40</ValueString>
                <ValueInteger Name="CacheReplicationPort">8899</ValueInteger>
                <ValueInteger Name="RadiusPort">12050</ValueInteger>
                <ValueInteger Name="OtherPort">4868</ValueInteger>
            </ConfigGroup>
            <ConfigGroup Name="2">
                <ValueString Encrypted="false" Name="Host">10.20.30.50</ValueString>
                <ValueInteger Name="CacheReplicationPort">8899</ValueInteger>
                <ValueInteger Name="RadiusPort">12050</ValueInteger>
                <ValueInteger Name="OtherPort">4868</ValueInteger>
            </ConfigGroup>
        </ConfigGroup>
     </ConfigGroup>

I just wondering what is the simplest way to parse this XML in Java – I want the value from the two host elements (e.g. 10.20.30.40 and 10.20.30.50). Note there may be more than two pool entries (or none at all).

I’m having trouble finding a simple example of how to use the various XML parsers for Java.

Any help is much appreciated.

Thanks!

  • 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-08T18:43:09+00:00Added an answer on June 8, 2026 at 6:43 pm

    The simplest way to search for what you are looking for, would be XPath.

    try {
    
        //Load the XML File
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document configuration = builder.parse("configs.xml");
    
        //Create an XPath expression
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expr = xpath.compile("//ConfigGroup/ValueString[@Name='Host']/text()");
    
        //Execute the XPath query
        Object result = expr.evaluate(configuration, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
    
        //Parse the results
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue()); 
        }
    } catch (ParserConfigurationException e) {
        System.out.println("Bad parser configuration");
        e.printStackTrace();
    } catch (SAXException e) {
        System.out.println("SAX error loading the file.");
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        System.out.println("Bad XPath Expression");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO Error reading the file.");
        e.printStackTrace();
    }
    

    The XPath expression

    "//ConfigGroup/ValueString[@Name='Host']/text()"
    

    looks for ConfigGroup elements anywhere in your XML, then finds ValueString elements within the ConfigGroup elements, that have a Name attribute with the value “Host”. @Name=Host is like a filter for elements with the name ValueString. And text() at the end, returns the text node of the selected elements.

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

Sidebar

Related Questions

I have the following XML: <?xml version=1.0 encoding=UTF-8?> <game> <name>Space Blaster</name> <description></description> <version>1</version> <fullscreen>false</fullscreen>
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I have following xml. <Name> <First>john</First> <Last>smith</Last> </Name> I want to capitalize first letter
I have example the following xml: <LOCAL_AUTHORITY> <NAME>Derby</NAME> <REGION></REGION> <IRD>22%</IRD> <LOCAL_AUTHORITY> I have the
I have following XML: <Library> <Item> <Field Name=Name>smooth</Field> <Field Name=File Type>mid</Field> <Field Name=File Size>60026</Field>
I have following XML string: <persons> <person> <name>Someone</name> <age>27</age> </person> <person> <name>Otherone</name> <age>43</age> </person>
I have following XML: <Users> <User Id=1> <Name>Name1</Name> <LastName>LastName1</LastName> </User> <User Id=5> <Name>Name5</Name> <LastName>LastName5</LastName>
i have following xml file: <package> <reader> <id>r1007</id> <name>Robert</name> <email>robert@yahoo.com</email> </reader> <writer> <id>w1920</id> <name>Young</name>
I have following XML: <Users> <User Id="1"> <Name>abc</Name> <LastName>d</LastName> </User> <User Id="2"> <Name></Name> <LastName>ab</LastName>
I have the following xml. Given, a UID , Manufacturer Name and Image Layout

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.