Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can libxml2 be used to parse data from XML?

I have looked around at the libxml2 code samples and I am confused on how to piece them all together.

What are the steps needed when using libxml2 to just parse or extract data from an XML file?

I would like to get hold of, and possibly store information for, certain attributes. How is this done?

like image 340
some_id Avatar asked Mar 28 '11 22:03

some_id


2 Answers

I believe you first need to create a Parse tree. Maybe this article can help, look through the section which says How to Parse a Tree with Libxml2.

like image 50
Sadique Avatar answered Sep 21 '22 05:09

Sadique


libxml2 provides various examples showing basic usage.

http://xmlsoft.org/examples/index.html

For your stated goals, tree1.c would probably be most relevant.

tree1.c: Navigates a tree to print element names

Parse a file to a tree, use xmlDocGetRootElement() to get the root element, then walk the document and print all the element name in document order.

http://xmlsoft.org/examples/tree1.c

Once you have an xmlNode struct for an element, the "properties" member is a linked list of attributes. Each xmlAttr object has a "name" and "children" object (which are the name/value for that attribute, respectively), and a "next" member which points to the next attribute (or null for the last one).

http://xmlsoft.org/html/libxml-tree.html#xmlNode

http://xmlsoft.org/html/libxml-tree.html#xmlAttr

like image 30
Jason Viers Avatar answered Sep 18 '22 05:09

Jason Viers