RapidXML抛出parse_error异常
RapidXML throwing a parse_error exception
当我尝试使用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, 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::解析
简介
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(' ');
// 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文件中读取一行
其示出了也从节点读取属性的源代码。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- NDK 构建中的异常"error: expected ';' after top level declarator"
- 捕获异常:error: svd()收敛失败
- c++: error:异常处理被禁用,使用-fexceptions来启用
- 为什么当我的lua代码抛出错误时,我无法捕获luabind::error异常?
- 提升序列化/反序列化存档"stream error"异常