tags:

views:

29

answers:

1

I'm using libxml2 and C++. The following function crashes here at return (char*)cur->content;. When I change it to return (char*)cur->name; then it will return attribute which is the name of the tag. What I want is 1, 2, and 3 (based on the XML file below the C++ code). What am I doing wrong?

char* xml2sdf::getId(xmlNode* part){

    xmlNode* cur = part->xmlChildrenNode;

    // get the id
    while (cur != NULL) {

        if ( !xmlStrcmp(cur->name, (const xmlChar *)"attribute") ) {
            xmlAttrPtr attr = cur->properties;

            if( !xmlStrcmp( attr->children->content, (const xmlChar*)"id" ) ){
                return (char*)cur->content;
            }
        }

        cur = cur->next;
        }

    }
}

The part of the XML file it is parsing:

<part ref="part10" name="part10">
    <attribute name="face">7</attribute>
    <attribute name="id">1</attribute>
</part>

<part ref="part20" name="part20">
    <attribute name="face">5</attribute>
    <attribute name="id">2</attribute>
</part>

<part ref="part30" name="part30">
    <attribute name="face">9</attribute>
    <attribute name="id">3</attribute>
</part>
A: 

I discovered it should be return (char*)cur->children->content; by trial and error.

alex
The content of a node is a child of that node, that is why you have to access children first. You might also consider using the xmlNodeGetContent function instead of directly accessing the structure. That is, "return (char*)xmlNodeGetContent( cur->children );".
Ishmael