java XMLStreamWriter writeCharacters 没有转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2783758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLStreamWriter writeCharacters without escaping
提问by Felix
How do I use XMLStreamWriter to write exactly what I put in? For instance, if I create script tag and fill it with javascript I don't want all my single quotes coming out as '
我如何使用 XMLStreamWriter 准确地写出我输入的内容?例如,如果我创建脚本标签并用 javascript 填充它,我不希望我所有的单引号都显示为 '
Here's a small test I wrote that doesn't use any of the abstractions I've got in place, just calls to writeCharacters.
这是我写的一个小测试,它不使用我现有的任何抽象,只是调用 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());
}
This produces an apos entity for both the single quotes surrounding hello world.
这将为围绕 hello world 的单引号生成一个 apos 实体。
回答by Bodum R Mugg
You could use a property on the factory:
您可以使用工厂的属性:
final XMLOutputFactory streamWriterFactory = XMLOutputFactory.newFactory();
streamWriterFactory.setProperty("escapeCharacters", false);
Then the writer created by this factory will write characters without escaping the text in the element given that the factory supports this property. XMLOutputFactoryImpl does.
如果工厂支持此属性,则此工厂创建的编写器将写入字符而不转义元素中的文本。XMLOutputFactoryImpl 确实如此。
回答by axtavt
XmlStreamWriter.writeCharacters()doesn't escape '. It only escapes <, >and &, and writeAttributealso escapes "(see javadoc).
XmlStreamWriter.writeCharacters()没有逃脱'。它只转义<,>和&,writeAttribute也转义"(参见javadoc)。
However, if you want to write text without escaping at all, you have to write it as a CDATAsection using writeCData().
但是,如果您想在不转义的情况下编写文本,则必须CDATA使用writeCData().
The typical approach for writing scripts in CDATAsections is:
分CDATA节编写脚本的典型方法是:
<script>//<![CDATA[
...
//]]></script>
That is:
那是:
out.writeCharacters("//");
out.writeCData("\n ... \n//");
回答by Dimitar II
Alternative method, with custom escape handler:
带有自定义转义处理程序的替代方法:
XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
xmlFactory.setProperty(XMLOutputFactory2.P_TEXT_ESCAPER, new MyEscapingWriterFactory());
'MyEscapingWriterFactory' is your implementation of 'EscapingWriterFactory' interface. It allows fine grained text escaping control. This is useful when you use text element to deal with random input (say, invalid XML with multiple processing instructions or incorrectly written CDATA sections).
“MyEscapingWriterFactory”是“ EscapingWriterFactory”接口的实现。它允许细粒度的文本转义控制。当您使用文本元素处理随机输入(例如,具有多个处理指令的无效 XML 或错误编写的 CDATA 部分)时,这很有用。
回答by David Thielen
You can also use woodstox's stax implementation. Their XMLStreamWriter2class has a writeRaw() method. We're using it for this specific reason and it works great.
您还可以使用 woodstox 的 stax 实现。它们的XMLStreamWriter2类有一个 writeRaw() 方法。我们出于这个特定原因使用它,并且效果很好。
回答by Roland
Write directly to the underlying Writeror OutputStream:
直接写入底层Writeror OutputStream:
Writer out = new StringWriter();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
... //write your XML
writer.flush();
//write extra characters directly to the underlying writer
out.write("<yourstuff>Test characters</yourstuff>");
out.flush();
... //continue with normal XML
writer.writeEndElement();
writer.flush();

