      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  #include <xercesc/framework/StdOutFormatTarget.hpp>
      8
      9  #include "item.h"
     10
     11  using namespace std;
     12  using namespace xercesc;
     13
     14  /**
     15     Creates a DOM element with a given name.
     16     @param doc the document that creates the element
     17     @param name the element name
     18     @return an element with the given name
     19  */
     20  DOMElement* create_element(DOMDocument* doc, string name)
     21  {
     22     XMLCh* xname = new XMLCh[name.length() + 1];
     23     XMLString::transcode(name.c_str(), xname, name.length());
     24     DOMElement* r = doc->createElement(xname);
     25     delete xname;
     26     return r;
     27  }
     28
     29  /**
     30     Creates a DOM element containing a text node.
     31     @param doc the document that creates the element
     32     @param name the element name
     33     @param text the text for the text node
     34     @return an element with the given name and a child that is a
     35     text node with the given text
     36  */
     37  DOMElement* create_text_element(DOMDocument* doc,
     38     string name, string text)
     39  {
     40     XMLCh* xtext = new XMLCh[text.length() + 1];
     41     XMLString::transcode(text.c_str(), xtext, text.length());
     42     DOMText* textNode = doc->createTextNode(xtext);
     43     delete xtext;
     44
     45     DOMElement* r = create_element(doc, name);
     46     r->appendChild(textNode);
     47
     48     return r;
     49  }
     50
     51  /**
     52     Converts an floating-point value to a string, e.g. 3.14 -> "3.14".
     53     @param n an floating-point value
     54     @return the equivalent string
     55  */
     56  string double_to_string(double n)
     57  {
     58     ostringstream outstr;
     59     outstr << n;
     60     return outstr.str();
     61  }
     62
     63  /**
     64     Builds a DOM element for a product.
     65     @param p the product
     66     @return a DOM element describing the product
     67  */
     68  DOMElement* create_product(DOMDocument* doc, const Product& p)
     69  {
     70     DOMElement* description_element = create_text_element(doc,
     71        "description", p.get_description());
     72     DOMElement* price_element = create_text_element(doc,
     73        "price", double_to_string(p.get_price()));
     74
     75     DOMElement* product_element = create_element(doc, "product");
     76     product_element->appendChild(description_element);
     77     product_element->appendChild(price_element);
     78
     79     return product_element;
     80  }
     81
     82  /**
     83     Builds a DOM element for an item.
     84     @param anItem the item
     85     @return a DOM element describing the item
     86  */
     87  DOMElement* create_item(DOMDocument* doc, const Item& anItem)
     88  {
     89     DOMElement* product_element
     90        = create_product(doc, anItem.get_product());
     91     DOMElement* quantity_element = create_text_element(doc,
     92        "quantity", double_to_string(anItem.get_quantity()));
     93
     94     DOMElement* item_element = create_element(doc, "item");
     95     item_element->appendChild(product_element);
     96     item_element->appendChild(quantity_element);
     97
     98     return item_element;
     99  }
    100
    101  /**
    102     Builds a DOM element for an array list of items.
    103     @param items the items
    104     @return a DOM element describing the items
    105  */
    106  DOMElement* create_item_list(DOMDocument* doc,
    107     const vector<Item>& items)
    108  {
    109     DOMElement* items_element = create_element(doc, "items");
    110     for (int i = 0; i < items.size(); i++)
    111     {
    112        DOMElement* item_element = create_item(doc, items[i]);
    113        items_element->appendChild(item_element);
    114     }
    115     return items_element;
    116  }
    117
    118  int main()
    119  {
    120     XMLPlatformUtils::Initialize();
    121
    122     // populate vector of items
    123     vector<Item> items;
    124     items.push_back(Item(Product("Toaster", 29.95), 3));
    125     items.push_back(Item(Product("Hair dryer", 24.95), 1));
    126
    127     // build the DOM document
    128     DOMImplementation* implementation
    129        = DOMImplementation::getImplementation();
    130     DOMDocument* doc = implementation->createDocument();
    131     doc->setStandalone(true);
    132
    133     DOMElement* root = create_item_list(doc, items);
    134     doc->appendChild(root);
    135
    136     // print the DOM document
    137
    138     DOMWriter* writer = implementation->createDOMWriter();
    139     writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
    140     XMLFormatTarget* out = new StdOutFormatTarget();
    141     writer->writeNode(out, *doc);
    142
    143     writer->release();
    144     doc->release();
    145
    146     return 0;
    147  }