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

  • Home
  • SEARCH
  • 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

How come unserialize isn't restoring my array? See code below.. // prints a:1:{s:8:txn_type;s:32:recurring_payment_profile_cancel;} echo
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
Okay so I have a scenario similar to the below code, I have a
The code below breaks up the @stat_array into fours and then prints out fields
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *
In the below code sample, what does {0:X2} mean? This is from the reflection
In the below code, the ListBox gets filled with the names of the colors
I would like to know how to modify the below code to strip =20
When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should

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.