使用boost xml解析 [英] xml parsing using boost

查看:886
本文介绍了使用boost xml解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BOOST下面的解析xml文件库 -

I am parsing below xml file using BOOST library-

<da>
        <m_day Type="int">15</m_day>
        <m_month Type="int">8</m_month>
        <m_year Type="int">1947</m_year>
</da>

我的CPP code是:

My cpp code is :

    #include <boost/archive/xml_oarchive.hpp> 
    #include <boost/archive/xml_iarchive.hpp>
    #include <iostream> 
    #include <fstream> 

    typedef struct date { 
        unsigned int m_day;
        unsigned int m_month;
        unsigned int m_year;
        date( int d,  int m,  int y) : m_day(d), m_month(m), m_year(y) 
        {}
        date() : m_day(1), m_month(1), m_year(2000) 
        {}
        friend std::ostream& operator << (std::ostream& out, date& d) 
        {
            out << "day: " << d.m_day 
                  << " month: " << d.m_month
        << " year: " << d.m_year;
            return out;
        }
        template<class Archive>
        void serialize(Archive& archive, const unsigned int version)
        {
        archive & BOOST_SERIALIZATION_NVP(m_day);
            archive & BOOST_SERIALIZATION_NVP(m_month);
            archive & BOOST_SERIALIZATION_NVP(m_year);
        }
    } date;

    BOOST_CLASS_IMPLEMENTATION(date, boost::serialization::object_serializable);//object_serializable);

    unsigned int flags =   boost::archive::no_header;


    int main()
    {

     std::ifstream file1("archive.xml");
     boost::archive::xml_iarchive ia(file1,flags);
     date dr;
     ia >> BOOST_SERIALIZATION_NVP(dr);


     std::ofstream file("archive2.xml");
      boost::archive::xml_oarchive oa(file,flags);
    //  date da(15, 8, 1947);
      oa & BOOST_SERIALIZATION_NVP(dr);


    return 0;
    }

我收到以下错误:

I am getting below error :

抛出的一个实例后终止所谓的推动::档案:: archive_exception
什么():输入流错误
中止(核心转储)

terminate called after throwing an instance of 'boost::archive::archive_exception' what(): input stream error Aborted (core dumped)

有关(如下所述)上述code是工作的罚款,而不属于正常XML

for Normal xml without attributes(as mentioned below) the above code is working fine

<da>
        <m_day>15</m_day>
        <m_month>8</m_month>
        <m_year>1947</m_year>
</da>

但对于previous XML文件,是否有code什么问题?你可以请让我知道如果与boost.i已搜查了这么多去寻找答案,但没有得到任何这是可能的。
在此先感谢!

But for previous xml file ,Is there any problem in code ? could you please let me know if it is possible with boost.i have searched so much to find the answer but failed to get any. Thanks in Advance !!!

推荐答案

升压序列化是的不可以的XML库。

Boost Serialization is not an XML library.

升压存档xml_ [IO]档案不是XML库无论是。

Boost Archive xml_[io]archive isn't an XML library either.

哎呀,甚至没有提升属性树是一个XML库。

Heck, not even Boost Property Tree is an XML library.

在短:升压不包含XML库

略长:你的可以的使用升压属性树来分析XML。它使用引擎盖下RapidXML的衍生物,可以读/写的非常的使用通用的XML文档加速属性树。

Slightly longer: you can use Boost Property Tree to parse your XML. It's using a derivative of RapidXML under the hood and you can read/write fairly generic XML documents using Boost Property Tree.

在现实中,属性树接口是相当具体并常常导致混乱。这里没有很多的控制。

In reality, the Property Tree interface is quite specific and often leads to confusion. There's not a lot of control.

如果你关心你的XML支持(命名空间?空白?顺序?PCDATA?处理指令?XPath的?编码?...),请考虑使用XML库(<一个href=\"http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c/9387612#9387612\">What XML解析器,我应该用C ++使用?)

If you care about your XML support (namespaces? whitespace? ordering? PCDATA? Processing instructions? XPath? Encodings? ...) please consider using an XML library (What XML parser should I use in C++?)

下面是如何使用升压属性树

Here's how to do it with Boost Property Tree

<大骨节病> 住在Coliru

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

typedef struct date { 
    unsigned int m_day;
    unsigned int m_month;
    unsigned int m_year;
    date( int d=1,  int m=1,  int y=2000) : m_day(d), m_month(m), m_year(y) {}

    friend std::ostream& operator << (std::ostream& out, date& d) {
        return out << "day: " << d.m_day << " month: " << d.m_month << " year: " << d.m_year;
    }

    void load(boost::property_tree::ptree const& pt)
    {
        m_day = pt.get("da.m_day", 1);
        m_month = pt.get("da.m_month", 1);
        m_year = pt.get("da.m_year", 2000);
    }
} date;

#include <sstream>

int main() {
    std::istringstream iss("<da>\n"
                "<m_day Type=\"int\">15</m_day>\n"
                "<m_month Type=\"int\">8</m_month>\n"
                "<m_year Type=\"int\">1947</m_year>\n"
                "</da>\n");

    boost::property_tree::ptree pt;
    read_xml(iss, pt);

    date d;
    d.load(pt);

    std::cout << d << "\n";
}

打印

day: 15 month: 8 year: 1947

这篇关于使用boost xml解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆