Skip to content

Instantly share code, notes, and snippets.

@JSchaenzle
Created May 18, 2012 18:37
Show Gist options
  • Save JSchaenzle/2726944 to your computer and use it in GitHub Desktop.
Save JSchaenzle/2726944 to your computer and use it in GitHub Desktop.

Revisions

  1. JSchaenzle revised this gist May 18, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions RapidXmlExample.c
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ int main(void)
    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. ",
  2. JSchaenzle created this gist May 18, 2012.
    40 changes: 40 additions & 0 deletions RapidXmlExample.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #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());
    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;
    }
    }
    18 changes: 18 additions & 0 deletions beerJournal.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?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>