Skip to content

Instantly share code, notes, and snippets.

@JSchaenzle
Created May 18, 2012 18:37
Show Gist options
  • Star 54 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save JSchaenzle/2726944 to your computer and use it in GitHub Desktop.
Save JSchaenzle/2726944 to your computer and use it in GitHub Desktop.
RapidXml example parsing beer journal
<?xml version="1.0" encoding="utf-8"?>
<MyBeerJournal>
<Brewery name="Founders Brewing Company" location="Grand Rapids, MI">
<Beer name="Centennial" description="IPA" rating="A+" dateSampled="01/02/2011">
"What an excellent IPA. This is the most delicious beer I have ever tasted!"
</Beer>
</Brewery>
<Brewery name="Brewery Vivant" location="Grand Rapids, MI">
<Beer name="Farmhouse Ale" description="Belgian Ale" rating="B" dateSampled="02/07/2015">
This beer is not so good... but I am not that big of a fan of english style ales.
</Beer>
</Brewery>
<Brewery name="Bells Brewery" location="Kalamazoo, MI">
<Beer name="Two Hearted Ale" description="IPA" rating="A" dateSampled="03/15/2012">
Another execllent brew. Two Hearted gives Founders Centennial a run for it's money.
</Beer>
</Brewery>
</MyBeerJournal>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "rapidxml-1.13/rapidxml.hpp"
using namespace rapidxml;
using namespace std;
int main(void)
{
cout << "Parsing my beer journal..." << endl;
xml_document<> doc;
xml_node<> * root_node;
// Read the xml file into a vector
ifstream theFile ("beerJournal.xml");
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
// Parse the buffer using the xml file parsing library into doc
doc.parse<0>(&buffer[0]);
// Find our root node
root_node = doc.first_node("MyBeerJournal");
// Iterate over the brewerys
for (xml_node<> * brewery_node = root_node->first_node("Brewery"); brewery_node; brewery_node = brewery_node->next_sibling())
{
printf("I have visited %s in %s. ",
brewery_node->first_attribute("name")->value(),
brewery_node->first_attribute("location")->value());
// Interate over the beers
for(xml_node<> * beer_node = brewery_node->first_node("Beer"); beer_node; beer_node = beer_node->next_sibling())
{
printf("On %s, I tried their %s which is a %s. ",
beer_node->first_attribute("dateSampled")->value(),
beer_node->first_attribute("name")->value(),
beer_node->first_attribute("description")->value());
printf("I gave it the following review: %s", beer_node->value());
}
cout << endl;
}
}
@DrSpeedy
Copy link

This is one of the best examples for the type of program I'm building. Thank you! But the best part of this is, these are all my favorite breweries.

@benjallauro
Copy link

Do you realize that this crashes after entering the first "for", right?

@woto
Copy link

woto commented Mar 10, 2018

I'm beginner in C++, but after compiling seems works fine
@benjallauro

Parsing my beer journal...
I have visited Founders Brewing Company in Grand Rapids, MI. On 01/02/2011, I tried their Centennial which is a IPA. I gave it the following review: 
            "What an excellent IPA. This is the most delicious beer I have ever tasted!"
        
I have visited Brewery Vivant in Grand Rapids, MI. On 02/07/2015, I tried their Farmhouse Ale which is a Belgian Ale. I gave it the following review: 
            This beer is not so good... but I am not that big of a fan of english style ales.
        
I have visited Bells Brewery in Kalamazoo, MI. On 03/15/2012, I tried their Two Hearted Ale which is a IPA. I gave it the following review: 
            Another execllent brew. Two Hearted gives Founders Centennial a run for it's money.

@jayanthas
Copy link

I have a xml which has data (which is my interest) at level 6 or 7. So how can I access them directly?

<?xml version="1.0"?>
<Root-tag xmlns:xsi="" xmlns:xsd="" schemaVersion="" xmlns="">
  <Level2 fileType="" version="" typesetVersion="">
    <Level31></Level31>
    <Level32></Level32>
  </Level2>
  <Level21 version="" typeVersion="">
    <Level34 xsi:type="" typeVersion="">
      <Level4 xsi:type="" typeVersion="">
        <Level5 xsi:type="">
          <Level61>xx</Level61>
          <Level62>xxx,xxx</Level62>
          <Level63></Level63>
          <Level64></Level64>
          <Level65 typeVersion="">
            <Level7></Level7>
          </Level65>
        </Level5>
      </Level4>
    </Level34>
  </Level21>
</Root-tag>

@jayanthas
Copy link

@JSchaenzle could you please answer my question

@Renardjojo
Copy link

Renardjojo commented Nov 27, 2019

I have some example to increase this post. My example is based on creation of scene referential with hierarchy.
XML file :

<?xml version="1.0" encoding="utf-8"?>
<WORLD>
    <REF name="LocalRef_1">
        <REF name="LocalRef_1_1">
            <REF name="LocalRef_1_1_1">
            </REF>
            <REF name="LocalRef_1_1_2">
            </REF>
            <REF name="LocalRef_1_1_3">
            </REF>
        </REF>
    </REF>
</WORLD>

My code to browse information :

 for (rapidxml::xml_node<>* node = doc.first_node("WORLD")->first_node("REF"); node != nullptr; node = node->first_node())
{
	cout << "A : " << node->first_attribute("name")->value() << endl;
        for (rapidxml::xml_node<>* node2 = node; node2 != nullptr; node2 = node2->next_sibling())
        {
	        cout << "B : " << node2->first_attribute("name")->value() <<endl;
        }
}

My result:

A : LocalRef_1
B : LocalRef_1
A : LocalRef_1_1
B : LocalRef_1_1
A : LocalRef_1_1_1
B : LocalRef_1_1_1
B : LocalRef_1_1_2
B : LocalRef_1_1_3

Have fun ! :)

@HyperL0gL0g
Copy link

Excellent example.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment