有互聯網上對此多篇。假設你是在Windows找到下面的例子供大家參考: -
<?xml version="1.0" encoding="UTF-8"?>
<Car>
<Wheels>
<Wheel1>FL</Wheel1>
<Wheel2>FR</Wheel2>
<Wheel3>RL</Wheel3>
<Wheel4>RR</Wheel4>
</Wheels>
</Car>
下面是代碼: -
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))
int main(int argc, char* argv[])
{
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
try
{
MSXML::IXMLDOMDocument2Ptr xmlDoc;
hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60), NULL, CLSCTX_INPROC_SERVER);
// TODO: if (FAILED(hr))...
if (xmlDoc->load(_T("input.xml")) != VARIANT_TRUE)
{
printf("Unable to load input.xml\n");
}
else
{
printf("XML was successfully loaded\n");
xmlDoc->setProperty("SelectionLanguage", "XPath");
MSXML::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*");
printf("Car has %u wheels\n", wheels->Getlength());
MSXML::IXMLDOMNodePtr node;
node = xmlDoc->createNode(MSXML::NODE_ELEMENT, _T("Engine"), _T(""));
node->text = _T("Engine 1.0");
xmlDoc->documentElement->appendChild(node);
hr = xmlDoc->save(_T("output.xml"));
if (SUCCEEDED(hr))
printf("output.xml successfully saved\n");
}
}
catch (_com_error &e)
{
printf("ERROR: %ws\n", e.ErrorMessage());
}
CoUninitialize();
}
return 0;
}
您還可以參考以下鏈接: -
http://codeproject.com/Articles/587488/Streaming-XML-parser-in-Cplusplus
http://msdn.microsoft.com/en-us/library/ms765540(v=vs.85).aspx
與此相反if你是在Linux平臺上,然後去TinyXml的: -
http://grinninglizard.com/tinyxml/
http://pugixml.org/ – Galik 2014-11-02 13:54:43
升壓屬性樹可以用於解析XML。 http://www.boost.org/doc/libs/1_56_0/doc/html/property_tree.html - 我認爲它只是標題。 – 2014-11-02 14:00:33
_「我真的更喜歡小型圖書館(比如一個.h文件和一個.c文件)」_你可能在這裏有一個很大的誤解。正確解析XML是一項複雜得多的任務,可以合理解決單個模塊中的問題。 – 2014-11-02 14:01:57