Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String as Document

Tags:

java

document

I'm following this turorial on parsing XML with XPath, and it gives the following example to open a document:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

What I would like to do, is change that so that the Document is reading a String variable that I already have made, instead of reading from a file. How can I do that?

like image 997
Chiggins Avatar asked Feb 25 '26 18:02

Chiggins


1 Answers

builder.parse(new InputSource(new StringReader("<some><xml></xml></some>")));
like image 80
Jan Thomä Avatar answered Feb 28 '26 07:02

Jan Thomä