خرید بک لینک

Vote count: 0

Lets say we have an xml like so:

<item>
   <![CDATA[abc]]>
</item>

I have a method convertStringToDocument that converts this String xmlString into a Document (org.w3c.dom.Document). In order to digitally sign the xml.

public static Document convertStringToDocument(String xmlString) {
    try {
        DocumentBuilder db = generateDocumentBuilder();
        Document document = db.parse(new InputSource(new StringReader(xmlString)));
        retu document;
    } 
    catch ...
    }
    retu null;
}

public static DocumentBuilder generateDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(false);
        dbf.setIgnoringComments(false);
        dbf.setFeature("http://xml.org/sax/features/exteal-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/exteal-parameter-entities", false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        retu db;
    } 
    catch ...
    }
    retu null;
}

However, when converting this Document back into a string, the <![CDATA[ ... ]]> tag is lost. The same this occurs for html encoded characters like &#8211;. Below is how Document objects are converted back to string:

public static String convertDocumentToString(Document doc, boolean omitXmlDeclaration) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.transform(new DOMSource(doc), new StreamResult(sw));
        retu sw.toString();
    } 
    catch ...
    }
    retu null;
}

Is it possible to convert the String xmlString into a Document and then back into a String without losing <![CDATA[...]]> tags or HTML encoding?

asked 32 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 1 مرداد 1395 ساعت: 23:57

صفحه بندی