【问题标题】:xml and percent symbolsxml 和百分比符号
【发布时间】: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();
...

【问题讨论】:

    标签: java xml stax


    【解决方案1】:

    我想通了。问题出在代码的不同部分。 setter 不仅仅是 set

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多