C++ : Parsing XML using Boost

Boost is a collection of very powerful libraries for C++. We could use boost to parse various format like XML, JSON etc.

Parsing XML string

In the below program ( example.cpp ) we feed a string (xml) to the boost property tree.
Boost library used : 1_77_0

Compilation :

g++ example.cpp -lboost_system

Program : example.cpp

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

namespace pt = boost :: property_tree;


int main()
{
    std :: string xml_str = "<?xml version = \"1.0\" encoding = \"utf-8\"?>\
    <tv>\
        <series>\
            <name>Breaking Bad</name>\
            <genre>crime</genre>\
            <year>2008-2013</year>\
            <creator>Vince Gilligan</creator>\
            <imdb>9.4</imdb>\
            <stream>Netflix</stream>\
        </series>\
        <series>\
            <name>Game Of Thrones</name>\
            <genre>Adventure</genre>\
            <year>2011-2019</year>\
            <creator>David Benioff & D.B. Weiss</creator>\
            <imdb>9.4</imdb>\
            <stream>Netflix</stream>\
       </series>\
    </tv>";

    // Create an empty property tree object
    pt :: ptree tv_tree;

    try {
        std :: stringstream ss; 
        ss << xml_str;
        read_xml(ss, tv_tree);
    } catch (pt :: xml_parser_error &e) {
        std :: cout << "Failed to parse the xml string." << e.what();
    } catch (...) {
        std :: cout << "Failed !!!";
    }

    // get_child is used to find the node in xml for iterating over its children.
    // Note : get_child throws if the path cannot be resolved. 
    for (auto& p : tv_tree.get_child("tv")) {
        std :: cout << "[" << p.first << "]" << std :: endl;
        for (auto& c : p.second) {
            std :: cout << "Tag : [" << c.first.data() << "], ";
            std :: cout << "Value : [" << c.second.data() << "]" << std :: endl;
        }   
    }
    return 0;
}

Output

[series]
Tag : [name], Value : [Breaking Bad]
Tag : [genre], Value : [crime]
Tag : [year], Value : [2008-2013]
Tag : [creator], Value : [Vince Gilligan]
Tag : [imdb], Value : [9.4]
Tag : [stream], Value : [Netflix]
[series]
Tag : [name], Value : [Game Of Thrones]
Tag : [genre], Value : [Adventure]
Tag : [year], Value : [2011-2019]
Tag : [creator], Value : [David Benioff & D.B. Weiss]
Tag : [imdb], Value : [9.4]
Tag : [stream], Value : [Netflix]


Parsing XML file

In the below program ( example.cpp ) we feed an xml file to the boost property tree.

Compilation :

g++ example.cpp -lboost_system

Consider a sample xml file.

XML file : tv.xml

<?xml version = \"1.0\" encoding = \"utf-8\"?>
<tv>
    <series>
        <name>House of Cards</name>
        <genre>Drama</genre>
        <year>2013-2018</year>
        <creator>Beau Willimon</creator>
        <imdb>8.7</imdb>
        <stream>Netflix</stream>
     </series>
     <series>
         <name>Fullmetal Alchemist</name>
         <genre>Animation, Action and Adventure</genre>
         <year>2003-2004</year>
         <creator>Hiromu Arakawa</creator>
         <imdb>8.5</imdb>
         <stream>Netflix</stream>
    </series>
</tv>

Program : example.cpp

#include <boost/property_tree/ptree.hpp>                                                    
#include <boost/property_tree/xml_parser.hpp>                                               
#include <boost/property_tree/detail/file_parser_error.hpp>                                 
#include <iostream>                                                                         
                                                                                            
namespace pt = boost :: property_tree;                                                      
                                                                                            
int main()                                                                                  
{                                                                                           
    // Create an empty property tree object                                                 
    pt :: ptree tv_tree;                                                                    
                                                                                            
    try {                                                                                   
        read_xml("tv.xml", tv_tree);                                                        
    } catch (pt :: xml_parser_error &e) {                                                     
        std :: cout << "Failed to parse the xml string." << e.what();                       
    } catch (...) {                                                                         
        std :: cout << "Failed !!!";                                                        
    }                                                                                       
                                                                                            
    // get_child is used to find the node in xml for iterating over its children.           
    // Note : get_child throws if the path cannot be resolved.                              
    for (auto& p : tv_tree.get_child("tv")) {                                               
        std :: cout << "[" << p.first << "]" << std :: endl;                                
        for (auto& c : p.second) {                                                          
            std :: cout << "Tag : [" << c.first.data() << "], ";                            
            std :: cout << "Value : [" << c.second.data() << "]" << std :: endl;            
        }                                                                                   
    }                                                                                       
    return 0;                                                                               
}

Output

[series]
Tag : [name], Value : [House of Cards]
Tag : [genre], Value : [Drama]
Tag : [year], Value : [2013-2018]
Tag : [creator], Value : [Beau Willimon]
Tag : [imdb], Value : [8.7]
Tag : [stream], Value : [Netflix]
[series]
Tag : [name], Value : [Fullmetal Alchemist]
Tag : [genre], Value : [Animation, Action and Adventure]
Tag : [year], Value : [2003-2004]
Tag : [creator], Value : [Hiromu Arakawa]
Tag : [imdb], Value : [8.5]
Tag : [stream], Value : [Netflix]


Copyright (c) 2019-2024, Algotree.org.
All rights reserved.