IncredibleXMLParser  3.05
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IXMLTestUnicode.cpp
Go to the documentation of this file.
1 
16 #ifdef WIN32
17 #define _CRT_SECURE_NO_DEPRECATE
18 #endif
19 
20 #include <stdio.h>
21 #include "IXMLParser.h"
23 const wchar_t *t2=L"<a><b>some text</b><b>other text </a>";
24 
26 // //
28 // //
30 void example1()
31 {
32  printf("EXAMPLE 1\n");
33 
34  // this open and parse the XML file:
35  IXMLDomParser iDom;
36  iDom.setRemoveClears(false);
37  ITCXMLNode xMainNode=iDom.openFileHelper(L"TestFiles/PMMLModel.xml",L"PMML");
38 
39  // this prints "RANK For <you>":
40  ITCXMLNode xNode=xMainNode.getChildNode(L"Header");
41  printf(" Application Name is: '%S'\n", xNode.getChildNode(L"Application").getAttribute(L"name"));
42 
43  // this prints "Hello World!"
44  printf(" Text inside Header tag is :'%S'\n", xNode.getText());
45 
46  // this prints "urn"
47  printf(" Value of the first attribute of the 'PMML/Extension/Key' tag is :'%S'\n", xMainNode.getChildNodeByPath(L"Extension/Key").getAttributeValue());
48 
49  // this gets the number of "NumericPredictor" tags:
50  xNode=xMainNode.getChildNode(L"RegressionModel").getChildNode(L"RegressionTable");
51  int n=xNode.nChildNode(L"NumericPredictor");
52 
53  // this prints the "coefficient" value for all the "NumericPredictor" tags:
54  int i,myIterator=0;
55  for (i=0; i<n; i++)
56  printf(" coeff %i=%S\n",i+1,xNode.getChildNode(L"NumericPredictor",&myIterator).getAttribute(L"coefficient"));
57 
58  // this create a file named "testUnicode.xml" based on the content of the first "Extension" tag of the XML file:
59  IXMLRenderer().writeToFile(xMainNode.getChildNode(L"Extension"),L"TestFiles/testUnicode.xml");
60 
61  printf(" The content of the clear tag is:%S\n",xMainNode.getChildNode(L"html_page").getChildNode(L"a").getClear().sValue);
62 }
63 
65 // //
67 // //
69 void example2()
70 {
71  printf("EXAMPLE 2\n");
72  IXMLDomParser iDom;
73  ITCXMLNode xMainNode=iDom.openFileHelper(L"TestFiles/PMMLModel.xml",L"PMML");
74 
75  // compare these 4 lines ...
76  wchar_t *t=IXMLStringDup(xMainNode.getAttribute(L"version")); // get version number
77  iDom.clear(); // free from memory the complete xml Tree
78  printf(" PMML Version :%S\n\n",t); // print version number
79  free(t); // free version number
80 
81 // ... with the following 3 lines (currently commented, because of error):
82 // const wchar_t *t=xMainNode.getAttribute(L"version"); // get version number (note that there is no 'stringDup')
83 // udom.clear(); // free from memory the complete xml Tree AND the version number inside the 't' var
84 // printf("PMML Version :%S\n",t); // since the version number in 't' has been free'd this will not work
85 }
86 
88 // //
90 // //
92 void example3()
93 {
94  printf("EXAMPLE 3\n");
95 
96 // We create in memory from scratch the following XML structure:
97 // <?xml version="1.0"?>
98 // <body color="#FFFFFF"> Hello "universe". </body>
99 // <A><B><C>pick-a-boo</C></B></A>
100 // ... and we transform it into a standard C string that is printed on screen.
101  IXMLNode xMainNode,xNode;
102  xMainNode=IXMLNode::createXMLTopNode();
103  xNode=xMainNode.addChild(L"xml",TRUE);
104  xNode.addAttribute(L"version",L"1.0");
105  xNode=xMainNode.addChild(L"body");
106  xNode.addText(L"Hello \"univ\"!");
107  xNode.deleteText();
108  xNode.addText(L"Hello \"universe\"!");
109  xNode.addAttribute(L"color",L"#wrongcolor");
110  xNode.updateAttribute(L"#FFFFFF",NULL,L"color");
111  xMainNode.addChild(L"A").addChild(L"B").addChild(L"C").addText(L"pick-a-boo");
112 
113  IXMLRenderer uRender;
114  IXMLCStr t=uRender.getString(xMainNode);
115  printf(" XMLString created from scratch:\n%S",t);
116 
117  // we delete some parts:
118  xNode.deleteAttribute(L"color");
119  xMainNode.getChildNode(L"A").deleteNodeContent();
120  t=uRender.getString(xMainNode,false);
121  printf("\n With the \"A\" tag deleted and the \"color\" attribute deleted:\n %S\n\n",t);
122 }
123 
125 // //
127 // //
129 void example4()
130 {
131  printf("EXAMPLE 4\n");
132 
133 // By default, the XML parser is "forgiving":
134 // (You can de-activate this behavior: see the header of xmlParser.cpp file)
135  IXMLResults xe;
136  ICXMLNode xMainNode=IXMLDomParser().parseStringNonT(t2,NULL,&xe);
137  IXMLRenderer iRender;
138  IXMLCStr t=iRender.getString(xMainNode,false);
139  printf(" The following XML: %S\n ...is parsed as: %S\n with the following info: '%S'\n\n",
140  t2,t?t:L"(null)",IXMLPullParser::getErrorMessage(xe.errorCode));
141 }
142 
144 // //
146 // //
148 void example5()
149 {
150  printf("EXAMPLE 5\n");
152 
153  // this deletes the "<b>other text</b>" subtree part:
154  xMainNode.getChildNode(L"b",1).deleteNodeContent();
155 
156  // To perform the same "delete" as above, we can also do:
157  // xNode=xMainNode.getChildNode(L"a").getChildNode(L"b",1); xNode.deleteNodeContent(); xNode=XMLNode::emptyXMLNode;
158  // If you forget the last part of the delete ("xNode=XMLNode::emptyXMLNode"), then the XMLNode will NOT be deleted:
159  // As long as there exists a reference to an XMLNode, the smartPointer mechanism prevent the node to be deleted.
160 
161  // To perform the same "delete" as above, we can also do:
162  // xNode=xMainNode.getChildNode(L"a").getChildNode(L"b",1); xNode.deleteNodeContent(true);
163  // The "true" parameter will force the deletion, even if there still exists some references to the XMLNode.
164  // This is however very dangerous because, after the delete, the xNode object is invalid and CANNOT be used anymore.
165  // Unexpected results may appear if you still try to access the xNode object after its "forced" deletion.
166  printf(" ...with the wrong node deleted: %S\n\n",IXMLRenderer().getString(xMainNode,false));
167 }
168 
170 // //
172 // //
174 void example6()
175 {
176  printf("EXAMPLE 6\n");
177  // This creates a XMLNode 'a' that is "<a><b>some text</b><b>other text</b></a>":
179 
180  // This creates a XMLNode 'c' that is "<c>hello</c>":
181  IXMLNode xNode=IXMLDomParser().parseString(L"<c>hello</c>").deepCopy();
182 
183  xMainNode.addChild(xNode,0);
184  IXMLRenderer uRender;
185  printf(" We inserted a new node 'c' as the first tag inside 'a':\n %S",uRender.getString(xMainNode,false));
186  xMainNode.addChild(xNode,xMainNode.positionOfChildNode(L"b",1));
187  printf("\n We moved the node 'c' at the position of the second 'b' tag:\n %S\n\n",uRender.getString(xMainNode,false));
188 }
189 
191 // //
193 // //
195 void example7()
196 {
197  printf("EXAMPLE 7\n");
198  printf(" Enumeration of the 'RegressionTable' tag inside the PMML file:\n");
199  IXMLReaderFile xmlfile(L"TestFiles/PMMLModel.xml");
200  IXMLPullParser pp(&xmlfile);
201  pp.findPath(L"PMML/RegressionModel/RegressionTable");
202  ITCXMLNode xMainNode;
203  IXMLDomParser iDom;
204  iDom.setRemoveClears(false);
205  for(;;)
206  {
207  xMainNode=iDom.parseOneChild(&pp);
208  if (xMainNode.nElement()==0) break;
209  printf(" <%S %S='%S' %S='%S'/>\n",xMainNode.getName(),xMainNode.getAttributeName(0),xMainNode.getAttributeValue(0),
210  xMainNode.getAttributeName(1),xMainNode.getAttributeValue(1));
211  }
212  pp.findPath(L"../html_page");
213  xMainNode=iDom.parseOneChild(&pp);
214  printf(" The content of the clear tag is:%S\n",xMainNode.getClear().sValue);
215 }
216 
218 // //
220 // //
222 void example8()
223 {
224  printf("\nEXAMPLE 8\n");
225 
226  unsigned char *originalBinaryData=(unsigned char *)"this is binary data.";
228  IXMLCStr t=b64.encode(originalBinaryData,21);
229  printf(
230  " To be able to include any binary data into an xml file, some Base64\n"
231  " conversion functions (binary data <--> ascii text) are provided:\n"
232  " original binary data : %s\n"
233  " encoded as text : %S\n",originalBinaryData,t);
234  printf(" decoded as binary again: %s\n\n",b64.decode(t));
235 }
236 
238 // //
240 // //
242 void example9()
243 {
244  printf("EXAMPLE 9\n");
245  printf(" Processing XML file containing chinese,cyrilic and other extended characters.\n");
246  ICXMLNode xMainNode=IXMLDomParser().parseFileNonT(L"TestFiles/utf8test.xml");
247  IXMLRenderer().writeToFile(xMainNode,L"TestFiles/outputTestUTF16.xml");
248  printf(" ... resulting multi-lingual file is 'outputTestUTF16.xml'.\n\n");
249 }
250 
252 // //
254 // //
256 void example10()
257 {
258  printf("EXAMPLE 10\n");
259  printf(" Two examples of usage of the \"getParentNode()\" method:\n");
260  // In the following two examples, I create a tree of XMLNode based on the string
261  // "<a><b>some text</b><b>other text</b></a>". After parsing this string
262  // I get a XMLNode that represents the <a> tag. Thereafter I "go down" one
263  // level, using getChildNode: I now have a XMLNode that represents the <b> tag.
264  // Thereafter I "go up" one level, using getParentNode(): I now have once again
265  // a XMLNode that represents the <a> tag. Thereafter, I print the name ('a') of
266  // this last XMLNode. The first example below is working as intended (it prints 'a'
267  // on the screen). However, the second example below prints "null" because when we
268  // did "xMainNode=xMainNode.getChildNode()" we lost all references to the
269  // top node and thus it's automatically "garbage collected" (free memory).
270  IXMLCStr t;
271  IXMLNode xNode,xMainNode;
272 
273  xMainNode=IXMLDomParser().parseString(t2).deepCopy();
274  xNode=xMainNode.getChildNode(); xNode=xNode.getParentNode(); t=(wchar_t*) xNode.getName(); printf(" Ex1: Name of top node; '%S'\n",t?t:L"null");
275 
276  xMainNode=IXMLDomParser().parseString(t2).deepCopy();
277  xMainNode=xMainNode.getChildNode(); xMainNode=xMainNode.getParentNode(); t=(wchar_t*)xMainNode.getName(); printf(" Ex2: Name of top node; '%S'\n",t?t:L"null");
278 }
279 
281 // //
283 // //
285 void example11()
286 {
287  printf("\nEXAMPLE 11\n");
288 
289  // For performance reason it's sometime better to use the old-style "fwprintf"
290  // function to create a XML file directly without constructing first
291  // a XMLNode structure. In such case, the ToXMLStringTool class comes in handy.
292 
293  const wchar_t *t3=L"Hello to the <\"World\">";
294  printf(" ToXMLStringTool demo: Original String: %S\n"
295  " Encoded in XML : %S\n",t3,ToIXMLStringTool().toXML(t3));
296 
297  // If you use several time (in different "fwprintf") the same ToXMLStringTool
298  // object, then the memory allocation (needed to create the output
299  // buffer) will be performed only once. This is very efficient, very fast.
300  // Usually, I create a global instance of the ToXMLStringTool class named "tx" (see
301  // line 42 of this file) and then I use "tx" everywhere. For example:
302  const wchar_t *t4=L"I say 'pick-a-boo'!";
303  printf(" Global ToXMLStringTool: %S\n",tx.toXML(t4));
304  printf(" Global ToXMLStringTool: %S\n",tx.toXML(t3));
305 
306  // However you must be careful because sometime the output buffer might be
307  // erased before being printed. The next example is not working:
308  printf(" Error using ToXMLStringTool: %S\n"
309  " %S\n",tx.toXML(t4),tx.toXML(t3));
310 
311  // However, this is working fine:
312  printf(" Correct usage of ToXMLStringTool: %S\n"
313  " %S\n",tx.toXML(t4),tx2.toXML(t3));
314 
315  // Using the "ToXMLStringTool class" and the "fwprintf function" is THE most efficient
316  // way to produce VERY large XML documents VERY fast.
317 }
318 
320 // //
322 // //
324 void example12()
325 {
326  printf("\nEXAMPLE 12\n Read and parse a JSON file.\n");
327  IXMLReaderFile iReader(L"TestFiles/twitter.json");
328  IJSONPullParser pp(&iReader);
329  IXMLDomParser iDom;
330  ITCXMLNode x=iDom.parse(&pp);
331  printf(" Write back the JSON file as an XML File.\n");
332  IXMLRenderer().writeToFile(x,L"TestFiles/twitterUTF16.xml");
333  printf(" Search inside the JSON file using XPATH:\n"
334  " Content of 'results/[2]/metadata/recent_retweets' is '%S'\n\n",
335  x.getElementByPath(L"results/[2]/metadata/recent_retweets"));
336 }
337 
338 int main(int argc, char **argv)
339 {
342  return 0;
343 }