1

从 Java1.2 和 Apache Xerces DomParser 升级到 Java1.7 和 Xerces JAXP DocumentBuilder 后,尽管使用“setCoalescing(true);”初始化 DocumentBuilderFactory,但升级后的解析完成且没有错误,但不会“解包”CDATA 元素。</p>

也就是说,输入的 XML 元素如原样<ITEMDESC><![CDATA[ Sales Bom Material,Dist]]></ITEMDESC>返回。

代码如下所示。

我是 XML 解析的新手,所以我很可能遗漏了一些非常基本的东西。

我们的输入 XML 实际上有数百个不同的标签,因此我们需要一个无需更改每个元素“get”即可工作的解决方案。

获得“setCoalescing(true);”是否还有其他要求/提示/提示/技巧?去工作 ?

在此先感谢您的任何建议。

代码:

        DocumentBuilderFactory aDocBuilderFactory = DocumentBuilderFactory.newInstance();
        aDocBuilderFactory.setValidating(m_dtdValidate);

        // Set to make sure that CDATA elements are automatically converted and collected into a single text element
        aDocBuilderFactory.setCoalescing(true);

        // Make sure that entity references are expanded, this includes the replacements for the reserved markup
        // characters
        aDocBuilderFactory.setExpandEntityReferences(true);

        // Ignore comments as they won't contain information to be processed
        aDocBuilderFactory.setIgnoringComments(true);

        // Get a document builder
        m_documentBuilder = aDocBuilderFactory.newDocumentBuilder();

        // Install entity resolver if required
        m_documentBuilder.setEntityResolver(new DocumentEntityResolver());

        m_document = m_documentBuilder.parse(pSource);
4

0 回答 0