4 * PWLib application source file for PxmlTest
6 * Main program entry point.
8 * Copyright 2002 David Iodice.
11 * Revision 1.1 2002/03/07 01:56:56 robertj
12 * Added XML sample/test program.
20 PCREATE_PROCESS(PxmlTest
);
25 : PProcess("XML Testing Guru", "PxmlTest", 1, 0, AlphaCode
, 1)
29 static const PString
XML_ID("<?xml version=\"1.0\"?>");
30 static const PString
LEThdr("<s:Envelope xmlns:s=\"http://schemas.letter.org/letter/envelope/\" s:encodingStyle=\"http://schemas.letter.org/letter/encoding/\"><s:Body>");
31 static const PString
LETtrl("</s:Body></s:Envelope>");
32 static const PString
SetNumHdr("<u:SetNum xmlns:u=\"urn:schemas-dustbowl:1\"><Num>");
33 static const PString
SetNumTrl("</Num></u:SetNum>");
35 void printElement(int i
,PXMLObject
* el
)
39 cout
<< "("<<i
++<<") "<< ((const PXMLElement
*)el
)->GetName() << endl
;
40 if (((const PXMLElement
*)el
)->HasAttributes())
42 for (PINDEX j
=0; j
< ((const PXMLElement
*)el
)->GetNumAttributes(); ++j
)
44 cout
<< " " << ((const PXMLElement
*)el
)->GetKeyAttribute(j
) << "=" << ((const PXMLElement
*)el
)->GetDataAttribute(j
);
47 cout
<< " " << ((const PXMLElement
*)el
)->GetData() << endl
;
48 if (((const PXMLElement
*)el
)->HasSubObjects())
50 PXMLObjectArray obs
= ((const PXMLElement
*)el
)->GetSubObjects();
51 for (PINDEX o
=0;o
<obs
.GetSize();++o
) printElement(i
,&obs
[o
]);
60 PString
COLORVAL("<Color>5</Color>");
61 PString
COLORT1("<Color>");
62 PString
COLORT2("</Color>");
63 PString
COLORT3("<Color/>");
65 PString
ns(PString::Signed
,num
);
66 PAssert((COLORT1
== PXML::CreateStartTag(t
)),"PXML::CreateStartTag(t) failed");
67 PAssert((COLORT2
== PXML::CreateEndTag(t
)),"PXML::CreateEndTag(t) failed");
68 PAssert((COLORT3
== PXML::CreateTagNoData(t
)),"PXML::CreateTagNoData(t) failed");
69 PAssert((COLORVAL
== PXML::CreateTag(t
,ns
)),"PXML::CreateTag(t,ns) failed");
71 PString ch
= LEThdr
+ SetNumHdr
+ "23" + SetNumTrl
+ LETtrl
;
73 PXML
xml(ch
,PXML::Indent
|PXML::NewLineAfterElement
+PXML::NoIgnoreWhiteSpace
);
77 //can we recreate the XML?
80 PString
ROOTELE("s:Envelope");
81 PString
ROOTATKEY1("xmlns:s");
82 PString
ROOTATDAT1("http://schemas.letter.org/letter/envelope/");
83 PString
ROOTATKEY2("s:encodingStyle");
84 PString
ROOTATDAT2("http://schemas.letter.org/letter/encoding/");
85 PAssert((ROOTELE
== xml
.GetRootElement()->GetName()),"Root element name not as expected");
86 if (xml
.GetRootElement()->HasAttributes()) {
87 for (PINDEX a
=0; a
<xml
.GetRootElement()->GetNumAttributes(); ++a
) {
88 PAssert((ROOTATKEY1
== xml
.GetRootElement()->GetKeyAttribute(a
) ||
89 ROOTATKEY2
== xml
.GetRootElement()->GetKeyAttribute(a
)),"Root element attribute key not as expected");
90 PAssert((ROOTATDAT1
== xml
.GetRootElement()->GetDataAttribute(a
) ||
91 ROOTATDAT2
== xml
.GetRootElement()->GetDataAttribute(a
)),"Root element attribute data not as expected");
94 PAssert((PString() == xml
.GetRootElement()->GetData()),"Root element data not as expected");
98 // An example of printing elements -- see the function defined above
100 for (PINDEX i
=0; i
<xml
.GetNumElements(); ++i
)
102 printElement(i
,xml
.GetElement(i
));
107 // Just checking reloading the same data in a different format
109 PStringStream rs
; xml
.GetRootElement()->PrintOn(rs
, -1, 0);
112 cout
<< "Revamped XML" << revamp
<< endl
;
116 // Extract a subset of the XML
117 // This can be used when the "data" of an XML element
118 // is XML and you don't need that parsed yet :-)
121 xml
.GetElement(0)->PrintOn(ss
, -1, 0);
123 PString
EXCERPT("<s:Body>" + SetNumHdr
+ "23" + SetNumTrl
+ "</s:Body>");
124 PAssert((EXCERPT
== ss
),"XML subset data not as expected");
127 PXMLElement
el(NULL
, t
);
128 PXMLData
* d
= new PXMLData(NULL
,ns
);
130 PAssert((PString("Color") == el
.GetName()),"Element name not as expected");
131 PAssert((PString("5") == el
.GetData()),"Element data not as expected");
135 // Checkout these examples of the PXMLSettings class
137 // Constructor with data and options
139 PXMLSettings
dgi(ch
,PXML::Indent
| PXML::NewLineAfterElement
| PXML::NoIgnoreWhiteSpace
);
140 cout
<< "PXMLSettings: " << dgi
<< endl
;
141 // Default Constructor
143 PString
optsect("Options");
144 PString
comType("Comm Type");
146 opts
.SetAttribute(optsect
,comType
,val
);
147 cout
<< "attributes==>" << opts
.GetAttribute(optsect
,comType
) << "<==" << endl
;
148 cout
<< "PXMLSettings: " << opts
;
149 cout
<< "done" << endl
;
152 // Constructor with PConfig and String
153 PFilePath
fp("cfg.txt");
154 PConfig
cfg(fp
, "Options");
155 PXMLSettings
xmlcfg(cfg
);
158 PFilePath
fp2("cfgOut.txt");
159 PConfig
* cfg2
; cfg2
= new PConfig(fp2
, "Root");
160 xmlcfg
.ToConfig(*cfg2
);
165 while(tfp
.Read((void *)&xcc
,1)) fxc
+= xcc
;
166 PXMLSettings
xmlcfg2(cfg
);
169 PAssert((xc2
== xc
),"Config not as expected");
171 cout
<< "*** Test Passed ***" << endl
;
174 // End of File ///////////////////////////////////////////////////////////////