【发布时间】:2019-10-21 09:22:40
【问题描述】:
我想使用 XMLStreamReader 来读取包含水平制表符 ASCII 码 	 的 XML 文件,例如:
<tag>foo&#009;bar</tag>
打印出来或写回另一个xml文件。
Google 告诉我在XMLInputFactory 中将javax.xml.stream.isCoalescing 设置为true,但我下面的测试代码没有按预期工作。
public static void main(String[] args) throws IOException, XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(factory.IS_COALESCING, true);
System.out.println("IS_COALESCING supported ? " + factory.isPropertySupported(factory.IS_COALESCING));
System.out.println("factory IS_COALESCING value is " +factory.getProperty(factory.IS_COALESCING));
String rawString = "<tag>foo	bar</tag>";
XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(rawString));
System.out.println("reader IS_COALESCING value is " +reader.getProperty(factory.IS_COALESCING));
PrintWriter pw = new PrintWriter(System.out, true);
while (reader.hasNext())
{
reader.next();
pw.print(reader.getEventType());
if (reader.hasText())
pw.append(' ').append(reader.getText());
pw.println();
}
}
输出是
IS_COALESCING supported ? true
factory IS_COALESCING value is true
reader IS_COALESCING value is true
1
4 foo bar
2
8
但我想保持相同的水平选项卡,例如:
IS_COALESCING supported ? true
factory IS_COALESCING value is true
reader IS_COALESCING value is true
1
4 foo	bar
2
8
我在这里缺少什么?谢谢
【问题讨论】:
标签: java ascii xmlstreamreader xml-encoding xmlencoder