1 | /*
|
---|
2 | * Copyright 1999-2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Id: CreateDOMDocument.cpp,v 1.20 2004/09/08 13:55:30 peiyongz Exp $
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * This sample illustrates how you can create a DOM tree in memory.
|
---|
23 | * It then prints the count of elements in the tree.
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | // ---------------------------------------------------------------------------
|
---|
28 | // Includes
|
---|
29 | // ---------------------------------------------------------------------------
|
---|
30 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
31 | #include <xercesc/util/XMLString.hpp>
|
---|
32 | #include <xercesc/dom/DOM.hpp>
|
---|
33 | #if defined(XERCES_NEW_IOSTREAMS)
|
---|
34 | #include <iostream>
|
---|
35 | #else
|
---|
36 | #include <iostream.h>
|
---|
37 | #endif
|
---|
38 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
39 |
|
---|
40 | XERCES_CPP_NAMESPACE_USE
|
---|
41 |
|
---|
42 | // ---------------------------------------------------------------------------
|
---|
43 | // This is a simple class that lets us do easy (though not terribly efficient)
|
---|
44 | // trancoding of char* data to XMLCh data.
|
---|
45 | // ---------------------------------------------------------------------------
|
---|
46 | class XStr
|
---|
47 | {
|
---|
48 | public :
|
---|
49 | // -----------------------------------------------------------------------
|
---|
50 | // Constructors and Destructor
|
---|
51 | // -----------------------------------------------------------------------
|
---|
52 | XStr(const char* const toTranscode)
|
---|
53 | {
|
---|
54 | // Call the private transcoding method
|
---|
55 | fUnicodeForm = XMLString::transcode(toTranscode);
|
---|
56 | }
|
---|
57 |
|
---|
58 | ~XStr()
|
---|
59 | {
|
---|
60 | XMLString::release(&fUnicodeForm);
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | // -----------------------------------------------------------------------
|
---|
65 | // Getter methods
|
---|
66 | // -----------------------------------------------------------------------
|
---|
67 | const XMLCh* unicodeForm() const
|
---|
68 | {
|
---|
69 | return fUnicodeForm;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private :
|
---|
73 | // -----------------------------------------------------------------------
|
---|
74 | // Private data members
|
---|
75 | //
|
---|
76 | // fUnicodeForm
|
---|
77 | // This is the Unicode XMLCh format of the string.
|
---|
78 | // -----------------------------------------------------------------------
|
---|
79 | XMLCh* fUnicodeForm;
|
---|
80 | };
|
---|
81 |
|
---|
82 | #define X(str) XStr(str).unicodeForm()
|
---|
83 |
|
---|
84 |
|
---|
85 | // ---------------------------------------------------------------------------
|
---|
86 | // main
|
---|
87 | // ---------------------------------------------------------------------------
|
---|
88 |
|
---|
89 | int main(int argC, char* argV[])
|
---|
90 | {
|
---|
91 | // Initialize the XML4C2 system.
|
---|
92 | try
|
---|
93 | {
|
---|
94 | XMLPlatformUtils::Initialize();
|
---|
95 | }
|
---|
96 |
|
---|
97 | catch(const XMLException& toCatch)
|
---|
98 | {
|
---|
99 | char *pMsg = XMLString::transcode(toCatch.getMessage());
|
---|
100 | XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
|
---|
101 | << " Exception message:"
|
---|
102 | << pMsg;
|
---|
103 | XMLString::release(&pMsg);
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // Watch for special case help request
|
---|
108 | int errorCode = 0;
|
---|
109 | if (argC > 1)
|
---|
110 | {
|
---|
111 | XERCES_STD_QUALIFIER cout << "\nUsage:\n"
|
---|
112 | " CreateDOMDocument\n\n"
|
---|
113 | "This program creates a new DOM document from scratch in memory.\n"
|
---|
114 | "It then prints the count of elements in the tree.\n"
|
---|
115 | << XERCES_STD_QUALIFIER endl;
|
---|
116 | errorCode = 1;
|
---|
117 | }
|
---|
118 | if(errorCode) {
|
---|
119 | XMLPlatformUtils::Terminate();
|
---|
120 | return errorCode;
|
---|
121 | }
|
---|
122 |
|
---|
123 | {
|
---|
124 | // Nest entire test in an inner block.
|
---|
125 | // The tree we create below is the same that the XercesDOMParser would
|
---|
126 | // have created, except that no whitespace text nodes would be created.
|
---|
127 |
|
---|
128 | // <company>
|
---|
129 | // <product>Xerces-C</product>
|
---|
130 | // <category idea='great'>XML Parsing Tools</category>
|
---|
131 | // <developedBy>Apache Software Foundation</developedBy>
|
---|
132 | // </company>
|
---|
133 |
|
---|
134 | DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));
|
---|
135 |
|
---|
136 | if (impl != NULL)
|
---|
137 | {
|
---|
138 | try
|
---|
139 | {
|
---|
140 | DOMDocument* doc = impl->createDocument(
|
---|
141 | 0, // root element namespace URI.
|
---|
142 | X("company"), // root element name
|
---|
143 | 0); // document type object (DTD).
|
---|
144 |
|
---|
145 | DOMElement* rootElem = doc->getDocumentElement();
|
---|
146 |
|
---|
147 | DOMElement* prodElem = doc->createElement(X("product"));
|
---|
148 | rootElem->appendChild(prodElem);
|
---|
149 |
|
---|
150 | DOMText* prodDataVal = doc->createTextNode(X("Xerces-C"));
|
---|
151 | prodElem->appendChild(prodDataVal);
|
---|
152 |
|
---|
153 | DOMElement* catElem = doc->createElement(X("category"));
|
---|
154 | rootElem->appendChild(catElem);
|
---|
155 |
|
---|
156 | catElem->setAttribute(X("idea"), X("great"));
|
---|
157 |
|
---|
158 | DOMText* catDataVal = doc->createTextNode(X("XML Parsing Tools"));
|
---|
159 | catElem->appendChild(catDataVal);
|
---|
160 |
|
---|
161 | DOMElement* devByElem = doc->createElement(X("developedBy"));
|
---|
162 | rootElem->appendChild(devByElem);
|
---|
163 |
|
---|
164 | DOMText* devByDataVal = doc->createTextNode(X("Apache Software Foundation"));
|
---|
165 | devByElem->appendChild(devByDataVal);
|
---|
166 |
|
---|
167 | //
|
---|
168 | // Now count the number of elements in the above DOM tree.
|
---|
169 | //
|
---|
170 |
|
---|
171 | unsigned int elementCount = doc->getElementsByTagName(X("*"))->getLength();
|
---|
172 | XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
|
---|
173 | << " elements." << XERCES_STD_QUALIFIER endl;
|
---|
174 |
|
---|
175 | doc->release();
|
---|
176 | }
|
---|
177 | catch (const OutOfMemoryException&)
|
---|
178 | {
|
---|
179 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
180 | errorCode = 5;
|
---|
181 | }
|
---|
182 | catch (const DOMException& e)
|
---|
183 | {
|
---|
184 | XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
|
---|
185 | errorCode = 2;
|
---|
186 | }
|
---|
187 | catch (...)
|
---|
188 | {
|
---|
189 | XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
|
---|
190 | errorCode = 3;
|
---|
191 | }
|
---|
192 | } // (inpl != NULL)
|
---|
193 | else
|
---|
194 | {
|
---|
195 | XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
|
---|
196 | errorCode = 4;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | XMLPlatformUtils::Terminate();
|
---|
201 | return errorCode;
|
---|
202 | }
|
---|
203 |
|
---|