Re: [xml] Getting element content non-recursively



On Mon, Sep 18, 2006 at 04:45:25PM +0100, Caroline Middlebrook wrote:
Hi all,



Is there a way in which I can get the content of an element that has
children, without getting the content of those children also? For example if
I have something like:



<node1>

    <node2>some text</node2>

<node1>



And I call xmlNodeGetContent on node1, I get "some text" and some blank
characters around it. For this particular example I would want an empty
string as node1 doesn't have any text content itself.

  node->children should be a text node with

        "

      "

   in it, the node->children->next should be the element node node2
and node->children->next->next should contain

                                "

 "

   as per your indentation. All spaces are significant in XML.

This is actually part of a larger problem that I am trying to solve, so
there may be a better way. Basically, for my application, I have little
fragments of XML that I need to add to various elements of other XML
documents. I need to parse these fragments to ensure they are valid so I was
looking at the functions that add children to nodes but I could not find
anything that adds (and parses) a complete fragment of xml. Therefore, what

  There is no notion in XML of parsing "fragment of a document", it's defined
only in terms of parsing a full document.

I do instead is build a new document from my fragment (which catches any
parse errors),

  Parse errors in XML are *fatal*, proces MUST stop there if you find one.

then I iterate around all of the attributes and nodes in a
recursive manner adding them to my target. For each node, I make a new
element with the name and content of the original and here is where I get my
problem - the xmlNodeGetContent function is giving me the concatenated
content of all children but as I am handling recursion myself, I need only
the content of the actual node I am referring to.

  I'm apparently too jet-lag to fully understand what you are doing here,
sorry !

Daniel

-- 
Red Hat Virtualization group http://redhat.com/virtualization/
Daniel Veillard      | virtualization library  http://libvirt.org/
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]