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.

Share:
284,743

Related videos on Youtube

Dewayne
Author by

Dewayne

Updated on July 30, 2021

Comments

  • Dewayne
    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
      Christophe Roussy about 8 years
      Also note that javax.xml.parsers.DocumentBuilder.parse(string) assumes the string is a uri (terrible...)
  • palantus
    palantus about 15 years
    I'd prefer the StringReader because it avoids String.getBytes(), but this should usually work also.
  • bendin
    bendin about 15 years
    When 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
    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
    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
    nkuebelbeck almost 11 years
    thanks much, saved me bunch lines of code, i was converting it back to text but I knew there was a better way!
  • Dejell
    Dejell over 10 years
    if I have <?XML> it returns an empty node what can i do?
  • Daniel Eisenreich
    Daniel Eisenreich over 4 years
    Check that you use the right import statement: import org.xml.sax.InputSource;
  • john ktejik
    john ktejik about 3 years
    For me, parse() takes InputStream, not InputSource
  • John LaBarge
    John LaBarge about 2 years
    Why is everything in Java so gross? Seriously?