Cannot create XML Document from String

2019-09-05 05:22发布

I am trying to create an org.w3c.dom.Document form an XML string. I am using this How to convert string to xml file in java as a basis. I am not getting an exception, the problem is that my document is always null. The XML is system generated and well formed. I wish to convert it to a Document object so that I can add new Nodes etc.

public static org.w3c.dom.Document stringToXML(String xmlSource) throws Exception {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

InputStream input = IOUtils.toInputStream(xmlSource); //uses Apache commons to obtain InputStream
BOMInputStream bomIn = new BOMInputStream(input); //create BOMInputStream from InputStream
InputSource is = new InputSource(bomIn); // InputSource with BOM removed

Document document = builder.parse(new InputSource(new StringReader(xmlSource)));
Document document2 = builder.parse(is);
System.out.println("Document=" + document.getDoctype()); // always null
System.out.println("Document2=" + document2.getDoctype()); // always null

return document;
}

I have tried these things: I created a BOMInputStream thinking that a BOM was causing the conversion to fail. I thought that this was my issue but passing the BOMInputStream to the InputSource doesn't make a difference. I have even tried passing a literal String of simple XML and nothing but null. The toString method returns [#document:null]

I am using Xpages, a JSF implementation that uses Java 6. Full name of Document class used to avoid confusion with Xpage related class of the same name.

标签: java xpages
2条回答
对你真心纯属浪费
2楼-- · 2019-09-05 05:46

You can try using this: http://www.wissel.net/blog/downloads/SHWL-8MRM36/$File/SimpleXMLDoc.java

查看更多
Melony?
3楼-- · 2019-09-05 05:48

Don't rely on what toString is telling you. It is providing diagnostic information that it thinks is useful about the current class, which is, in this case, nothing more then...

"["+getNodeName()+": "+getNodeValue()+"]";

Which isn't going to help you. Instead, you will need to try and transform the model back into a String, for example...

String text
        = "<fruit>"
        + "<banana>yellow</banana>"
        + "<orange>orange</orange>"
        + "<pear>yellow</pear>"
        + "</fruit>";

InputStream is = null;
try {
    is = new ByteArrayInputStream(text.getBytes());
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(is);
    System.out.println("Document=" + document.toString()); // always null

    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.setOutputProperty(OutputKeys.METHOD, "xml");
    tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    ByteArrayOutputStream os = null;
    try {

        os = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(document);
        StreamResult sr = new StreamResult(os);
        tf.transform(domSource, sr);

        System.out.println(new String(os.toByteArray()));

    } finally {
        try {
            os.close();
        } finally {
        }
    }

} catch (ParserConfigurationException | SAXException | IOException | TransformerConfigurationException exp) {
    exp.printStackTrace();
} catch (TransformerException exp) {
    exp.printStackTrace();
} finally {
    try {
        is.close();
    } catch (Exception e) {
    }
}

Which outputs...

Document=[#document: null]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fruit>
    <banana>yellow</banana>
    <orange>orange</orange>
    <pear>yellow</pear>
</fruit>
查看更多
登录 后发表回答