流星悟语 发表于 2015-8-30 12:49:20

Tizen系统LibXML2创建/读取XML文件的代码

有时,您需要使用xml文件,例如设置一些定制的设置以xml格式文件。来看演示吧!
#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, "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, "Error in xmlParseFile\n");
      }

      //get root element
      curNode = xmlDocGetRootElement(doc);
      if (NULL == curNode)
      {
                dlog_print(DLOG_INFO, LOG_TAG, "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, "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, "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, "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;
}

页: [1]
查看完整版本: Tizen系统LibXML2创建/读取XML文件的代码