1. 程式人生 > >RapidXml 讀取檔案並遍歷節點的所有屬性

RapidXml 讀取檔案並遍歷節點的所有屬性

RapidXml 試圖成為最快的 XML DOM 解析工具包,同時保證解析結果的可用性、可移植性以及與 W3C 標準的相容性。RapidXml 使用 C++ 編寫,因此在操作同一資料時,其解析速度接近於 strlen() 函式。 整個解析工具包包含在一個頭檔案中,所以使用時不用編譯也不用連線。要想使用 RapidXml 只要包含 rapidxml.hpp 即可,當然如果要用附加功能(如列印函式),你可以包含 rapidxml_print.hpp 檔案。 RapidXml 為採用C++語言操作XML提供了機遇,同時結合XMPP協議也開啟了諸如Wt、CxServer等基於C++的網路應用在即時通訊領域的更寬廣的發展空間。

測試示例:

讀取xml檔案,並遍歷所有節點的所有屬性。xml的檔案內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<EllipsoidParams>
  <Datum Name="BeiJing54" SemiMajorAxis="6378245.0" Flattening="298.3"/>
  <Datum Name="XiAn80" SemiMajorAxis="6378140.0" Flattening="298.257"/>
  <Datum Name="CGCS2000" SemiMajorAxis="6378137.0" Flattening="298.257222101"/>
  <Datum Name="WGS-84" SemiMajorAxis="6378137.0" Flattening="298.257223563"/>
  <Datum Name="WGS-72" SemiMajorAxis="6378135.0" Flattening="298.26"/>
  <Datum Name="WGS-66" SemiMajorAxis="6378145.0" Flattening="298.25"/>
  <Datum Name="WGS-60" SemiMajorAxis="6378165.0" Flattening="298.3"/>
  <Datum Name="Krassovsky" SemiMajorAxis="6378245.0" Flattening="298.3"/>
  <Datum Name="International" SemiMajorAxis="6378388.0" Flattening="297.0"/>
  <Datum Name="MERIT-83" SemiMajorAxis="6378137.0" Flattening="298.257"/>
  <Datum Name="SGS85" SemiMajorAxis="6378136.0" Flattening="298.257"/>
  <Datum Name="GRS80" SemiMajorAxis="6378137.0" Flattening="298.257222101"/>
  <Datum Name="GRS75" SemiMajorAxis="6378140.0" Flattening="298.257"/>
  <Datum Name="Airy 1830" SemiMajorAxis="6377563.396" Flattening="299.3249753150"/>
  <Datum Name="Modified Airy" SemiMajorAxis="6377340.189" Flattening="299.32493736548"/>
  <Datum Name="Aust_SA" SemiMajorAxis="6378160.0" Flattening="298.25"/>
  <Datum Name="Bessel 1841" SemiMajorAxis="6377397.155" Flattening="299.15281285"/>
  <Datum Name="Clarke 1880" SemiMajorAxis="6378249.145" Flattening="293.4663"/>
  <Datum Name="Clarke 1866" SemiMajorAxis="6378206.4" Flattening="294.9786982139"/>
  <Datum Name="GRS67" SemiMajorAxis="6378160.0" Flattening="298.247167427"/>
  <Datum Name="APL4.9" SemiMajorAxis="6378137.0" Flattening="298.25"/>
  <Datum Name="Andrae" SemiMajorAxis="6377104.43" Flattening="300.0"/>
  <Datum Name="Bessel_nam" SemiMajorAxis="6377483.865" Flattening="299.1528128"/>
  <Datum Name="CPM" SemiMajorAxis="6375738.7" Flattening="334.29"/>
  <Datum Name="Delamber" SemiMajorAxis="6376428.0" Flattening="311.5"/>
  <Datum Name="Engelis" SemiMajorAxis="6378136.05" Flattening="298.2566"/>
  <Datum Name="Everest30" SemiMajorAxis="6377276.345" Flattening="300.8017"/>
  <Datum Name="Everest48" SemiMajorAxis="6377304.063" Flattening="300.8017"/>
  <Datum Name="Everest56" SemiMajorAxis="6377301.243" Flattening="300.8017"/>
  <Datum Name="Everest69" SemiMajorAxis="6377295.664" Flattening="300.8017"/>
  <Datum Name="EverestSS" SemiMajorAxis="6377298.556" Flattening="300.8017"/>
  <Datum Name="Fischer60" SemiMajorAxis="6378166.0" Flattening="298.3"/>
  <Datum Name="Fischer60m" SemiMajorAxis="6378155.0" Flattening="298.3"/>
  <Datum Name="Fischer68" SemiMajorAxis="6378150.0" Flattening="298.3"/>
  <Datum Name="Helmert" SemiMajorAxis="6378200.0" Flattening="298.3"/>
  <Datum Name="Hough" SemiMajorAxis="6378270.0" Flattening="297.0"/>
  <Datum Name="IAU76" SemiMajorAxis="6378140.0" Flattening="298.257"/>
  <Datum Name="Kaula" SemiMajorAxis="6378163.0" Flattening="298.24"/>
  <Datum Name="Lerch" SemiMajorAxis="6378139.0" Flattening="298.257"/>
  <Datum Name="Maupertius" SemiMajorAxis="6397300.0" Flattening="191.0"/>
  <Datum Name="New_intl" SemiMajorAxis="6378157.5" Flattening="298.24961539"/>
  <Datum Name="NWL9D" SemiMajorAxis="6378145.0" Flattening="298.25"/>
  <Datum Name="Plessis" SemiMajorAxis="6376523.0" Flattening="0.0"/>
  <Datum Name="Southeast_asia" SemiMajorAxis="6378155.0" Flattening="298.3000002409"/>
  <Datum Name="Sphere" SemiMajorAxis="6370997.0" Flattening="298.257"/>
  <Datum Name="Walbeck" SemiMajorAxis="6376896.0" Flattening="302.7800001817"/>
</EllipsoidParams>

測試程式程式碼:
#include <tchar.h>
#include <string>
#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"//rapidxml::file
#include "rapidxml/rapidxml_print.hpp"//rapidxml::print
using namespace rapidxml;


std::string ReadFile(const char* fileName)
{
	std::string strBuffer;
	FILE *fp = fopen(fileName, "rb");
	if(NULL == fp)
	{
		return "";
	}

	char buf[1024];	
	while(true)
	{
		const size_t byteRead = fread(buf, 1, sizeof(buf), fp);
		if(0 == byteRead)
		{
			break;
		}
		strBuffer.insert(strBuffer.end(), buf, buf+byteRead);
	}
	fclose(fp);

	return strBuffer;
}
//
int ReadFileAndTraversal()
{
	//讀取檔案內容
	std::string szFileBuf = ReadFile("./Ellipsoid.xml");
	
	//使用rapidxml::file讀取檔案更方便
	rapidxml::file<> fdoc("Ellipsoid.xml");
	
	//列印讀取的內容
	std::cout << fdoc.data() << std::endl;

	rapidxml::xml_document<> doc;
	doc.parse<0>(fdoc.data());
	//doc.parse<0>(szFileBuf.c_str());

	//列印整個XML內容
	std::cout << doc.name() << std::endl;

	//在XML文件中尋找第一個節點
	const rapidxml::xml_node<> *ellipsoid = doc.first_node("EllipsoidParams");
	if(NULL == ellipsoid)
	{
		return -1;
	}

	//遍歷所有橢球資訊
	for(rapidxml::xml_node<> *datum = ellipsoid->first_node("Datum");
		NULL != datum;
		datum = datum->next_sibling())
	{
		//遍歷單個橢球的所有屬性
		std::string szTmp("");
		for(rapidxml::xml_attribute<char> * attr = datum->first_attribute("Name");
			attr != NULL;
			attr = attr->next_attribute())
		{
			szTmp.append(attr->name());//name() value()返回的字串不會去掉首尾的空白字元
			szTmp.append(": ");
			szTmp.append(attr->value());
			szTmp.append(", ");
		}
		std::cout << szTmp.c_str() << std::endl;
	}

	return 0;
}

//
int _tmain(int argc, _TCHAR* argv[])
{	
	//讀取檔案並列印整個檔案
	ReadFileAndTraversal();

	system("pause");

	return 0;
}