Re: [xml] Extracting multiple XML fragments of the same type



On Tue, Aug 20, 2002 at 02:52:31PM +0200, Holger Rauch wrote:
Hi!

In my XML file, there are repeating structures that I would like to
extract individually. In the excerpt shown below, these are the fragments
denoted by <Document> elements. I would like to store each of those
<Document>s (and its contents) in a separate xmlDoc. What I got up to now
is:

if ( xmlBuff == NULL ) {
  xmlBuff = xmlBufferCreate();
}
if ( xmlBuff != NULL ) {
  xmlNodeDump( xmlBuff,
             doc,
             curNode,
             0,
             1 );
}

This works nicely for outputting the first of those 3 <Document>s
(since curNode points to the <Document> element. Is there a way to easily
create a new xmlDoc from an existing xmlBuffer? Or is it better to use a
function other than xmlNodeDump for what I am trying to achieve?

  Basically you shouldn't have to serialize and reparse every fragment,
simplest is probably to create each new document using
  doc = xmlNewDoc("1.0");
and then use 
  xmlDocCopyNode(curNode, doc, 1);
to generate a new copy of the tree in the new document.

  If you don't care about the input document and are ready to do the
namespace and doc fixups you may simply prune and paste curNode from
its original document into doc, but doing this safely needs a bit more
work and knowledge of the library tree representation.

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



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