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