Can we customize simple-xml to ignore unknown nodes

When using simple-xml, is there a way to ignore nodes that it does not recognize?

+3
source share
2 answers

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

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

In the corresponding note, you can also process optional elements using @Element(required=false).

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

+3
source

Discliamer: 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>

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

All Articles