0

我需要一些帮助才能使我的 XML 序列化正常工作。现在我有两个函数可以逐个字符地读取我的 XML 文件。现在他们成功地检测和存储元素以及将元素的内容存储为变量。我有函数打印元素的层次结构,它们成功地显示了它们包含的内容。

我的问题:我无法获得正确识别元​​素结束标签的功能!例如,当解析到元素结束标记时,它会将其检测为元素而不是元素的结尾。

对不起所有的文字和代码的长度。我试图尽可能彻底。先谢谢了!

这是编译和运行代码时显示的内容。注意 hot 有一些元素不等于:

XML.World.Item.name = silver key
XML.World.Item.properties.property = metal
XML.World.Item.properties.property = silver
XML.World.Item.properties = 
XML.World.Item.weight = 1
XML.World.Item.displayChar = )
XML.World.Item.value = 10
XML.World.Item.rarity = 5
XML.World.Item = 
XML.World.Creature.name = orc
XML.World.Creature.properties.property = orcish
XML.World.Creature.properties.property = humanoid
XML.World.Creature.properties = 
XML.World.Creature.level = 2
XML.World.Creature.maxHP = 15
XML.World.Creature.displayChar = o
XML.World.Creature = 
XML.World = 

我的 XML 文件名为 world.xml,这是它包含的 XML 代码:

<?xml version="1.0" encoding="UTF-8"?>
<World>
<Item>
<name>silver key</name>
<properties>
<property>metal</property>
<property>silver</property>
</properties>
<weight>1</weight>
<displayChar>)</displayChar>
<value>10</value>
<rarity>5</rarity>
</Item>
<Creature>
<name>orc</name>
<properties>
<property>orcish</property>
<property>humanoid</property>
</properties>
<level>2</level>
<maxHP>15</maxHP>
<displayChar>o</displayChar>
</Creature>
</World>

这是我正在使用的代码 - XMLSerialization.h、XMLSerialization.cpp 和 main.cpp:

XMLSerialization.h

#include <iostream>
#include <string>
#include <fstream>

class XMLSerialization {

public:
    virtual bool parseElement(std::istream & xmlFile, std::string hierarchy$
    virtual bool parseXML(std::istream & xmlFile);

private:
    //none
};

XMLSerialization.cpp

#include "XMLSerialization.h"
#include <string>

using namespace std;
bool XMLSerialization::parseElement(istream & xmlFile, string hierarchy){
    char c; // the character as we reach it
    string elementName;

    //reads char by char, checking for '>' at the end of the tag

    do {
        c = xmlFile.get();
        if (c != '>')
            elementName.push_back(c);
    }
    while (c != '>');

    string content; //holds the non-element content of the element

    while (true){
            c = xmlFile.get();
            if (c == '<'){
                    if (xmlFile.peek() == '/'){
                            xmlFile.get();
                            string endTag; //holds the end tag as its read

                            while(c != '>'){
                                    c = xmlFile.get();
                                    if (c != '>'){
                                            endTag.push_back(c);
                                    }
                            }

                            if (endTag != elementName){
                                cout<<"Tag name mismatch! "<<endTag<<
                                            " differs from "<<elementName
                                            <<"."<<endl;
                                    return false;
                            }

                            //output what is known. Where we are in the
                            //file, current element, and its content
                            cout<<hierarchy<<"."<<elementName<<" = "<<
                                    content<<endl;
                            return true;
                    }
                    else {
                            //read in '<' and was NOT an end tag. c is at
                            //the first char after < so function calls
                            //on itself again. Passing hierarchy and current
                            //element name so next element knows where it
                            //is in the xmlFile.
                            if (!parseElement(xmlFile, hierarchy + "." + el$
                                    return false;
                            }
                    }
            }

            else {
                    //c is not '<' so its content. Also ignores EOL
                    if (c != '\n'){
                                content.push_back(c);
                    }
            }
    }

    return true;
}



// checks for a valid XML Header
bool XMLSerialization::parseXML(istream & xmlFile){
    char c; // char to hold the character as we reach it

    //get character while the character != '<'
    do {
            c = xmlFile.get();
    }
    while (c != '<');

    //checks the character after the '<'
    if (xmlFile.get() != '?'){
            cout<<"Invalid XML Header! Does not begin with '<?'"<<endl;
            return false;
    }
   //continues through header look for '?'
    do {
            c = xmlFile.get();
    }
    while (c != '?');

    // checks for the header ending with ?>
    if (xmlFile.get() != '>'){
            cout<<"Invalid XML Header! Does not end with '?>'"<<endl;
            return false;
    }

    // go through character until the first tag after the header
    do {
            c = xmlFile.get();
    }
    while (c != '<');

    // at the first character after the opening '<' of the tag
    // call parseElement
    return parseElement(xmlFile, "XML");
}

主文件

#include "XMLSerialization.h"
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char * argv[]){
    cout<<"________________________"<<endl;
    cout<<"XML TESTING"<<endl<<"________________________"<<endl<<endl;
    ifstream xmlFile;
    xmlFile.open("world.xml");
    XMLSerialization test;
    test.parseXML(xmlFile);
    xmlFile.close();
    return 0;
}
4

1 回答 1

2

出于对所有神圣事物的热爱,请使用适当的 XML 解析器。你会很高兴你做到了。我强烈推荐libxml2

于 2013-02-07T00:08:14.780 回答