我们可以配置simple-xml使其忽略未知节点吗

use*_*949 5 java xml-deserialization simple-framework

使用simple-xml时,有没有办法让它忽略无法识别的节点?

Mic*_*yle 5

是的。如果您用它注释您的类,@Root(strict=false)则会忽略任何未映射的元素。请参阅文档了解更多详细信息:

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#loosemap

相关说明,您还可以使用 处理可选元素@Element(required=false)

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#可选


Yan*_*eve 0

免责声明:如果 simple-xml 意味着除简单 XML以外的任何内容,那么以下答案是无关紧要的

首先,查看: http: //www.w3.org/TR/xmlschema-1/#element-any

允许此类任何元素的示例模式是:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Root">
        <xs:complexType>
            <xs:all>
                <xs:element name="Element">
                <xs:complexType>
                    <xs:sequence minOccurs="0">
                        <xs:any processContents="lax" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

验证上述内容的示例 xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<Root xsi:noNamespaceSchemaLocation="Any.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Element>
    <Root>
        <Element><Node1><SubElement/></Node1><Node2/></Element>
    </Root>
    </Element>
</Root>
Run Code Online (Sandbox Code Playgroud)