如何在iOS中使用libxml
本篇文章简单介绍如何在iOS中使用libxml
Mac OS版本:10.8.2
XCode版本:4.5.1
1. 选择xcode工程设定
2. 选择target
3. 选择Summary
4. 拉到Linked Frameworks and Libraries的地方,按下+按键
输入libxml并选择libxml2,按下Add按键
如此便可在工程中看到libxml2.dylib
如同第一张图中,改选到Build Setting
往下拉找到Search Paths在里面找到Header Search Paths
开启编辑,并按下+然后输入${SDK_ROOT}/usr/include/libxml2
如此一来便可在xcode项目中使用libxml了
#include <libxml2/libxml/parser.h>
#include <libxml2/libxml/tree.h>
便可include libxml
void CBLibXMLUtility::saveWithLibXML(map<string,string>& data,const string& fileName){ // create xml document xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0"); xmlNodePtr root = xmlNewNode(NULL,BAD_CAST"CloudBoxRoot"); //set root xmlDocSetRootElement(doc,root); for(map<string,string>::iterator iter = data.begin(); iter != data.end(); iter++) { cout<<"key:"<<iter->first<<" value:"<<iter->second<<endl; xmlNewTextChild(root, NULL, BAD_CAST (*iter).first.c_str(), BAD_CAST (*iter).second.c_str()); } //save xml int nRel = xmlSaveFile(fileName.c_str(),doc); if (nRel != -1) { cout<<"create a xml:"<<nRel<<"bytes"<<endl; //DebugLog("Create a xml %d bytes\n",nRel); } //release xmlFreeDoc(doc);}
这段代码是一段简单的范例,将map数据结构中的数据保存到xml中。
其他关于libxml的使用请参照我之前的文章"libxml教程"
范例代码下载
- 1楼BeyondVincent4天前 01:14
- 0.0
- Re: cloudhsu4天前 10:07
- 回复BeyondVincentn1.1