【发布时间】:2013-09-19 16:51:27
【问题描述】:
我正在使用XMLStreamReader解析一段xml:
XMLStreamReader rd = XMLInputFactory.newInstance().createXMLStreamReader(io_xml, "UTF-8");
...
if (eventType == XMLStreamConstants.START_ELEMENT) {
String name = rd.getLocalName();
if (name.equals("key")) {
String val = rd.getElementText();
}
}
问题是,我读错了以下字符串:"<key>cami%C3%B5es%2Babc</key>"
org.junit.ComparisonFailure:
expected:<cami[%C3%B5es%]2Babc> but was:<cami[ C3 B5es ]2Babc>
我需要在 XML 中做一些特别的事情吗?我已经尝试将所有内容都放在 CDATA 部分中,但我得到了同样的错误。
使用“常规”解析器一切正常:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document parse = builder.parse(is);
String value = parse.getFirstChild().getTextContent();
...
【问题讨论】: