[libxml2]_[C/C++]_[libxml 2を使用して分析xmlファイルを読み込む]
2844 ワード
シーン:
1.xmlファイルはほとんどの場合、構成用に使用されますが、windowsのmsxmlライブラリはWindowsが持参したライブラリではありません.それを使用するにはパッケージ化され、プラットフォームから独立したライブラリを学ぶのに時間がかかります.
2.libxml 2はプラットフォームにまたがるxmlライブラリであり、expatと同様にMacOSXはデフォルトでサポートする.
3.libxml 2を用いるxmlファイルを読み取る例を以下に示すが、pathパスはutf 8符号化しなければ中国語パスを認識できないことに注意する.
4.libxml 2のvcコンパイルについてlibxml 2-2.7を見る.1win 32のReadmeファイル
test_libxml2.cpp
Table.xml
出力:
1.xmlファイルはほとんどの場合、構成用に使用されますが、windowsのmsxmlライブラリはWindowsが持参したライブラリではありません.それを使用するにはパッケージ化され、プラットフォームから独立したライブラリを学ぶのに時間がかかります.
2.libxml 2はプラットフォームにまたがるxmlライブラリであり、expatと同様にMacOSXはデフォルトでサポートする.
3.libxml 2を用いるxmlファイルを読み取る例を以下に示すが、pathパスはutf 8符号化しなければ中国語パスを認識できないことに注意する.
4.libxml 2のvcコンパイルについてlibxml 2-2.7を見る.1win 32のReadmeファイル
test_libxml2.cpp
#include "stdafx.h"
#include <iostream>
#include "gtest/gtest.h"
#include "libxml/tree.h"
#include "libxml/parser.h"
#include "libxml/xpath.h"
#include "libxml/xpathInternals.h"
static void Parse(std::string path)
{
xmlDocPtr doc = xmlParseFile(path.c_str());
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST "/setting/url", xpathCtx);
xmlNodeSetPtr vendors = xpathObj->nodesetval;
for(int i = 0; i < vendors->nodeNr; ++i)
{
xmlNodePtr vendor = vendors->nodeTab[i];
xmlNodePtr children = vendor->children;
while(children)
{
//xmllib Text node
if(!xmlNodeIsText(children))
{
const char* name = (const char*)children->name;
xmlChar* content1 = xmlNodeGetContent(children);
char* content = (char*)content1;
std::cout << "name: " << name << ":" << " content1:" << content1 << std::endl;
xmlFree(content1);
}
children = children->next;
}
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
}
TEST(test_libxml2,test)
{
Parse("..\\Debug\\Table.xml");
}
Table.xml
<?xml version="1.0" encoding="UTF-8"?>
<setting>
<url>
<help>http://www.xxxx.com/online-help/56/</help>
<purchase>http://www.xxxx.com/purchase/56.html</purchase>
<support>http://www.xxxx.com/support.html</support>
<brand>http://www.xxxx.com</brand>
<update>http://www.xxxx.com/w</update>
<product>http://www.xxxx.com/56.html</product>
</url>
</setting>
出力:
Note: Google Test filter = test_libxml2.test
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from test_libxml2
[ RUN ] test_libxml2.test
name: help: content1:http://www.xxxx.com/online-help/56/
name: purchase: content1:http://www.xxxx.com/purchase/56.html
name: support: content1:http://www.xxxx.com/support.html
name: brand: content1:http://www.xxxx.com
name: update: content1:http://www.xxxx.com/w
name: product: content1:http://www.xxxx.com/56.html
[ OK ] test_libxml2.test (2978 ms)