0

I parsed and have pointer xmlNodePtr upto category tag, But I want to get the value of the node(name) like TrailersFreeMovies , Trailers in an array.

<categories><category><name>TrailersFreeMovies</name><url>https://www.ex1.com/srs/index.php?cid=47</url></category><category><name>Trailers</name><url>https://www.ex1.com/srs/index.php?cid=45</url></category></categories>

guide me to parse this

Shanmu G
  • 120
  • 1
  • 11
  • libxml2 can be confusing to a newcomer. I would honestly recommend using a simpler library if performance isn't of the essence. – Richard J. Ross III Sep 27 '12 at 16:23
  • I'd recommend looking at the libxml2 examples, specifically `tree1.c`, which shows how to navigate a document and get contents. http://xmlsoft.org/examples/index.html#tree1.c – Jason Viers Sep 27 '12 at 21:54

1 Answers1

0

XPath part of the api returns an array of nodes. See XPath examples.

Once you obtain the result of xmlXPathEvalExpression as xpathObj then the array is in xpathObj->nodesetval->nodeTab. nodesetval is a pointer to xmlNodeSet type.

xmlGetNodePath returns following values for the nodes of your sample xml matching the //name xpath expression:

/categories/category[1]/name
/categories/category[2]/name

So a specific answer to your question would be: apply xpath expression ("%s/name", xmlGetNodePath(categoryNode)) and process returned array of nodes. For each entry get the text with xmlNodeListGetString(doc, node->xmlChildrenNode, 1).

Jarekczek
  • 7,456
  • 3
  • 46
  • 66