开发者

parsing for xml values

开发者 https://www.devze.com 2023-04-09 22:08 出处:网络
I have a simple xml string defined in the following way in a c code: char xmlstr[] = \"<root><str1>W开发者_JS百科elcome</str1><str2>to</str2><str3>wonderland</s

I have a simple xml string defined in the following way in a c code:

char xmlstr[] = "<root><str1>W开发者_JS百科elcome</str1><str2>to</str2><str3>wonderland</str3></root>";

I want to parse the xmlstr to fetch all the values assigned to str1,str2,str3 tags.

I am using libxml2 library. As I am less experienced in xml handling, I unable get the values of the required tags. I tried some sources from net, but I am ending wrong outputs.


Using the libxml2 library parsing your string would look something like this:

char xmlstr[] = ...;
char *str1, *str2, *str3;
xmlDocPtr doc = xmlReadDoc(BAD_CAST xmlstr, "http://someurl", NULL, 0);
xmlNodePtr root, child;

if(!doc)
{ /* error */ }

root = xmlDocGetRootElement(doc);

now that we have parsed a DOM structure out of your xml string, we can extract the values by iterating over all child values of your root tag:

for(child = root->children; child != NULL; child = child->next)
{
    if(xmlStrcmp(child->name, BAD_CAST "str1") == 0)
    {
        str1 = (char *)xmlNodeGetContent(child);
    }

    /* repeat for str2 and str3 */
    ...
}


I usual do xml parsing using minixml library

u hope this will help you

http://www.minixml.org/documentation.php/basics.html

0

精彩评论

暂无评论...
验证码 换一张
取 消