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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:56:13+00:00 2026-05-20T14:56:13+00:00

The below code prints the element contents of my XML file, however, for the

  • 0

The below code prints the element contents of my XML file, however, for the life of me I have been unsuccessful at extracting the element values and putting them in a map or other type of array or list. Any help would be greatly appreciated!!!!

  public class client
  {
     public static void main(String[] args )
     {
        int length = 0;
        String [] array;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {

           //Using factory get an instance of document builder
           DocumentBuilder builder  = factory.newDocumentBuilder();

           //create the document by parsing the actual file
           Document doc = builder.parse(new File ("server.xml"));

           //creat an XPath to be used in the extraction of the nodes
           XPathFactory xPFactory = XPathFactory.newInstance();
           XPath path = xPFactory.newXPath();

           //Extract ports
           XPathExpression portExpr
            = path.compile("//configuration/hostInfo/port");

           //Extract hosts
           XPathExpression hostExpr
            = path.compile("//configuration/hostInfo/host");

           Object pResult = portExpr.evaluate(doc, XPathConstants.NODESET);
           Object hResult = hostExpr.evaluate(doc, XPathConstants.NODESET);
           NodeList pNodes = (NodeList) pResult;
           NodeList hNodes = (NodeList) hResult;
           array = new String [(pNodes.getLength())*2];

           array = populateArray(array, pNodes, hNodes);
           for (int k = 0; k < array.length; k++)
               System.out.println( k+ "="+ array[k]);

        }catch (Exception e) {
           e.printStackTrace();
     }
  }

  public static String[] populateArray (String array[], NodeList pNodes, NodeList hNodes){

        array[0]=pNodes.item(0).getTextContent();
        array[1]=hNodes.item(1).getTextContent();

        for (int i = 1; i < pNodes.getLength(); i++){
           array[2*i]= pNodes.item(i).getTextContent();
           array[(2*i)+1]= hNodes.item(i).getTextContent();
        }
        return array;
     }

  }

XML FILE

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
   <hostInfo>
     <port>8189</port>
     <host>localhost</host>    
   </hostInfo>
   <hostInfo>
     <port>8190</port>
     <host>localhost</host>
   </hostInfo>
   <hostInfo>
     <port>8191</port>
     <host>localhost</host>
   </hostInfo>  
 </configuration>
  • 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-20T14:56:13+00:00Added an answer on May 20, 2026 at 2:56 pm

    You can do path.compile that will return a XPathExpression object. Then you can call evaluate to get an array of NodeList.

    Take a look at http://www.ibm.com/developerworks/library/x-javaxpathapi.html and http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/package-summary.html .

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

Sidebar

Related Questions

I have a python psp page code is shown below. Currently it only prints
The code below prints out all comments for a given submissionid in chronological order.
The below code reads in a file and then counts the number of 'a',
I'm reading a XML file same as below: <ts> <tr comment= label=tr1> <node order=1
I have a simple XML representation of a table below. When I traverse the
The code below breaks up the @stat_array into fours and then prints out fields
I have the below (simplified) code, which uses the following source: <html> <p>line 1</p>
I have some code (below) where I am assigning the address of an integer
In the below code, I am looping through each XML and creating dynamic content.
In the example code below, I have a question about the List. My prof

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.