RapidXML引发parse_error异常

问题描述

| 当我尝试使用RapidXML框架解析一个简单的.xml文件时,它会引发parse_error,其原因如下:\“ expected <\”。 现在这实际上是我第一次编写XML代码,因此这可能是一个愚蠢的语法错误在这种情况下,请多多包涵:) 这是我的xmlParser.h:
#ifndef __XML_PARSER_H__
#define __XML_PARSER_H__

#include \"rapidxml.hpp\"
#include \"windowUtil.h\"

class XmlParser
{
public:
    bool parse(char *xml)
    {
        try
        {
            doc.parse<0>(xml);
        }
        catch(rapidxml::parse_error &e)
        {
            msg_Box(NULL,e.what(),\"RapidXML exception!\",MB_OK | MB_ICONERROR | MB_TASKMODAL);

            return false;
        }

        return true;
    }

    char* get_first_node_name()
    {
        return doc.first_node()->name();
    }
private:
    rapidxml::xml_document<> doc;
};

#endif
这就是它的调用和使用方式:
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
    XmlParser xmlParser;
    WindowFramework *window = create_window(&framework,NULL,\"GAME\");

    if(!init_window(window,true,true))
        return kill(1);
    if(!xmlParser.parse(\"./layouts/login_gui.xml\"))
        return kill(1);

    framework.main_loop();

    return kill(0);
}
login_gui.xml:
<?xml version=\"1.0\"?>
<button>
    <text>EXIT</text>
    <buttonready>button.png</buttonready>
    <buttonrollover>button_active.png</buttonrollover>
    <buttonpressed>button_pressed.png</buttonpressed>
    <buttoninactive>button_inactive.png</buttoninactive>
</button>
    

解决方法

        
parse
方法采用包含XML的字符串,您正在向其传递文件名。您的文件名被视为XML数据,显然这是不正确的。您必须先读入文件,然后使用结果字符串调用parse。 从RapidXML文档中:   函数xml_document :: parse      概要
void parse(Ch *text); 
  描述      解析以零结尾的XML   字符串根据给定的标志。 您修改后的结构可能类似于
bool parse(char *xmlFile)        
{            
     try            
     {  
        std::string xml(getXmlDataFromFile(xmlFile));
        doc.parse<0>(xml.c_str());            
     }  
    ,        我经常引用有关使用RapidXML的精美文档。这是一本必读的书! 这是我尝试读取文档(demo.xml)的第一个节点的尝试。
string input_xml;
string line;
ifstream in(\"demo.xml\");

// read file into input_xml
while(getline(in,line))
    input_xml += line;

// make a safe-to-modify copy of input_xml
// (you should never modify the contents of an std::string directly)
vector<char> xml_copy(input_xml.begin(),input_xml.end());
xml_copy.push_back(\'\\0\');

// only use xml_copy from here on!
xml_document<> doc;
// we are choosing to parse the XML declaration
// parse_no_data_nodes prevents RapidXML from using the somewhat surprising
// behavior of having both values and data nodes,and having data nodes take
// precedence over values when printing
// >>> note that this will skip parsing of CDATA nodes <<<
doc.parse<parse_declaration_node | parse_no_data_nodes>(&xml_copy[0]);

// we didn\'t keep track of our previous traversal,so let\'s start again
// we can match nodes by name,skipping the xml declaration entirely
xml_node<>* cur_node = doc.first_node(\"button\");

// go straight to the first text node
cur_node = cur_node->first_node(\"text\");
string text = cur_node->value(); // if the node doesn\'t exist,this line will crash
cout << text << endl;

// and then to the next node
cur_node = cur_node->next_sibling(\"buttonready\");
string b_ready = cur_node->value();
cout << b_ready << endl;

// and then to the next node
// ...
输出:
EXIT
button.png
如果将来您的XML变得更加复杂,则可以查看以下答案: 使用C ++从xml文件读取一行 其中显示了源代码,该源代码也从节点读取属性。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...