尝试解析此有效XML文件时,为什么TinyXML2的XMLDocument :: FirstChild()函数返回NULL?

时间:2018-09-27 18:46:16

标签: c++ xml tinyxml2

我知道这里可能缺少一些非常明显的东西,但这已经让我发疯了几天。

这是我编写的一个简单函数,现在应该可以在XML文档中找到第一个标签:

void parse_weather_file(const char* weatherFileName, WeatherDataAsPWMValues *wd)
{
    if (wd == NULL) return;

    std::cout << weatherFileName << std::endl;

    tinyxml2::XMLDocument doc(weatherFileName);
    tinyxml2::XMLNode *root = doc.FirstChild();
    if (root == NULL) std::cout << "Error" << std::endl;
}

这是我要解析的XML文档:

<!-- Sample output of openWeatherMap API -->
<?xml version="1.0" encoding="utf-8"?>
<current>
    <city id="2643741" name="City of London">
        <coord lon="-0.09" lat="51.51">
        <country>GB</country>
    <sun rise="2015-06-30T03:46:57" set="2015-06-30T20:21:12">
    </city>
    <temperature value="72.34" min="66.2" max="79.88" unit="fahrenheit"/>
    <humidity value="43" unit="%">
    <pressure value="1020" unit="hPa">
    <wind>
        <speed value="7.78" name="Moderate breeze">
        <direction value="140" code="SE" name="SouthEast">
    </wind>
    <clouds value="0" name="clear sky">
    <visibility value="10000">
    <precipitation mode="no">
    <weather number="800" value="Sky is Clear" icon="01d">
    <lastupdate value="2015-06-30T08:36:14">
</current>

这是我从函数中获得的输出:

test.xml
Error

这表明root在函数末尾为NULL,但是我不确定为什么会这样。

2 个答案:

答案 0 :(得分:3)

如@ zx485所述,您的XML格式错误。但是,即使修复了该错误,您的代码仍将失败。对于TinyXML-2 documentationIntent intent = new Intent(context, NotificationActionService.class); pendingIntent = PendingIntent.getService(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder builder = new NotificationCompat.Action.Builder(iconId, actionTitle, pendingIntent); notificationBuilder.addAction(builder.build()); ... 类不具有接受文件名作为输入的构造函数。但是,它确实有一个接受PackageManager pkgMngr = ctx.getPackageManager(); Intent intent = packageManager.getLaunchIntentForPackage(ctx.getPackageName()); pendingIntent = PendingIntent.**getActivity**(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT); 作为输入的构造函数:

tinyxml2::XMLDocument

bool可以隐式转换为XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE ); ,这就是为什么您的代码可以编译,但是实际上您根本没有加载文件,这就是const char*为NULL的原因。 / p>

您需要改为调用类的bool方法:

root

例如:

LoadFile()

此外,您应该使用XMLError LoadFile( const char* filename ); 方法而不是void parse_weather_file(const char* weatherFileName, WeatherDataAsPWMValues *wd) { if (wd == NULL) return; std::cout << weatherFileName << std::endl; tinyxml2::XMLDocument doc; XMLError err = doc.LoadFile(weatherFileName); if (err != XML_SUCCESS) { std::cout << "Error loading file: " << (int)err << std::endl; /* or: std::cout << "Error loading file: " << doc.ErrorName() << std::endl; std::cout << "Error loading file: " << tinyxml2::XMLDocument::ErrorIDToName(err) << std::endl; std::cout << "Error loading file: " << doc.ErrorStr() << std::endl; std::cout << "Error loading file" <<< endl; doc.PrintError(); */ return; } tinyxml2::XMLNode *root = doc.FirstChild(); if (root == NULL) { std::cout << "Error root is null" << std::endl; return; } // use root as needed... } 方法:

RootElement()

答案 1 :(得分:0)

通过在单个元素上添加结束符/,使您的XML格式良好。
看起来是这样:

<?xml version="1.0" encoding="utf-8"?>
<current>
    <city id="2643741" name="City of London">
        <coord lon="-0.09" lat="51.51" />
        <country>GB</country>
        <sun rise="2015-06-30T03:46:57" set="2015-06-30T20:21:12" />
    </city>
    <temperature value="72.34" min="66.2" max="79.88" unit="fahrenheit" />
    <humidity value="43" unit="%" />
    <pressure value="1020" unit="hPa" />
    <wind>
        <speed value="7.78" name="Moderate breeze" />
        <direction value="140" code="SE" name="SouthEast" />
    </wind>
    <clouds value="0" name="clear sky" />
    <visibility value="10000" />
    <precipitation mode="no" />
    <weather number="800" value="Sky is Clear" icon="01d" />
    <lastupdate value="2015-06-30T08:36:14" />
</current>

希望这可以帮助您识别错误...