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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:51:34+00:00 2026-05-25T18:51:34+00:00

How to extract the text of such an element via XPath: <document> some text

  • 0

How to extract the text of such an element via XPath:

<document>
  some text
     <subelement>subelement text</subelement>
  postscript
</document>

The XPath expression:

/document

returns document node text and all its subnodes text:

some text         subelement text    postscript

While the XPath expression:

/document/text()

returns just the first text node:

some text

that is, “postscript” is missing.

Question
Is there a way to get the text of all text nodes that are immediate sons of <document>?

Postscript
Very focused Example, in case you want to test yourself, copy into a main method and fix the imports.

    DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
                 "<document>"
                 + "some text into document"
                 + "    <subelement>"
                 + "        some text into SUBelement"
                 + "    </subelement>"
                 + "POSTSCRIPT"
                 + "</document>";

    //i'm forced to use an InputSource because parse doesn't take readers directly :-(
    Document doc = dbuilder.parse(new InputSource(new StringReader(xml)));

    //usual way to get an xpath
    XPath xp = XPathFactory.newInstance().newXPath();

    System.out.println(xp.evaluate("/document", doc));

    System.out.println(xp.evaluate("/document/text()",doc));
  • 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-25T18:51:34+00:00Added an answer on May 25, 2026 at 6:51 pm

    While the XPath expression:

    /document/text()
    

    returns just the first text node:

    some text into document
    

    that is, “postscript” is missing.

    The XPath expression above returns all text node children of /document, but the XPath.evaluate() method, with no 3rd argument converts its result to a string.
    In the process, it apparently acts like <xsl:value-of> in that it only converts the first node in the result node-set.

    To print the value of all text node children, supply XPathConstants.NODESET as the 3rd argument to XPath.evaluate(). This will give you the nodeset of text nodes as a NodeList. Then you can loop through them and print each one. Or you could try passing the NodeList directly to println(), and see what it prints. 🙂

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

Sidebar

Related Questions

i want to extract some text in certain website. here is web address what
I'm reading an XML document using JavaScript & jQuery, and need to extract some
I have text files formatted as such: R156484COMP_004A7001_20100104_065119.txt I need to consistently extract the
My application has a feature that parses text using a regular expression to extract
What's the best/most efficient way to extract text set between parenthesis? Say I wanted
I'm currently using POI to attempt to extract text out of a batch of
I have a big problem. I want to extract text from html table that
I need to extract multiple text/dropdown list fields from an asp.net form and format
How to extract a text part by regexp in linux shell? Lets say, I
I want to extract the text information (Chinese) from the images that picked by

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.