开发者

Can we configure simple-xml to let it ignore unknown nodes

W开发者_StackOverflowhen using simple-xml, is there a way to let it ignore the nodes it does not recognize?


Yes. If you annotate your class with @Root(strict=false) it will ignore any elements that are not mapped. See the documentation for additional details:

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

On a related note, you can also handle optional elements using @Element(required=false).

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


Discliamer: If simple-xml means anything but a simple XML then the following answer is irrelevant

First, look at: http://www.w3.org/TR/xmlschema-1/#element-any

An example schema allowing such any elements is:

<?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>

And an example xml validating to the above is:

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜