Article View: cn.bbs.comp.xml
Article #326libxml2����XMLʹ��ʵ��(ת��)
From: <Test/>
Date: Sun, 14 Mar 2010 09:27
Date: Sun, 14 Mar 2010 09:27
176 lines
5864 bytes
5864 bytes
libxml2
1 ����ָ�룺 �ĵ�ָ��(xmlDocPtr)�����ָ��(xmlNodePtr)��
2 �õ��ĵ�doc: xmlReadFile
3 �õ�����root_node�� xmlDocGetRootElement
4 ��������
1)��õ����ֵ�� xmlNodeGetContent(��Ӧ��xmlFree)
2)����
ָ����һ����㣺 xmlNodePtr ->children
���ֵ�� xmlNodePtr->name,
����ڱ��� xmlNodePtr->next
5 �ͷ��ڴ棺 xmlFreeDoc��xmlFree
������ܵķ���Ϊʹ��Dom�����������������ʵ��˵�����ʹ��Libxml2����xml�ĵ���ʹXpath��ȡ�ض���������ֵ��
����ʹ�õ�xml�ĵ�Ϊ:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1>content of node 1</node1>
<node3 attribute="yes">node has attributes</node3>
<node4>other way to create content</node>
</root>
���������룺
#include <iostream>
#include <libxml/parser.h>
#include <libxml/tree.h>
using namespace std;
int main(int argc,char** argv)
{
xmlDocPtr doc=NULL;
xmlNodePtr cur=NULL;
char* name=NULL;
char* value=NULL;
xmlKeepBlanksDefault (0);
if(argc<2)
{
cout<<"argc must be 2 or above."<<endl;
return -1;
}
doc=xmlParseFile(argv[1]);//����Dom��
if(doc==NULL)
{
cout<<"Loading xml file failed."<<endl;
exit(1);
}
cur=xmlDocGetRootElement(doc);//��ȡ��ڵ�
if(cur==NULL)
{
cout<<"empty file"<<endl;
xmlFreeDoc(doc);
exit(2);
}
//walk the tree
cur=cur->xmlChildrenNode;//get sub node
while(cur !=NULL)
{
name=(char*)(cur->name);
value=(char*)xmlNodeGetContent(cur);
cout<<"name is: "<<name<<", value is: "<<value<<endl;
xmlFree(value);
cur=cur->next;
}
xmlFreeDoc(doc);//�ͷ�xml������������Դ
xmlCleanupParser();
return 0;
}
˵����
1. ��ʹ��dom��������xml�ĵ�ʱ������Ĭ�ϵķ�ʽ�ǰѽڵ��Ŀհ�����һ���ӽ� �㣬����Ϊ���ܺͳ�˵�ĵ�һ���ӽڵ���������xmlKeepBlanksDefault (0)�������������ֿհס�
2. ����ʹ��xmlChar* xmlNodeGetContent(xmlNodePtr cur)�����ȡ�ڵ����ݺ������xmlFree()�����������ڴ�����ͷš�
ʹ��Xpath��ȡ�ض��������ݣ�
#include <iostream>
#include <string>
using namespace std;
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
int main(int argc,char** argv)
{
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodeset;
string xpathExpr;
char* val=NULL;
int size,i;
if(argc<2)
{
cout<<"argc must be 2 or above."<<endl;
return -1;
}
doc = xmlParseFile(argv[1]);
if (doc == NULL)
{
cout<<"Error: unable to parse file: "<<argv[1]<<endl;
return -1;
}
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL)
{
cout<<"Error: unable to create new XPath context"<<endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -2;
}
xpathExpr="/root/node3";
xpathObj = xmlXPathexpression_r_r((const xmlChar*)(xpathExpr.c_str()), xpathCtx);
if(xpathObj == NULL)
{
cout<<"Error: unable to uate xpath expression_r_r"<<xpathExpr<<endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -3;
}
nodeset=xpathObj->nodesetval;
if(xmlXPathNodeSetIsEmpty(nodeset))
{
cout<<"No such nodes."<<endl;
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -4;
}
//get the value
size = (nodeset) ? nodeset->nodeNr : 0;
for(i = 0; i <size; i++)
{
val=(char*)xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
cout<<"the results are: "<<val<<endl;
xmlFree(val);
}
//Cleanup of XPath data
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
����ʹ�õĹ���У����˴���û��ʹ��xmlFree(val)��Ҳû��ʹ��xmlCleanupParser()����Ȼ��û��ʹ��xmlKeepBlanksDefault (0)�������ʹ����valgrind����λ�ڴ�й¶����!
��ʾ��xmlCleanupParser()��Ҫ�������Σ�����ͻ����coredump;
# gdb -c core.18659
.. ...
Program terminated with signal 6, Aborted.
.. ...
(gdb) bt
#0 0x0000003aeec30265 in ?? ()
#1 0x0000003aeec31d10 in ?? ()
.. ...
btҲ�������κ����õ���Ϣ����ʵֻ���ڴ���ظ��ͷ���ɣ�
----------------------------------------------------------------------------
ת�ԣ�http://hi.baidu.com/goggle1/blog/item/5a381ef2cca48dc00a46e0d5.html
--
[1;36mHappy XMLing
[m[1;35m�� ��Դ:��ˮľ���� newsmth.net��[FROM: 114.249.217.*][m
Technical Details
Message-ID:
<4kkTbE$IOY@newsmth.net>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!weretis.net!feeder2.news.weretis.net!feeds.phibee-telecom.net!news.byr.edu.cn!news.neu.edu.cn!news.newsmth.net!NEWSMTHShow Full Headers
Complete Article Headers
X-Google-Thread: 115571,2090cb1c3c63acd7,start X-Google-NewGroupId: yes X-Google-Attributes: gidf7ebf7e2af,domainid0,public,usenet X-Google-Language: Chinese,GB Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!weretis.net!feeder2.news.weretis.net!feeds.phibee-telecom.net!news.byr.edu.cn!news.neu.edu.cn!news.newsmth.net!NEWSMTH From: thinkhy@newsmth.net-SPAM.no (<Test/>) Newsgroups: cn.bbs.comp.xml Subject: libxml2����XMLʹ��ʵ��(ת��) Date: 14 Mar 2010 09:27:52 GMT Organization: ˮľ���� Message-ID: <4kkTbE$IOY@newsmth.net> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: XML/q/M.1268558846.y0