Create/Read XML file by LibXML2

Sometimes, you need to work with the xml file, such as to set some customized settings in an XML format file.
#include <libxml/parser.h> // include this header file into your .h file

void create_xmldoc()
{
    xmlDocPtr doc;
    xmlNodePtr root_node;

    //new document instance
    doc = xmlNewDoc(BAD_CAST"1.0");
    //set root node
    root_node = xmlNewNode(NULL,BAD_CAST"root");
    xmlDocSetRootElement(doc,root_node);

    //create a comment and cdata section
    xmlNodePtr cdata = xmlNewCDataBlock(doc,BAD_CAST "This is a CData block",xmlStrlen(BAD_CAST "This is a CData block"));
    xmlAddChild(root_node,cdata);
    xmlNodePtr comment = xmlNewComment(BAD_CAST "This is a comment node");
    xmlAddChild(root_node,comment);

    //create sub-node under the root node
    xmlNewChild(root_node, NULL, BAD_CAST "newNode1", BAD_CAST "newNode1 content");
    xmlNewTextChild(root_node, NULL, BAD_CAST "newNode2", BAD_CAST "newNode2 content");

    //create a node with content and attribute, add to root
    xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");
    xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");
    xmlAddChild(root_node,node);
    xmlAddChild(node,content);
    xmlNewProp(node,BAD_CAST"attribute",BAD_CAST "yes");

    //create a son and grandson of the root
    node = xmlNewNode(NULL, BAD_CAST "son");
    xmlAddChild(root_node,node);
    xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "grandson");
    xmlAddChild(node,grandson);
    xmlAddChild(grandson, xmlNewText(BAD_CAST "This is a grandson node"));

    //create a node with namespace
    node = xmlNewNode(NULL,BAD_CAST "newNode3");
    xmlAddChild(root_node,node);
    xmlNsPtr ns = xmlNewNs(node,BAD_CAST "http://org.tizen.xmldemo/ns/newNode3",BAD_CAST "cns");
    xmlNodePtr subnode = xmlNewChild(node,ns,BAD_CAST "subNode1",BAD_CAST "subNode1 content");
    xmlNewNsProp(subnode,ns,BAD_CAST "subNode1Attr",BAD_CAST "subNode1 attribute");
    subnode = xmlNewNode(ns,BAD_CAST "subNode2");
    xmlAddChild(node,subnode);
    xmlNewNsProp(subnode,ns,BAD_CAST "dataType",BAD_CAST "datetime");
    xmlNewTextChild(subnode,ns,BAD_CAST "date",BAD_CAST "2015-06-10");

    //save the document
    int nRel = xmlSaveFile(XML_FILE_NAME,doc);
    if (nRel != -1)
    {
    	xmlChar *docstr;
    	int len;
    	xmlDocDumpMemory(doc, &docstr, &len);
    	dlog_print(DLOG_INFO, LOG_TAG, "[create_xmldoc]File length: %d, file content:\n%s", len, docstr);
    }

    //memory free
    xmlFreeDoc(doc);
}

void read_xmldoc()
{
    xmlDocPtr doc;
	xmlNodePtr curNode;
	xmlChar *szKey;
	xmlChar *szAttr;
	xmlChar *content;
	xmlChar *attrVal;

	// parsing the xml file and return the doc handler(xmlDocPtr)
	doc = xmlParseFile(XML_FILE_NAME);
	if (NULL == doc)
	{
		dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Error in xmlParseFile\n");
	}

	//get root element
	curNode = xmlDocGetRootElement(doc);
	if (NULL == curNode)
	{
		dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Error in xmlDocGetRootElement\n");
	}
	if (xmlStrcmp(curNode->name, BAD_CAST "root"))
	{
	   xmlFreeDoc(doc);
	}

	// search children by node name
	szKey = BAD_CAST "node2";
	szAttr = BAD_CAST "attribute";
	get_child(curNode, szKey, &content, szAttr, &attrVal);
	dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Get node content and attribute, node: %s, content: %s; attribute: %s, attribute value: %s\n",
			szKey, content, szAttr, attrVal);
	xmlFree(content);
	xmlFree(attrVal);

	szKey = BAD_CAST "son";
	curNode = get_child(curNode, BAD_CAST "son", &content, NULL, NULL);
	dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Get node content and attribute, node: %s, content: %s\n", szKey, content);
	xmlFree(content);
	szKey = BAD_CAST "grandson";
	get_child(curNode, BAD_CAST "grandson", &content, NULL, NULL);
	dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Get node content and attribute, node: %s, content: %s\n", szKey, content);
	xmlFree(content);

	xmlFreeDoc(doc);
}

static xmlNodePtr
get_child(xmlNodePtr parent, xmlChar* name, xmlChar** content, xmlChar* attr, xmlChar** attrVal)
{
	xmlNodePtr curNode;
	xmlChar *szContent;
	xmlChar* szAttr;

	curNode = parent->xmlChildrenNode;
	while(curNode != NULL)
    {
	    // get content of node
	    if ( !xmlStrcmp(curNode->name, name) )
	    {
	    	szContent = xmlNodeGetContent(curNode);
	        *content = szContent;
	        break;
	    }
	    curNode = curNode->next;
    }

    // get value of attribute if exists
    if ( curNode != NULL &&
    	 attr != NULL &&
    	 xmlHasProp(curNode,attr) )
    {
        szAttr = xmlGetProp(curNode,attr);
        *attrVal = szAttr;
    }

    return curNode;
}

Responses

0 Replies