Java XMLStreamReader, how to get element text when text contains START_ELEMENT
Asked Answered
M

2

6

If I have an XML element such as:

<title>this is a title</title>

The text is very easy to get using XMLStreamReader.getElementText(). However, if I run into an element like this, I cannot figure out how to get the text:

<title>this is a <othertag>title with another</othertag> tag inside of it</title>

I wasn't even sure if that was valid XML, but it seems to pass the W3C validator I tried it on. According to the API docs, you can't use getElementText() to get the text if you are going to encounter another START_ELEMENT event inside. So... what can you use?

Maegan answered 4/11, 2012 at 12:4 Comment(0)
I
3

Use getText

getText() returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD. If an ENTITY_REFERENCE has been resolved, any character data will be reported as CHARACTERS events.

It answered 4/11, 2012 at 12:29 Comment(1)
Thanks! This should have been obvious but my brain was fried after about 16 hours of coding nonstop.Maegan
P
1

I think you need to parse the elements inside the <title> tag separately (as this is a and then the <othertag>...</...> section followed by tag inside of it

Pamphylia answered 4/11, 2012 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.