xml parsing using boost

前端 未结 2 1350
既然无缘
既然无缘 2020-12-21 07:32

I am parsing below xml file using BOOST library-


        15
        8         


        
2条回答
  •  执笔经年
    2020-12-21 07:54

    Boost Serialization is not an XML library.

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

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

    In short: Boost Does Not Contain An XML Library.


    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.

    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++?)

    Bonus: sample with Boost Property Tree

    Here's how to do it with Boost Property Tree

    Live On Coliru

    #include 
    #include 
    #include 
    
    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 
    
    int main() {
        std::istringstream iss("\n"
                    "15\n"
                    "8\n"
                    "1947\n"
                    "\n");
    
        boost::property_tree::ptree pt;
        read_xml(iss, pt);
    
        date d;
        d.load(pt);
    
        std::cout << d << "\n";
    }
    

    Prints

    day: 15 month: 8 year: 1947
    

提交回复
热议问题