如何将字符串转换为xml

时间:2021-07-15 21:47:29

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

我有一个包含XML数据的字符串,我想将其转换为XML文档对象。我怎么做?

3 个解决方案

#1


8  

say theString holds the XML,

说theString包含XML,

        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(theString)));

More info here

更多信息在这里

#2


0  

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.

您所要做的就是将字符串传递给XmlDocument类的LoadXml方法。然后,您可以使用XPath获取文档中元素/属性的值。

#3


0  

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.

如果您有XML的模式文件,则可以使用JAXB执行此操作。如果没有模式文件,最好坚持使用JDOM或SAXON等库。

#1


8  

say theString holds the XML,

说theString包含XML,

        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(theString)));

More info here

更多信息在这里

#2


0  

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.

您所要做的就是将字符串传递给XmlDocument类的LoadXml方法。然后,您可以使用XPath获取文档中元素/属性的值。

#3


0  

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.

如果您有XML的模式文件,则可以使用JAXB执行此操作。如果没有模式文件,最好坚持使用JDOM或SAXON等库。