如何使用XMLStreamWriter準確地寫入我放入的內容?例如,如果我創建script標籤並用javascript填充它,我不希望所有的單引號出現爲&;XMLStreamWriter在不轉義的情況下寫入字符
這是我寫的一個小測試,它不使用任何抽象概念,只是調用writeCharacters。
public void testWriteCharacters() {
StringWriter sw = new StringWriter();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
StringBuffer out = new StringBuffer();
try {
XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
writer.writeStartElement("script");
writer.writeAttribute("type","text/javascript");
writer.writeCharacters("function hw(){ \n"+
"\t alert('hello world');\n" +
"}\n");
writer.writeEndElement();
out.append(sw);
} catch (XMLStreamException e) {
} finally {
try {
sw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
System.out.println(out.toString());
}
這產生了一個包含hello world的單引號的apos實體。
可能的重複[如何將未轉義的XML寫入XMLStreamWriter?](http://stackoverflow.com/questions/19998460/how-to-write-unescaped-xml-to-xmlstreamwriter) – Roland 2016-07-18 08:41:11