2

I am trying to import an image from an SVG format into the software I am working with. For example, I have an SVG like this:

<svg height="100" width="100">
    <text>I Love SVG!
        <tspan> NOT! </tspan>"
    </text>
</svg>

When processing this data the 'text' element, I have TextString= "element.getTextContent()". This makes TextString = "I Love SVG! NOT!" when all I want is "I Love SVG!". So the getTextContent method returns the text from the element and its child elements, when I don't want to include the child elements text.

Is there a simple way to grab only the text content of an element without getting the child nodes text as well? Thanks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

2 Answers2

0

You can iterate through the element's child nodes using element.getChildNodes(). Any of them that have a node type of Node.TEXT_NODE are text nodes (there will often be more than one).

String textContent = "";
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.length(); i++) {
    Node n = childNodes.item(i);
    if (n.getNodeType() == Node.TEXT_NODE) {
       textContent += n.getNodeValue();
    }
}
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • OK, but I want the text content for the original text element to only have the text of its element and not its children (e.g. the value of TextString for text to be " I Love SVG!", and TextString for tspan to be "Not!" – DirtyProgrammer Dec 22 '14 at 14:29
  • @DirtyProgrammer Yes, that's exactly what I'm showing you how to do. The `text` element in your example has two children that are text nodes. One is a text node containing `I Love SVG!` and some whitespace, and the other is a text node containing `"` and some whitespace (maybe that `"` was just a typo). Since there can be several, you should iterate through them, trim off the whitespace, and concatenate them together as needed. – JLRishe Dec 22 '14 at 14:32
  • What is happening if I concatenate them is I'll get the string: "I Love SVG! NOT! NOT!" – DirtyProgrammer Dec 22 '14 at 14:35
  • @DirtyProgrammer Did you include the `if (n.getNodeType() == Node.TEXT_NODE)` part? – JLRishe Dec 22 '14 at 14:36
0

Found out a solution to my problem, thanks for the suggestions JLRishe and kulatamicuda.

instead of TextString = element.getTextContent() ( which got the text from all child nodes as well as the original text node)

I used TextString = element.getFirstChild().getTextContent().

When using this I made sure the first child was actually a text node and not something like this:

<text><tspan>tspan text</tspan></text>