⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
字号:
<html>

<head>
	<title>parser.cpp</title>
</head>

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -