更新时间:2022-04-10 04:39:29
以下代码将String转换为XML文档。获得文档后,您可以导航节点,也可以写入文件等。
The below code converts a String to an XML document. Once you have the document, you can navigate the nodes or you can write to a file etc.
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class Util {
public static Document stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
}