      1  #include <string>
      2  #include <sstream>
      3  #include <vector>
      4  #include <xercesc/dom/DOM.hpp>
      5  #include <xercesc/util/XMLString.hpp>
      6  #include <xercesc/util/PlatformUtils.hpp>
      7
      8  #include "item.h"
      9
     10  using namespace std;
     11  using namespace xercesc;
     12
     13  /*
     14    Converts a sequence of XMLCh characters to a string.
     15    @param in the sequence of XMLCh characters
     16    @return the transcoded string
     17  */
     18  string XMLCh_to_string(const XMLCh* in)
     19  {
     20     char* s = XMLString::transcode(in);
     21     string r(s);
     22     XMLString::release(&s);
     23     return r;
     24  }
     25
     26  /**
     27     Converts a string to a floating-point value, e.g. "3.14" -> 3.14.
     28     @param s a string representing a floating-point value
     29     @return the equivalent floating-point value
     30  */
     31  double string_to_double(string s)
     32  {
     33     istringstream instr(s);
     34     double x;
     35     instr >> x;
     36     return x;
     37  }
     38
     39  /**
     40     Converts a string to an integer, e.g. "314" -> 314.
     41     @param s a string representing an integer
     42     @return the equivalent integer
     43  */
     44  int string_to_int(string s)
     45  {
     46     istringstream instr(s);
     47     int x;
     48     instr >> x;
     49     return x;
     50  }
     51
     52  /**
     53     Obtains a product from a DOM node
     54     @param e a <product> element
     55     @return the product described by the given node
     56  */
     57  Product get_product(DOMNode* e)
     58  {
     59     DOMNodeList* children = e->getChildNodes();
     60     string name = "";
     61     double price = 0;
     62     for (int i = 0; i < children->getLength(); i++)
     63     {
     64        DOMNode* child_node = children->item(i);
     65        DOMElement* child_element
     66           = dynamic_cast<DOMElement*>(child_node);
     67        if (child_element != NULL)
     68        {
     69           string tagName = XMLCh_to_string(child_element->getTagName());
     70           DOMText* textNode
     71              = dynamic_cast<DOMText*>(child_element->getFirstChild());
     72
     73           if (tagName == "description")
     74              name = XMLCh_to_string(textNode->getData());
     75           else if (tagName == "price")
     76           {
     77              string price_text = XMLCh_to_string(textNode->getData());
     78              price = string_to_double(price_text);
     79           }
     80        }
     81     }
     82     return Product(name, price);
     83  }
     84
     85  /**
     86     Obtains an item from a DOM node
     87     @param e an <item> element
     88     @return the item described by the given node
     89  */
     90  Item get_item(DOMNode* e)
     91  {
     92     DOMNodeList* children = e->getChildNodes();
     93     Product p;
     94     int quantity = 0;
     95     for (int i = 0; i < children->getLength(); i++)
     96     {
     97        DOMNode* child_node = children->item(i);
     98        DOMElement* child_element
     99           = dynamic_cast<DOMElement*>(child_node);
    100        if (child_element != NULL)
    101        {
    102           string tagName = XMLCh_to_string(child_element->getTagName());
    103           if (tagName == "product")
    104              p = get_product(child_element);
    105           else if (tagName == "quantity")
    106           {
    107              DOMText* textNode = dynamic_cast<DOMText*>(
    108                 child_element->getFirstChild());
    109              string quantity_text
    110                 = XMLCh_to_string(textNode->getData());
    111              quantity = string_to_int(quantity_text);
    112           }
    113        }
    114     }
    115     return Item(p, quantity);
    116  }
    117
    118
    119  /**
    120     Obtains an array list of items from a DOM node
    121     @param e an <items> element
    122     @return a vector of all <item> children of e
    123  */
    124  vector<Item> get_items(DOMNode* e)
    125  {
    126     vector<Item> items;
    127
    128     // get the <item> children
    129
    130     DOMNodeList* children = e->getChildNodes();
    131     for (int i = 0; i < children->getLength(); i++)
    132     {
    133        DOMNode* child_node = children->item(i);
    134        DOMElement* child_element
    135           = dynamic_cast<DOMElement*>(child_node);
    136        if (child_element != NULL)
    137        {
    138           string tagName = XMLCh_to_string(child_element->getTagName());
    139           if (tagName == "item")
    140           {
    141              Item c = get_item(child_element);
    142              items.push_back(c);
    143           }
    144        }
    145     }
    146     return items;
    147  }
    148
    149  int main()
    150  {
    151     XMLPlatformUtils::Initialize();
    152
    153     DOMImplementation* implementation
    154        = DOMImplementation::getImplementation();
    155     DOMBuilder* parser = implementation->createDOMBuilder(
    156        DOMImplementationLS::MODE_SYNCHRONOUS, NULL);
    157     DOMDocument* doc = parser->parseURI("items.xml");
    158
    159     DOMNode* root = doc->getDocumentElement();
    160     vector<Item> items = get_items(root);
    161
    162     for (int i = 0; i < items.size(); i++)
    163        items[i].print();
    164     parser->release();
    165
    166     return 0;
    167  }