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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:07:22+00:00 2026-06-11T20:07:22+00:00

My xml file is look like this . I want to get the value

  • 0

My xml file is look like this . I want to get the value node text content as like this .

<property regex=".*" xpath=".*">
     <value>
          127.0.0.1
     </value>
<property regex=".*" xpath=".*">
<value>

</value>
</property>

I want to get text as order they specified in a file . Here is my java code .

Document doc = parseDocument("properties.xml");
NodeList properties = doc.getElementsByTagName("property");
for( int i = 0 , len = properties.getLength() ; i < len ; i++) {
     Element property = (Element)properties.item(i);
     //How can i proceed further .
}

Output Expected :

 Node 1 : 127.0.0.1

Please suggest your views .

  • 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-11T20:07:23+00:00Added an answer on June 11, 2026 at 8:07 pm

    The following method looks for all property elements within the document and collects all value children of those elements named value wihtout using XPath.

      private static List<Element> getValueElements(Document document) {
        List<Element> result = new ArrayList<Element>();
        NodeList propertyElements = document.getElementsByTagName("property");
        for (int i = 0, ilen = propertyElements.getLength(); i < ilen; i++) {
          Node propertyNode = propertyElements.item(i);
          if (!(propertyNode instanceof Element))
            continue;
    
          NodeList children = ((Element) propertyNode).getChildNodes();
          for (int j = 0, jlen = children.getLength(); j < jlen; j++) {
            Node child = children.item(j);
            if (!(child instanceof Element) || !"value".equals(child.getNodeName()))
              continue;
    
            result.add((Element) child);
          }
        }
        return result;
      }
    

    But you can do the same in a more elegant way using the XPath expression //property/value:

    private static List<Element> getValueElementsUsingXpath(Document document) throws XPathExpressionException {
      XPath xpath = XPathFactory.newInstance().newXPath();
      // XPath Query for showing all nodes value
      XPathExpression expr = xpath.compile("//property/value");
      Object xpathResult = expr.evaluate(document, XPathConstants.NODESET);
    
      List<Element> result = new ArrayList<Element>();
      NodeList nodes = (NodeList) xpathResult;
      for (int i = 0; i < nodes.getLength(); i++) {
        Node valueNode = nodes.item(i);
        if (!(valueNode instanceof Element)) continue;
        result.add((Element) valueNode);
      }
    
      return result;
    }
    

    You can use the method above like this:

      public static void main(String[] args) throws Exception {
        Document doc = parseDocument("properties.xml");
        List<Element> valueElements = getValueElements(doc);  // or getValueElementsUsingXpath(doc)
    
        int nodeNumber = 0;
        for (Element element : valueElements) {
          nodeNumber++;
          System.out.println("Node " + nodeNumber + ": " + formatValueElement(element));
        }
      }
    
      private static String formatValueElement(Element element) {
        StringBuffer result = new StringBuffer();
    
        boolean first = true;
        NodeList children = ((Element) element).getChildNodes();
        for (int i = 0, len = children.getLength(); i < len; i++) {
          Node child = children.item(i);
    
          String childText = null;
          switch (child.getNodeType()) {
          case Node.CDATA_SECTION_NODE:
          case Node.TEXT_NODE:
            childText = child.getTextContent().trim();
          }
    
          if (childText == null || childText.isEmpty()) {
            continue;
          }
    
          if (first)
            first = false;
          else
            result.append(" ");
    
          result.append(childText);
        }
    
        return result.toString();
      }
    

    I tested it with the following two XML inputs, since your XML lacks a closing </property> tag.

    Here is the first one (I added extra elements, to show that they are not found):

      <rootNode>
      <property regex=".*" xpath=".*">
           <value>
                127.0.0.1
           </value>
           <anythingElse>Text here</anythingElse>
      </property>
      <anythingElse>Text here</anythingElse>
      <property regex=".*" xpath=".*">
      <value>
           val <![CDATA[
           <Valve className="org.tomcat.AccessLogValve" exclude="PASSWORD,pwd,pWord,ticket" enabled="true" serviceName="zohocrm" logDir="../logs" fileName="access" format="URI,&quot;PARAM&quot;,&quot;REFERRER&quot;,TIME_TAKEN,BYTES_OUT,STATUS,TIMESTAMP,METHOD,SESSION_ID,REMOTE_IP,&quot;INTERNAL_IP&quot;,&quot;USER_AGENT&quot;,PROTOCOL,SERVER_NAME,SERVER_PORT,BYTES_IN,ZUID,TICKET_DIGEST,THREAD_ID,REQ_ID"/>
           ]]> test
      </value>
      </property>
      </rootNode>
    

    The second one has nested property elements (I added the missing element at the end):

      <property regex=".*" xpath=".*">
          <value>
              127.0.0.1
          </value>
          <property regex=".*" xpath=".*">
          <value>
              val <![CDATA[
              <Valve className="org.tomcat.AccessLogValve" exclude="PASSWORD,pwd,pWord,ticket" enabled="true" serviceName="zohocrm" logDir="../logs" fileName="access" format="URI,&quot;PARAM&quot;,&quot;REFERRER&quot;,TIME_TAKEN,BYTES_OUT,STATUS,TIMESTAMP,METHOD,SESSION_ID,REMOTE_IP,&quot;INTERNAL_IP&quot;,&quot;USER_AGENT&quot;,PROTOCOL,SERVER_NAME,SERVER_PORT,BYTES_IN,ZUID,TICKET_DIGEST,THREAD_ID,REQ_ID"/>
              ]]> test
          </value>
          </property>
      </property>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file with elements that look like this (abbreviated for clarity):
I have an XML File with Elements that look like the following: <level> <name>Name
I want to get values under specific tags of my xml file (accessed via
I need to extract requests from a log file that look like this :
XML File Sample <GateDocument> <GateDocumentFeatures> ... </GateDocumentFeatures> <TextWithNodes> <Node id=0/> MESSAGE SET <Node id=19/>
I have an XML file from which I am parsing some content to display
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I'm wrting something that look like this (of course its a bit more complex
I want to translate a given XML file (it is a RelaxNG grammar) to
How do I get this Ant file to generate my stubs in the ./src

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.