需要借助C++函数解析/显示XML序列化

Need assistance with C++ function to parse/display XML serialization

本文关键字:显示 XML 序列化 函数 C++      更新时间:2023-10-16

我需要一些帮助才能使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、XMLSerializaation.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");
}

main.cpp

#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;
}

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