Jdom 如何将 String 转换为 Document 对象

如何将 String 字符串转换为 XML 的JDom document 文档对象?

如果使用的文件的方式:

try { SAXBuilder builder = new SAXBuilder(); Document anotherDocument = builder.build(new File("/some/directory/sample.xml")); } catch(JDOMException e) { e.printStackTrace(); } catch(NullPointerException e) { e.printStackTrace(); }

如果使用字符串,使用下面的代码:

String exampleXML = "<your-xml-string>"; InputStream stream = new ByteArrayInputStream(exampleXML.getBytes("UTF-8")); Document anotherDocument = builder.build(stream);