我希望在相当旧的 XML 块中搜索(文档日期为 1999 年),但让 TinyXML2 按预期运行有点困难。我可以抓取某些片段,但是当另一个片段中有一个元素时我会遇到问题。拿这个样本:
<SUBJECT><TITLE>Mathematics</TITLE></SUBJECT>
<AREA><TITLE>Arithmetic</TITLE></AREA>
<SECTION><TITLE>Whole Numbers</TITLE></SECTION>
<TOPIC GRADELEVEL="4"><TITLE>Introduction to Numbers</TITLE></TOPIC>
<DESCRIPTION><TITLE>Description</TITLE></DESCRIPTION>
<FIELDSPACE>
<PARA>To represent each conceivable number by means of a separate
little picture or number symbol is impossible. Therefore the civilizations of
the past all developed a certain pattern whereby they could write down numbers,
by making use of a small number of symbols. </PARA>
</FIELDSPACE>
<FIELDSPACE>
<PARA>Today, we use the Hindu-Arabic system, which first of all is
decimal, because we make use of only 10 different symbols, namely,</PARA>
<LITERALLAYOUT> 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.</LITERALLAYOUT>
</FIELDSPACE>
<FIELDSPACE>
<PARA>Secondly, a place value applies. This means that if only 1
digit is written down then it is that number, such as a 3, a 6, or an 8.</PARA>
</FIELDSPACE>
<FIELDSPACE>
<PARA>Thirdly, only the addition principle is built into our number
symbols.</PARA>
<PARA>In other words,</PARA>
<LITERALLAYOUT> 135 means 100 + 300 + 5</LITERALLAYOUT>
<LITERALLAYOUT> 6.3 means 6 + three tenths = 6 + <EQUATION>
<INLINEGRAPHIC FILEREF="Mathematics/Arithmetic/WholeNumbers/IntroductionNumbers/eq.png" />
</EQUATION></LITERALLAYOUT>
<LITERALLAYOUT> and two and a quarter = <EQUATION>
<INLINEGRAPHIC FILEREF="Mathematics/Arithmetic/WholeNumbers/IntroductionNumbers/eq2.png" />
</EQUATION></LITERALLAYOUT>
<PARA>means</PARA>
<LITERALLAYOUT> two plus a quarter = <EQUATION>
<INLINEGRAPHIC FILEREF="Mathematics/Arithmetic/WholeNumbers/IntroductionNumbers/eq3.png" />
</EQUATION></LITERALLAYOUT>
</FIELDSPACE>
这是我写的:
XMLDocument doc;
Resource::resource_t *f = Resource::Open("IntroductionNumbers.xml"); // File load
if (!f)
return;
doc.Parse((const char*)f->buffer, f->size);
Resource::Close(f);
XMLElement *pElem;
pElem = doc.FirstChildElement();
if (!pElem)
return;
for (pElem = pElem->FirstChildElement(); pElem; pElem = pElem->NextSiblingElement())
{
if (!strcmp(pElem->Value(), "SUBJECT"))
{
// Print what's in pElem->FirstChildElement("TITLE")->GetText()
// This works fine.
}
else if (!strcmp(pElem->Value(), "AREA"))
{
// Print what's in pElem->FirstChildElement("TITLE")->GetText()
// This works fine.
}
...
...
...
else if (!strcmp(pElem->Value(), "TOPIC"))
{
char *temp;
temp = msprintf("%s - Section %s", pElem->FirstChildElement("TITLE")->GetText(), pElem->FirstAttribute()->Value());
// Print what's in temp
// This still works!
}
else if (!strcmp(pElem->Value(), "FIELDSPACE"))
{
// I can print PARA or FIELDSPACE, but I can't seem to read LITERALLAYOUT, EQUATION, or INLINEGRAPHIC.
}
}
我需要通用代码,而不是特定于该解决方案的代码——有数百个 XML 文件,我需要编写一些能够解析所有这些文件的东西。我将如何在 LITERALLAYOUT/EQUATION/INLINEGRAPHIC 中获取信息?
提前致谢!