Re: [xml] dump node (not recursive)



David wrote:
Hello,

I want to dump a node (with its properties and content) but xmlNodeDump doesn't exactly do what i want.

the xml file is like
-------------------------
<xml>
 <node1 name="hello">
  <node1_2>content</node1_2>
 </node1>
</xml>
---------------------

when the current node is node1 i want to get the result <node1 name="hello">

when the current node is node1_2 i want to get the result <node1_2>content</node1_2>



with the function xmlNodeDump (and node is node1) i have the result
 <node1 name="hello">
  <node1_2>content</node1_2>
 </node1>
This is to much in-depth infomation for me


I hope there is a default function for this.

Cheerz, Corley

But...the contents of the node1 element is everything between <node1> and </node1>, so that's what you would expect the dump routine to show.

You can write your own routine that just displays the element name and all of its attributes. That's not too hard.

Here is a method that I use to visit all of the attributes of an element and make a callback for each of them. Just ignore the callback related stuff and look at how it iterates over the attributes. For each of them, it extracts the name and value and puts them into the params structure that it passes to the callback. You could do a printf in the inner loop using pAttr->name and pChild->content, if that's all you're after.
        //----------------------------------------------------------------------------
void
XmlLibraryAdapter::elementNodeAttributeVisitor (
    xmlNodePtr pNode,
    XmlLibraryAdapterAttributeVisitorCallback_T *pCallback,
    void *pPrivate) const
{
    xmlAttr *pAttr = NULL;
    XmlLibraryAdapterAttributeVisitorCallbackParams_T params;
    params.pPrivate = pPrivate;
    for (pAttr = pNode->properties; pAttr; pAttr = pAttr->next)
    {
        params.attributeName = (const char *)pAttr->name;
        if (pAttr->ns)
        {   // Attribute is in a namespace, so we need the name
            // string to contain the namespace prefix.
            assert (pAttr->ns->prefix);
            std::string attrName ((const char *)pAttr->ns->prefix);
            attrName += ":";
            attrName += (const char *)pAttr->name;
            params.attributeName = attrName.c_str();
        }
                
        xmlNode *pChild = pAttr->children;
        assert (pChild);
        params.attributeValue = (const char *)pChild->content;
                        
        (*pCallback) (&params);
    }
}

- Rush





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