1

RapidXML フレームワークを使用して単純な .xml ファイルを解析しようとすると、「expected <」という原因で parse_error がスローされます。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>
4

2 に答える 2

3

メソッドはparseXML を含む文字列を受け取り、ファイル名を渡します。ファイル名は 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());            
     }  
于 2011-06-13T16:45:04.103 に答える
3

私がいつも参照している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 ファイルから行を読み取る

これは、ノードからもプロパティを読み取るソース コードを示しています。

于 2011-06-13T16:46:43.887 に答える