source: icXML/icXML-devel/samples/src/CreateDOMDocument/CreateDOMDocument.cpp @ 2726

Last change on this file since 2726 was 2726, checked in by cameron, 6 years ago

Add original Xerces tests and samples directories

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