yogsma
yogsma

Reputation: 10594

How to convert string to xml

I have a string which contains XML data and I want to convert it to XML document object. How do I do that?

Upvotes: 3

Views: 14515

Answers (3)

Preet Sangha
Preet Sangha

Reputation: 65556

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

Upvotes: 8

flash
flash

Reputation: 6820

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.

Upvotes: 0

John McCloskey
John McCloskey

Reputation: 117

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.

Upvotes: 0

Related Questions