使用Boost解析XML属性

gro*_*999 17 c++ xml boost boost-propertytree

我想与您分享我在尝试使用Boost库(版本1.52.0)处理C++中XML元素的某些属性时遇到的问题.给出以下代码:

#define ATTR_SET ".<xmlattr>"
#define XML_PATH1 "./pets.xml"

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;
using namespace boost;
using namespace boost::property_tree;

const ptree& empty_ptree(){
    static ptree t;
    return t;
}

int main() {
    ptree tree;
    read_xml(XML_PATH1, tree);
    const ptree & formats = tree.get_child("pets", empty_ptree());
    BOOST_FOREACH(const ptree::value_type & f, formats){
        string at = f.first + ATTR_SET;
        const ptree & attributes = formats.get_child(at, empty_ptree());
        cout << "Extracting attributes from " << at << ":" << endl;
        BOOST_FOREACH(const ptree::value_type &v, attributes){
            cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我有以下XML结构:

<?xml version="1.0" encoding="utf-8"?>
<pets>
    <cat name="Garfield" weight="4Kg">
        <somestuff/>
    </cat>
    <dog name="Milu" weight="7Kg">
        <somestuff/>
    </dog>
    <bird name="Tweety" weight="0.1Kg">
        <somestuff/>
    </bird>
</pets>
Run Code Online (Sandbox Code Playgroud)

因此,我将得到的控制台输出将是下一个:

Extracting attributes from cat.<xmlattr>:
First: name Second: Garfield
First: weight Second: 4Kg
Extracting attributes from dog.<xmlattr>:
First: name Second: Milu
First: weight Second: 7Kg
Extracting attributes from bird.<xmlattr>:
First: name Second: Tweety
First: weight Second: 0.1Kg
Run Code Online (Sandbox Code Playgroud)

但是,如果我决定对从根节点放置的每个元素使用公共结构(为了从它们的特定属性中识别它们),结果将完全改变.在这种情况下,这可能是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<pets>
    <pet type="cat" name="Garfield" weight="4Kg">
        <somestuff/>
    </pet>
    <pet type="dog" name="Milu" weight="7Kg">
        <somestuff/>
    </pet>
    <pet type="bird" name="Tweety" weight="0.1Kg">
        <somestuff/>
    </pet>
</pets>
Run Code Online (Sandbox Code Playgroud)

输出将如下:

Extracting attributes from pet.<xmlattr>:
First: type Second: cat
First: name Second: Garfield
First: weight Second: 4Kg
Extracting attributes from pet.<xmlattr>:
First: type Second: cat
First: name Second: Garfield
First: weight Second: 4Kg
Extracting attributes from pet.<xmlattr>:
First: type Second: cat
First: name Second: Garfield
First: weight Second: 4Kg
Run Code Online (Sandbox Code Playgroud)

由于已经打印了三组属性,因此可以正确识别从根节点悬挂的元素数量.然而,所有这些都是指第一个元素的属性......

我不是C++方面的专家,也不是Boost的新手,所以这可能是我在哈希映射处理方面所缺少的东西......任何建议都会非常感激.

小智 15

您的程序问题位于以下行:

const ptree & attributes = formats.get_child(at, empty_ptree());
Run Code Online (Sandbox Code Playgroud)

有了这条线,你就要求让孩子pet.<xmlattr>离开pets,你可以独立于f你所经历的3次这样做.在这篇文章之后,我猜你需要使用的是:

const ptree & attributes = f.second.get_child("<xmlattr>", empty_ptree());
Run Code Online (Sandbox Code Playgroud)

适用于您的xml文件的完整代码是:

#define ATTR_SET ".<xmlattr>"
#define XML_PATH1 "./pets.xml"

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;
using namespace boost;
using namespace boost::property_tree;

const ptree& empty_ptree(){
    static ptree t;
    return t;
}

int main() {
    ptree tree;
    read_xml(XML_PATH1, tree);
    const ptree & formats = tree.get_child("pets", empty_ptree());
    BOOST_FOREACH(const ptree::value_type & f, formats){
        string at = f.first + ATTR_SET;
        const ptree & attributes = f.second.get_child("<xmlattr>", empty_ptree());
        cout << "Extracting attributes from " << at << ":" << endl;
        BOOST_FOREACH(const ptree::value_type &v, attributes){
            cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)