Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要C+的帮助+;函数来解析/显示XML序列化_C++_Xml_Parsing_Xml Parsing_Xml Serialization - Fatal编程技术网

C++ 需要C+的帮助+;函数来解析/显示XML序列化

C++ 需要C+的帮助+;函数来解析/显示XML序列化,c++,xml,parsing,xml-parsing,xml-serialization,C++,Xml,Parsing,Xml Parsing,Xml Serialization,我需要一些帮助使我的XML序列化正常工作。现在我有两个函数可以逐个读取XML文件。现在,他们成功地检测和存储元素,并将元素的内容存储为变量。我让函数打印元素的层次结构,它们成功地显示了它们包含的内容 我的问题是:我无法让函数正确识别元素的结束标记!例如,当解析到达elements end标记时,它会将其检测为一个元素,而不是元素的结尾 对于所有文本和代码长度,我深表歉意。我正努力做到尽可能彻底。提前感谢 这是编译和运行代码时显示的内容。请注意,有些元素不等于零: XML.World.Item.n

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

我的问题是:我无法让函数正确识别元素的结束标记!例如,当解析到达elements end标记时,它会将其检测为一个元素,而不是元素的结尾

对于所有文本和代码长度,我深表歉意。我正努力做到尽可能彻底。提前感谢

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

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>

银钥匙
金属
银
1.
)
10
5.
兽人
兽人
人形
2.
15
o

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

<?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

#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{
公众:
虚拟boolpasselement(std::istream&xmlFile,std::string层次结构$
虚拟boolparsexml(std::istream&xmlFile);
私人:
//没有
};
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;
}
#包括“XMLSerialization.h”
#包括
使用名称空间std;
bool-XMLSerialization::parseElement(istream&xmlFile,字符串层次结构){
char c;//到达时的字符
字符串元素名;
//逐字符读取,检查标记末尾的“>”
做{
c=xmlFile.get();
如果(c!='>'))
元素名称。推回(c);
}
而(c!='>');
string content;//保存元素的非元素内容
while(true){
c=xmlFile.get();
如果(c=''){
c=xmlFile.get();
如果(c!='>')){
结束标记。推回(c);
}
}
if(endTag!=elementName){

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

我推荐Rapidxml。你真的不想自己动手。这个项目的一部分是编写一个自定义XML解析器。不幸的是=/如果你不处理DTD、实体引用和合适的字符,你不能称之为XML解析器er编码检测、处理指令、CDATA节、名称空间……充其量您可以说这是一种“类似XML”的格式:)哈哈,是的。这是一个类似XML的解析函数。也就是说,有什么建议吗?嗯,你的部分问题是
parseElement
在第一个while循环中使用
c
未初始化。你开发的是哪个操作系统?我非常同意Michael和Steven的说法:使用库。这取决于你所针对的操作系统除了各种第三方库之外,还有一些已经可用的东西(例如,在Windows下有MSXML)