Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse XML document?

Tags:

java

xml

I have xml document in variable (not in file). How can i get data storaged in that? I don't have any additional file with that, i have it 'inside' my sourcecode. 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.
like image 569
qqryq Avatar asked Dec 28 '25 16:12

qqryq


1 Answers

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

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
like image 87
duffymo Avatar answered Dec 30 '25 06:12

duffymo