简体   繁体   中英

Does libxml2 maintain document order?

IIRC, there are two flavours of XML parsers: DOM, and SAX. SAX is required to parse the XML document from top-to-bottom without any form of reordered (and is event-based), whilst DOM I believe is allowed to reordered.

Is that correct? And would reading the xmlDocPtr returned from xmlReadFile potentially be out of order (I'm assuming libxml2 is either SAX or DOM; it may be its own thing).

For instance:

<!-- original document, in order -->
<xml>
    <element>1</element>
    <element>2</element>
    <element>3</element>
</xml>

<!-- document as it appears in memory (DOM) -->
<xml>
    <element>3</element>
    <element>1</element>
    <element>2</element>
</xml>

libxml2 supports both DOM and SAX parsing. Its DOM parser is actually built on top of its SAX parser. So the DOM output would maintain document order. I have never heard of a DOM parser reordering XML nodes.

whilst DOM I believe is allowed to reordered.

The XML spec says order of XML elements is significant, so any XML API should maintain the order of the elements.

As a side-note, one time ordering ISN'T significant is for ordering of attributes, eg <termdef id="dt-dog" term="dog"/> could come out as <termdef term="dog" id="dt-dog"/>

http://www.w3.org/TR/REC-xml/#sec-starttags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM