I have XML in the form of a String that contains
<message>HELLO!</message>
How can I get the String "Hello!" from the XML? It should be ridiculously easy but I am lost. The XML isn't in a doc, it is simply a String.
I have XML in the form of a String that contains
<message>HELLO!</message>
How can I get the String "Hello!" from the XML? It should be ridiculously easy but I am lost. The XML isn't in a doc, it is simply a String.
Using JDOM:
Using the Xerces
DOMParser
:Using the JAXP interfaces:
You could also use tools provided by the base JRE:
You could do this with JAXB (an implementation is included in Java SE 6).
Output
There is doing XML reading right, or doing the dodgy just to get by. Doing it right would be using proper document parsing.
Or... dodgy would be using custom text parsing with either wisuzu's response or using regular expressions with matchers.
I think you would be look at String class, there are multiple ways to do it. What about
substring(int,int)
andindexOf(int)
lastIndexOf(int)
?One of the above answer states to convert XML String to bytes which is not needed. Instead you can can use
InputSource
and supply it withStringReader
.