为什么它不能正确解析 XML 文件?
Why it's not parsing correctly the XML file?
我想从 xml 文件中获取文件名,但它似乎没有存储有关该文件的任何信息。
用于存储文件(或之后的文件)名称的结构:
struct Document{
std::string file1;
std::string file2;
std::string file3;
std::string file4;
}Doc;
从 xml 文件中获取元素:
static std::string getElementText(tinyxml2::XMLElement *_element) {
std::string value;
if (_element != NULL) {
value = _element->GetText();
}
return value;
}
正在解析 xml 文件:
void parseXml(char* file) {
tinyxml2::XMLDocument doc;
doc.LoadFile(file);
printf("Stuff\n");
if (doc.ErrorID() == 0) {
tinyxml2::XMLElement *pRoot;
pRoot = doc.FirstChildElement("scene");
Document * thisDoc = new Document();
while (pRoot) {
printf("Another Stuff\n");
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = Doc.file1.c_str();
printf("%s\n", file1);
printf("Stuff2\n");
pRoot = pRoot->NextSiblingElement("scene");
}
}
}
XML 文件是:
<scene>
<model>plane.txt</model>
<model>cone.txt</model>
<model>box.txt</model>
<model>sphere.txt</model>
</scene>
我测试时得到的输出:
我认为您将自己与所有称为 'doc' 某事或其他的各种变量混淆了。
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = Doc.file1.c_str();
显然应该是这个
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = thisDoc->file1.c_str();
还有这个
struct Document{
std::string file1;
std::string file2;
std::string file3;
std::string file4;
}Doc;
应该是这个
struct Document {
std::string file1;
std::string file2;
std::string file3;
std::string file4;
};
除非你真的想声明一个名为 Doc
的全局变量。如果你这样做了,那就太糟糕了。
选择好的变量名很重要,确实如此。
我想从 xml 文件中获取文件名,但它似乎没有存储有关该文件的任何信息。
用于存储文件(或之后的文件)名称的结构:
struct Document{
std::string file1;
std::string file2;
std::string file3;
std::string file4;
}Doc;
从 xml 文件中获取元素:
static std::string getElementText(tinyxml2::XMLElement *_element) {
std::string value;
if (_element != NULL) {
value = _element->GetText();
}
return value;
}
正在解析 xml 文件:
void parseXml(char* file) {
tinyxml2::XMLDocument doc;
doc.LoadFile(file);
printf("Stuff\n");
if (doc.ErrorID() == 0) {
tinyxml2::XMLElement *pRoot;
pRoot = doc.FirstChildElement("scene");
Document * thisDoc = new Document();
while (pRoot) {
printf("Another Stuff\n");
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = Doc.file1.c_str();
printf("%s\n", file1);
printf("Stuff2\n");
pRoot = pRoot->NextSiblingElement("scene");
}
}
}
XML 文件是:
<scene>
<model>plane.txt</model>
<model>cone.txt</model>
<model>box.txt</model>
<model>sphere.txt</model>
</scene>
我测试时得到的输出:
我认为您将自己与所有称为 'doc' 某事或其他的各种变量混淆了。
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = Doc.file1.c_str();
显然应该是这个
thisDoc->file1 = getElementText(pRoot- >FirstChildElement("model"));
const char *file1 = thisDoc->file1.c_str();
还有这个
struct Document{
std::string file1;
std::string file2;
std::string file3;
std::string file4;
}Doc;
应该是这个
struct Document {
std::string file1;
std::string file2;
std::string file3;
std::string file4;
};
除非你真的想声明一个名为 Doc
的全局变量。如果你这样做了,那就太糟糕了。
选择好的变量名很重要,确实如此。