我在某个地方找到了(不记得在哪里):
public static documentFragment parseXml(document doc, String fragment) { // Wrap the fragment in an arbitrary element. fragment = "<fragment>"+fragment+"</fragment>"; try { // Create a DOM builder and parse the fragment. documentBuilderFactory factory = documentBuilderFactory.newInstance(); document d = factory.newdocumentBuilder().parse( new InputSource(new StringReader(fragment))); // import the nodes of the new document into doc so that they // will be compatible with doc. Node node = doc.importNode(d.getdocumentElement(), true); // Create the document fragment node to hold the new nodes. documentFragment docfrag = doc.createdocumentFragment(); // Move the nodes into the fragment. while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); } // Return the fragment. return docfrag; } catch (SAXException e) { // A parsing error occurred; the XML input is not valid. } catch (ParserConfigurationException e) { } catch (IOException e) { } return null;}


