In Java, how do I parse XML as a String instead of a file?
284,743
Solution 1
I have this function in my code base, this should work for you.
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
also see this similar question
Solution 2
One way is to use the version of parse that takes an InputSource rather than a file
A SAX InputSource can be constructed from a Reader object. One Reader object is the StringReader
So something like
parse(new InputSource(new StringReader(myString))) may work.
Solution 3
Convert the string to an InputStream and pass it to DocumentBuilder
final InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.parse(stream);
EDIT
In response to bendin's comment regarding encoding, see shsteimer's answer to this question.
Solution 4
I'm using this method
public Document parseXmlFromString(String xmlString){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
org.w3c.dom.Document document = builder.parse(inputStream);
return document;
}
Solution 5
javadocs show that the parse method is overloaded.
Create a StringStream or InputSource using your string XML and you should be set.
Related videos on Youtube
Author by
Dewayne
Updated on July 30, 2021Comments
-
Dewayne almost 3 years
I have the following code:
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
How can I get it to parse XML contained within a String instead of a file?
-
Christophe Roussy about 8 yearsAlso note that
javax.xml.parsers.DocumentBuilder.parse(string)
assumes the string is a uri (terrible...)
-
-
palantus about 15 yearsI'd prefer the StringReader because it avoids String.getBytes(), but this should usually work also.
-
bendin about 15 yearsWhen you call getBytes(), what encoding are you expecting it to use? How are you telling to the XML parser which encoding it's getting? Do you expect it to guess? What happens when you are on a platform where the default encoding isn't UTF-8?
-
sattu almost 11 years@shsteimer I am passing in xml string and it is returning null. It does not throw any exception. What must be wrong?
-
Alexander Malakhov almost 11 years@sattu: You should post it as a new question. It's really hard to tell without seeing your code.
-
nkuebelbeck almost 11 yearsthanks much, saved me bunch lines of code, i was converting it back to text but I knew there was a better way!
-
Dejell over 10 yearsif I have <?XML> it returns an empty node what can i do?
-
Daniel Eisenreich over 4 yearsCheck that you use the right import statement:
import org.xml.sax.InputSource;
-
john ktejik about 3 yearsFor me, parse() takes InputStream, not InputSource
-
John LaBarge about 2 yearsWhy is everything in Java so gross? Seriously?