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

📄 parser2.cpp.html

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

<head>
	<title>parser2.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 element
 54     @param e a &lt;product&gt; element
 55     @return the product described by the given element
 56  */</font>
 57  Product get_product(DOMNode* e)
 58  {
 59     DOMNodeList* children = e-&gt;getChildNodes();
 60  
 61     DOMText* textNode
 62        = dynamic_cast&lt;DOMText*&gt;(children-&gt;item(0)-&gt;getFirstChild());
 63     string name = XMLCh_to_string(textNode-&gt;getData());
 64  
 65     textNode
 66        = dynamic_cast&lt;DOMText*&gt;(children-&gt;item(1)-&gt;getFirstChild());
 67     string price_text = XMLCh_to_string(textNode-&gt;getData());
 68     double price = string_to_double(price_text);
 69  
 70     return Product(name, price);
 71  }
 72  
 73  <font color='#0000cc'>/**
 74     Obtains an item from a DOM element
 75     @param e an &lt;item&gt; element
 76     @return the item described by the given element
 77  */</font>
 78  Item get_item(DOMNode* e)
 79  {
 80     DOMNodeList* children = e-&gt;getChildNodes();
 81     Product p = get_product(children-&gt;item(0));
 82  
 83     DOMText* textNode = dynamic_cast&lt;DOMText*&gt;(
 84        children-&gt;item(1)-&gt;getFirstChild());
 85     string quantity_text = XMLCh_to_string(textNode-&gt;getData());
 86     int quantity = string_to_int(quantity_text);
 87  
 88     return Item(p, quantity);
 89  }
 90  
 91  <font color='#0000cc'>/**
 92     Obtains an array list of items from a DOM element
 93     @param e an &lt;items&gt; element
 94     @return a vector of all &lt;item&gt; children of e
 95  */</font>
 96  vector&lt;Item&gt; get_items(DOMNode* e)
 97  {
 98     vector&lt;Item&gt; items;
 99  
100     <font color='#0000cc'>// get the &lt;item&gt; children</font>
101  
102     DOMNodeList* children = e-&gt;getChildNodes();
103     for (int i = 0; i &lt; children-&gt;getLength(); i++)
104     {
105        Item c = get_item(children-&gt;item(i));
106        items.push_back(c);
107     }
108     return items;
109  }
110  
111  class SimpleErrorHandler : public DOMErrorHandler
112  {
113  public:
114      bool handleError(const DOMError&amp; domError);
115  };
116  
117  bool SimpleErrorHandler::handleError(const DOMError&amp; error)
118  {
119      cout &lt;&lt; XMLCh_to_string(error.getLocation()-&gt;getURI())
120           &lt;&lt; ", line " &lt;&lt; error.getLocation()-&gt;getLineNumber()
121           &lt;&lt; ", char " &lt;&lt; error.getLocation()-&gt;getColumnNumber()
122           &lt;&lt; ": " &lt;&lt; XMLCh_to_string(error.getMessage()) &lt;&lt; "\n";
123      return error.getSeverity() != DOMError::DOM_SEVERITY_FATAL_ERROR;
124  }
125  
126  int main()
127  {
128     XMLPlatformUtils::Initialize();
129  
130     DOMImplementation* implementation
131        = DOMImplementation::getImplementation();
132     DOMBuilder* parser = implementation-&gt;createDOMBuilder(
133        DOMImplementationLS::MODE_SYNCHRONOUS, NULL);
134     DOMErrorHandler* handler = new SimpleErrorHandler();
135     parser-&gt;setErrorHandler(handler);
136     parser-&gt;setFeature(XMLUni::fgDOMValidation, true);
137     parser-&gt;setFeature(XMLUni::fgDOMWhitespaceInElementContent, false);
138     DOMDocument* doc = parser-&gt;parseURI("items.xml");
139  
140     DOMNode* root = doc-&gt;getDocumentElement();
141     vector&lt;Item&gt; items = get_items(root);
142  
143     for (int i = 0; i &lt; items.size(); i++)
144        items[i].print();
145  
146     parser-&gt;release();
147     delete handler;
148  
149     return 0;
150  }</pre>
</body>
</html>

⌨️ 快捷键说明

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