简体   繁体   中英

Clone libxml2 document

What's the best way to clone the parsed in-memory representation of an XML document in libxml2?

By “clone” I mean a deep clone, an independent data structure which represents the same data as the original. By “best way” I mean a reasonable compromise between implementation complexity and performance overhead. So for example, serializing and re-parsing would be few lines of code but incur unneccessary parsing overhead. On the other hand, manually cloning every member of every data structure could likely lead to hundreds of lines of code due to all the case distinctions.

My best bet at the moment would seem to be the xmlDOMWrapCloneNode function . But on the one hand, its intended purpose is cloning a single node, so extra care would be neccessary to construct a compatible document and use that to receive the clone. On the other hand, some of the complexity in there might be avoidable if a whole document has to be cloned. Looking at the code illustrates the complexity involved here, though. Copying and adapting all that code seems like way too much work.

You're probably looking for xmlCopyDoc :

 xmlDocPtr xmlCopyDoc (xmlDocPtr doc, int recursive) 

Do a copy of the document info. If recursive, the content tree will be copied too as well as DTD, namespaces and entities.

doc: the document
recursive: if not zero do a recursive copy.
Returns: a new #xmlDocPtr, or NULL in case of error.

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