我有要求序列化包含像ンᅧᅭ%ンᅨ&
這樣的字符串的文檔的要求。雖然序列化它拋出以下異常:序列化時處理無效的xml字符
java.io.IOException: The character '' is an invalid XML character
有作爲與任何解決辦法,我們可以序列化此字符串的方法嗎?
StringWriter stringOut = new StringWriter();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
String xml = "ンᅧᅭ%ンᅨ&";
//String xml = "ンᅧᅭ%ンᅨ&";
Element junk = doc.createElement("replyToQ");
junk.appendChild(doc.createCDATASection(xml));
//junk.appendChild(doc.createTextNode(stripNonValidXMLCharacters(xml)));
rootElement.appendChild(junk);
//org.w3c.dom.Document doc = this.toDOM();
//Serialize DOM
OutputFormat format = new OutputFormat(doc,"UTF-8",true);
format.setIndenting(false);
format.setLineSeparator("");
format.setPreserveSpace(true);
format.setOmitXMLDeclaration(false);
XMLSerializer serial = new XMLSerializer(stringOut, format);
// As a DOM Serializer
serial.asDOMSerializer();
serial.serialize(doc.getDocumentElement());
這是哪種語言? –
您可以嘗試使用不同的編碼而不是UTF-8。這裏有一個列表,您可以嘗試:http://en.wikipedia.org/wiki/ISO/IEC_2022#ISO.2FIEC_2022_character_sets。 – splrs