這幾乎是我所做的第一個C++程序,它應該在文檔中顯示xml節點列表。我使用TinyXML做了完全相同的工作,但是我發現Pugi更好,並且希望繼續使用它。使用PugiXML進行XML解析,無限循環
程序代碼:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "pugixml/src/pugixml.hpp"
#include "pugixml/src/pugiconfig.hpp"
#include "pugixml/src/pugixml.cpp"
using namespace pugi;
const char * identify(xml_node node)
{
const char * type;
switch(node.type())
{
case node_null:
type = "Null";
break;
case node_document:
type = "Document";
break;
case node_element:
type = "Element";
break;
case node_pcdata:
type = "PCDATA";
break;
case node_cdata:
type = "CDATA";
break;
case node_comment:
type = "Comment";
break;
case node_pi:
type = "Pi";
break;
case node_declaration:
type = "Declaration";
break;
case node_doctype:
type = "Doctype";
break;
default:
type = "Invalid";
}
return type;
}
void walk(xml_node parent)
{
printf("%s:\t%s\t%s\n", identify(parent), parent.name(), parent.value());
for(xml_node child = parent.first_child(); child != 0; child = parent.next_sibling())
{
walk(child);
}
}
int main(int argc, char* argv[])
{
for (int i=1; i<argc; i++)
{
xml_document doc;
xml_parse_result result = doc.load_file(argv[i]);
cout << argv[i] << ": " << result.description() << endl;
if (result)
{
walk(doc);
}
}
return 0;
}
示例XML:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<iOne>
<iTwo>
<iThree>
<one>1</one>
<two>2</two>
<three>3</three>
</iThree>
</iTwo>
<one>1</one>
<two>2</two>
<three>3</three>
</iOne>
代碼工作,直到它遇到的第一個兩個<three>
S和進入一個無限循環,這mades我覺得有在for(xml_node child = parent.first_child(); child != 0; child = parent.next_sibling())
中的條件有問題,但所有內容與示例中的相同?我可能錯過了一些非常明顯的東西......這些都是我在C++中的第一個嬰兒步驟:)
我被理解爲C++中的NULL是正確的嗎?
另外(對於提出多個問題抱歉),這是一個真正的做法與pugi的東西嗎?對於一個C++程序,我似乎並沒有使用指針?我很困惑。
是的,這就是我原來的實際情況,它仍然陷入無限循環。 – Dreen 2011-05-18 15:12:59
更新了我的回答 - 我認爲這比我之前提到的更可能。 – Mat 2011-05-18 15:18:47
是的,就是這樣:)謝謝 – Dreen 2011-05-18 15:33:41