2010-10-01 115 views
0

我解析的XML文件,該文件是很簡單的:RapidXML XML解析錯誤

<?xml version="1.0" encoding="utf-8"?> 
<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications with XML.</description> 
    </book> 
</catalog> 

當我解析使用它的默認設置,即解析< 0>(),像這樣:

ifstream file(xmlFile.c_str(), ifstream::in); 
int length; 
file.seekg (0, ios::end); 
length = (int)file.tellg(); 
file.seekg (0, ios::beg); 
char xmlBuf[ length + 1 ]; 
file.read(&xmlBuf[0], length); 
xmlBuf[length] = '\0'; 

document.parse<0>(xmlBuf); 

當使用xml_node.value_size()或xml_node.name_size()查詢時,所有節點都位於正確的位置並且具有正確的長度,但實際上將名稱或值轉換爲字符串或甚至只是printf()返回很多如下所示的內容:

(........./.(..............-. <titlK. 

有沒有其他人遇到過這個問題?

回答

2

如果您使用的是C++,爲什麼不用C++的方式呢?

int main(int argc, char* argv[]) 
{ 
    std::string buf; 
    std::string line; 
    std::ifstream in("file.xml"); 

    // read file into buf 
    while(std::getline(in,line)) 
    buf += line; 

    // After that, take a look on the traverse_xml() function implemented 
    // here: http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/ 
    // It has great stuff! 

    return 0; 
}