How to get only top-level text content of a node using getTextContent ()

I am trying to get only top-level text and none of the child text. So I have the following xml:

<job>
  text1
  <input> text2 </input>
</job>

and I would like to get only the parent text (text1). So in this example, I would do

node.getTextContent();

and get text1, not the text1text2one that currently gives getTextContent. Now I read the man pages , and I know that they say that getTextContent returns the concatenated string of all the children with the parent. But I would like the text from the parent. Another way I thought about this is to try to isolate the parent from the children and run the getTextContent command only for the parent, but I don't know how much this is possible.

Any help would be appreciated

, -Josh

+3
6

node , . , XPath.

+2

getChildNodes()? , Nodes getContent() node.getContent(). , node.

: -. , xml , .

+2

node.getTextContent();

:

if (node.getFirstNode() != null) 
{
  node.getFirstChild().getTextContent();
}
+2

I think you can probably use xpath for job / text (), it might be easier than navigating the DOM model.

If you can, avoid mixed content, its a bit of a pain to work with.

+1
source

If someone is having problems with this, the best way I have found is to get all the child nodes of the node, and then get the node type of each child node. If you get a text node call getTextContent () on that node and there you go!

0
source

node.firstChild.textContent.trim();

0
source

Source: https://habr.com/ru/post/1785514/


All Articles