How to convert string to xml
I have a string which contains XML data and I want to convert it to X开发者_开发知识库ML document object. How do I do that?
say theString holds the XML,
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(theString)));
More info here
All you have to do is pass the string to the LoadXml method of the XmlDocument class. Then, you can use XPath to get the values of the elements/attributes in the document.
You could use several libraries to do this.
If you have a schema file for the XML you could use JAXB to do this. If you have no schema file you better stick with libraries like JDOM or SAXON.
精彩评论