[xml] Exchanging same XML fragment in multiple XML documents


I would like to exchange the same XML fragment in multiple XML
documents. These documents (each of them represented as an xmlDocPtr) are
available to me as elements of an array, so I would loop through all
documents, trying to find the node whose name equals that of the first
element in the new fragment, and finally replace the old fragment with the
new one using xmlReplaceNode().

How can I search for a node having a certain name *without* making a
particular assumption on the document structure? I know there are the
"children", "parent", "next", and "prev" links, but using these I would
have to assume that

a) my document structure remains always the same or
b) change my code each time my document structure changes

So, what's the way around this problem?

I am aware that there's a function named
xmlParseBalancedChunkMemory() that can be used to turn the textual
representation of the new fragment into a node list (xmlNodePtr). However,
the first parameter of this function is an xmlDocPtr, so it seems I would
actually have to use this function inside the loop so that
xmlParseBalancedChunkMemory() always "knows" about the current
document. However, this seems inefficient to me since it shouldn't be
necessary to reparse an XML fragment that doesn't change simply for the
reason that this fragment later "knows" to which document it is supposed
to pertain to.

Isn't it possible to parse the new fragment only once and only change the
document the fragment should be part of in the loop (for performance
reasons)? If so, how can I do this?

(I was looking at both tree.c and parser.c and the HTML documentation, I
searched xmlsoft.org using search.php and found a thread "Pasting
Fragment" where Daniel mentioned xmlParseBalancedChunkMemory())

Any help will be greatly appreciated!



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]