1

示例 xml

<root>
  <first>
    <second><![CDATA[hello, world]]></second>
  </first>
<root>

我正在使用 xstream(1.4.7)、stax2-api(3.1.4)、woodstox(5.0.3)。

@Test
    public void xmlInputFactoryTest() throws XMLStreamException, IOException {

        ClassPathResource resource = new ClassPathResource("/sample/input.xml");

        NameCoder nameCoder = new XmlFriendlyNameCoder();

        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader reader = factory.createXMLEventReader(resource.getInputStream());
        XMLStreamReader streamReader = StaxUtils.createEventStreamReader(reader);
        HierarchicalStreamReader hsr = new StaxReader(new QNameMap(), streamReader, nameCoder);
        write(hsr);
    }

    public void write(HierarchicalStreamReader hsr) {
        System.out.println(hsr.getNodeName() + ", " + hsr.getValue()); // should be print "hello,world" in second tag
        if (hsr.hasMoreChildren()) {
            hsr.moveDown();
            write(hsr);
        }
    }

这是我的示例测试代码

我无法在路径“root/first/second”中获取 cdata 字符串结果

我认为 XMLInputFactory.newInstance() 返回类对结果的影响。

当我添加

factory.setProperty(XMLInputFactory.IS_COALESCING, true);

获取 XMLInputFactory.newInstance() 之后的这段代码。

有用。但这似乎不是答案。

.

版本不兼容?

有没有其他可能的方法?

4

0 回答 0