Alex
Alex

Reputation: 1102

How do I get the content with libxml2?

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>

Upvotes: 4

Views: 9494

Answers (1)

Alex
Alex

Reputation: 1102

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

Upvotes: 8

Related Questions