How to parse an XML document?

I have an XML document in a variable (not in a file). How can I get the data in this? I have no additional file with this, I have an “inside” of my source code. When i use

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(XML);

(XML is my xml variable), I get an error

java.io.FileNotFoundException: C:\netbeans\app-s7013\<network ip_addr="10.0.0.0\8" save_ip="true"> File not found.
+3
source share
2 answers

Read your XML in a StringReader, wrap it in an InputSource and pass it to your DocumentBuilder:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
+5
source

Assuming XML is a String, don't confuse the version that takes the string - the string is a URL, not your input!

You need a version that accepts an input stream.

( , Google ). StringReader.

+2

Source: https://habr.com/ru/post/1750212/


All Articles