找回密码
 立即注册
查看: 2031|回复: 0

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

[复制链接]
发表于 2015-8-30 12:49:20 | 显示全部楼层 |阅读模式
有时,您需要使用xml文件,例如设置一些定制的设置以xml格式文件。来看演示吧!
  1. #include <libxml/parser.h> // include this header file into your .h file

  2. void create_xmldoc()
  3. {
  4.     xmlDocPtr doc;
  5.     xmlNodePtr root_node;

  6.     //new document instance
  7.     doc = xmlNewDoc(BAD_CAST"1.0");
  8.     //set root node
  9.     root_node = xmlNewNode(NULL,BAD_CAST"root");
  10.     xmlDocSetRootElement(doc,root_node);

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

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

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

  25.     //create a son and grandson of the root
  26.     node = xmlNewNode(NULL, BAD_CAST "son");
  27.     xmlAddChild(root_node,node);
  28.     xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "grandson");
  29.     xmlAddChild(node,grandson);
  30.     xmlAddChild(grandson, xmlNewText(BAD_CAST "This is a grandson node"));

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

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

  50.     //memory free
  51.     xmlFreeDoc(doc);
  52. }

  53. void read_xmldoc()
  54. {
  55.     xmlDocPtr doc;
  56.         xmlNodePtr curNode;
  57.         xmlChar *szKey;
  58.         xmlChar *szAttr;
  59.         xmlChar *content;
  60.         xmlChar *attrVal;

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

  67.         //get root element
  68.         curNode = xmlDocGetRootElement(doc);
  69.         if (NULL == curNode)
  70.         {
  71.                 dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Error in xmlDocGetRootElement\n");
  72.         }
  73.         if (xmlStrcmp(curNode->name, BAD_CAST "root"))
  74.         {
  75.            xmlFreeDoc(doc);
  76.         }

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

  85.         szKey = BAD_CAST "son";
  86.         curNode = get_child(curNode, BAD_CAST "son", &content, NULL, NULL);
  87.         dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Get node content and attribute, node: %s, content: %s\n", szKey, content);
  88.         xmlFree(content);
  89.         szKey = BAD_CAST "grandson";
  90.         get_child(curNode, BAD_CAST "grandson", &content, NULL, NULL);
  91.         dlog_print(DLOG_INFO, LOG_TAG, "[read_xmldoc]Get node content and attribute, node: %s, content: %s\n", szKey, content);
  92.         xmlFree(content);

  93.         xmlFreeDoc(doc);
  94. }

  95. static xmlNodePtr
  96. get_child(xmlNodePtr parent, xmlChar* name, xmlChar** content, xmlChar* attr, xmlChar** attrVal)
  97. {
  98.         xmlNodePtr curNode;
  99.         xmlChar *szContent;
  100.         xmlChar* szAttr;

  101.         curNode = parent->xmlChildrenNode;
  102.         while(curNode != NULL)
  103.     {
  104.             // get content of node
  105.             if ( !xmlStrcmp(curNode->name, name) )
  106.             {
  107.                     szContent = xmlNodeGetContent(curNode);
  108.                 *content = szContent;
  109.                 break;
  110.             }
  111.             curNode = curNode->next;
  112.     }

  113.     // get value of attribute if exists
  114.     if ( curNode != NULL &&
  115.              attr != NULL &&
  116.              xmlHasProp(curNode,attr) )
  117.     {
  118.         szAttr = xmlGetProp(curNode,attr);
  119.         *attrVal = szAttr;
  120.     }

  121.     return curNode;
  122. }
复制代码


欢迎来到泰泽网:http://www.tizennet.com/ 泰泽论坛:http://bbs.tizennet.com/ 好没有内涵哦,快到设置中更改这个无聊的签名吧!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|泰泽应用|泰泽论坛|泰泽网|小黑屋|Archiver|手机版|泰泽邮箱|泰泽网 ( 蜀ICP备13024062号-1 )

GMT+8, 2024-4-19 10:36 , Processed in 0.069203 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表