-1

I am very new to XML stuff, so i have to ask for help. I have a xml file like this:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<main>
<string name="firstname">John2</string>
<string name="secondname">Doe</string>
<string name="Street">4th</string>
<string name="City">NY</string>
</main>

I have libxml2 and i need to get the firstnameand the lastname values. There are many examples, but i could not find how to get a node value by its name.

Andrei Golubev
  • 283
  • 1
  • 2
  • 15

2 Answers2

0

You can make use of

void getReference (xmlDocPtr doc, xmlNodePtr cur) {

    xmlChar *uri;
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
           1 uri = xmlGetProp(cur, "uri");
            printf("uri: %s\n", uri);
            xmlFree(uri);
        }
        cur = cur->next;
    }
    return;
}

Where cur would be your node "main" and the child attributes will come from using xmlGetProp function

V31
  • 7,626
  • 3
  • 26
  • 44
0

I don't know who minuses my question and why, but i have found the answer myself.

while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"string"))) {
        uri = xmlGetProp(cur, "name");
        if ( ( !xmlStrcmp ( uri, ( const xmlChar * ) "firstname" ) ) ){
            key = xmlNodeListGetString ( doc, cur -> xmlChildrenNode,1);
            printf("string: %s\n", uri);
            printf ( "firstname: %s\n", key);
        }
       .... so on with the lastname
    }
    cur = cur->next;
}
Andrei Golubev
  • 283
  • 1
  • 2
  • 15