aboutsummaryrefslogtreecommitdiff
path: root/documentation/cxx/tree/dbxml/index.xhtml
blob: adfeb246c625b3dd82fe4233a96f14befc678dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
  <title>C++/Tree Mapping and Berkeley DB XML Integration Guide</title>

  <meta name="copyright" content="&copy; 2006-2010 Code Synthesis Tools CC"/>
  <meta name="keywords" content="xsd,xml,schema,c++,mapping,data,binding,berkeley,db,dbxml"/>
  <meta name="description" content="C++/Tree Mapping and Berkeley DB XML Integration Guide"/>

  <link rel="stylesheet" type="text/css" href="../../../default.css" />

<style type="text/css">
  pre {
    background : #cde8f6;

    padding    : 0 0 0 1em;
    margin     : 2em 0em 2em 0;

  }

  h1 {padding-top: 1em;}

</style>
</head>

<body>
<div id="container">
  <div id="content">

  <h1>Introduction</h1>

  <p>This guide shows how to integrate the
     <a href="http://www.codesynthesis.com/products/xsd/c++/tree/">C++/Tree</a>
     mapping generated by
     <a href="http://www.codesynthesis.com/products/xsd/">CodeSynthesis XSD</a>
     with
     <a href="http://www.oracle.com/database/berkeley-db/xml/index.html">Berkeley DB XML</a>.

     Berkeley DB XML is an embedded XML database which allows efficient
     storage and query of XML instance documents.

     CodeSynthesis XSD is a W3C XML Schema to C++ data binding compiler.

     The C++/Tree mapping allows you to manipulate the data stored in XML
     using objects that semantically correspond to your application
     domain rather than dealing with direct representations of XML.

     For an introduction to
     the Berkeley DB XML refer to the
     <a href="http://www.oracle.com/database/berkeley-db/xml/index.html">Berkeley DB XML
     Getting Started Guide</a>.
     For an introduction to the C++/Tree mapping refer to
     the <a href="../guide/">C++/Tree Mapping Getting Started Guide</a>.
  </p>


  <p>This guide describes the following four operations:</p>

  <ul>
    <li>Create a new document in DB from an object model</li>
    <li>Create an object model from a document in DB</li>
    <li>Create an object model from a document fragment in DB</li>
    <li>Update a document fragment in DB from an object model</li>
  </ul>

  <p>Our examples will be based on simple XML for book library. The XML
     Schema definition for the library is in
     <a href="./library.xsd">library.xsd</a> and is compiled by XSD
     to obtain <code>library.hxx</code> and <code>library.cxx</code>.
     All C++ code fragments that are presented in this guide are available
     as a single program in <a href="./driver.cxx">driver.cxx</a>.
     The complete example is available in the
     <code>examples/cxx/tree/dbxml</code> directory of the XSD distribution.</p>

  <p>Note that due to the incomplete DOM API implementation provided by DB
     XML (as of version 2.3.10), the generated code and your application
     should be compiled with the <code>DBXML_DOM</code> macro defined in
     order to avoid using unsupported parts of the API.
  </p>

  <p>A sample XML document for the library schema is presented below:</p>

  <pre>
&lt;lib:catalog xmlns:lib="http://www.codesynthesis.com/library">
  &lt;book available="true" id="ES">
    &lt;isbn>20530902&lt;/isbn>
    &lt;title>The Elements of Style&lt;/title>
    &lt;genre>reference&lt;/genre>

    &lt;author>
      &lt;name>William Strunk, Jr.&lt;/name>
      &lt;born>1869-07-01&lt;/born>
      &lt;died>1946-09-26&lt;/died>
    &lt;/author>

    &lt;author>
      &lt;name>E.B. White&lt;/name>
      &lt;born>1899-07-11&lt;/born>
      &lt;died>1985-10-01&lt;/died>
    &lt;/author>
  &lt;/book>
&lt;/lib:catalog>
  </pre>

  <h1>Create Document from Object Model</h1>

  <p>In this step, we will programmatically create a book catalog
     with one book, save it into an <code>XmlDocument</code> object
     using one of the serialization functions generated by XSD (
     <code>catalog_</code> in our case), and store the
     <code>XmlDocument</code> object as a new document in the
     DB container:</p>

  <pre>
XmlManager manager;
XmlContainer container (manager.createContainer ("new.bdbxml"));
XmlUpdateContext update_context (manager.createUpdateContext ());
XmlQueryContext context (manager.createQueryContext ());
context.setNamespace ("lib", "http://www.codesynthesis.com/library");

// Create a new catalog with one book.
//
catalog c;

book b (20530902,                        // ISBN
        title ("The Elements of Style"), // Title
        genre::reference,                // Genre
        "ES");                           // ID

author strunk ("William Strunk, Jr.", date (1869, 7, 1));
strunk.died (date (1946, 9, 26));

b.author ().push_back (strunk);
c.book ().push_back (b);

// Create a new XML document.
//
XmlDocument doc (manager.createDocument ());
doc.setName ("new.xml");

// Obtain its DOM representation and add the root element.
//
xercesc::DOMDocument&amp; dom_doc (*doc.getContentAsDOM ());

dom_doc.appendChild (
  dom_doc.createElementNS (
    xml::string ("http://www.codesynthesis.com/library").c_str (),
    xml::string ("lib:catalog").c_str ()));

// Serialize the object model to the XML document. Also avoid
// re-initializing the Xerces-C++ runtime since XmlManager has
// it initialized.
//
catalog_ (dom_doc, c, xml_schema::flags::dont_initialize);

// Place the document into the container.
//
container.putDocument (doc, update_context);
  </pre>

  <p>If we now resolve the <code>new.xml</code> in the container and
     print its content, we will get:</p>

  <pre>
&lt;lib:catalog xmlns:lib="http://www.codesynthesis.com/library">
  &lt;book available="true" id="ES">
    &lt;isbn>20530902&lt;/isbn>
    &lt;title>The Elements of Style&lt;/title>
    &lt;genre>reference&lt;/genre>
    &lt;author>
      &lt;name>William Strunk, Jr.&lt;/name>
      &lt;born>1869-07-01&lt;/born>
      &lt;died>1946-09-26&lt;/died>
    &lt;/author>
  &lt;/book>
&lt;/lib:catalog>
  </pre>

  <h1>Create Object Model from Document</h1>

  <p>Creating an object model from a document is a matter
     of obtaining <code>XmlDocument</code> object and passing its DOM
     representation to one of the parsing functions generated by XSD
     (<code>catalog_</code> in our case):
  </p>

  <pre>
// Resolve the document in the container.
//
XmlDocument doc (container.getDocument ("new.xml"));

// Create the object model from the document's DOM. Also avoid
// re-initializing the Xerces-C++ runtime since XmlManager has
// it initialized.
//
auto_ptr&lt;catalog> c (catalog_ (*doc.getContentAsDOM (),
                               xml_schema::flags::dont_initialize));

cerr &lt;&lt; *c &lt;&lt; endl;
  </pre>

  <p>This code fragment prints:</p>

  <pre>
book:
isbn: 20530902
title: The Elements of Style
genre: reference
author:
name: William Strunk, Jr.
born: 1869-07-01
died: 1946-09-26
available: 1
id: ES
  </pre>

  <h1>Create Object Model from Document Fragment</h1>

  <p>The following code fragment looks up the book with id <code>"ES"</code>
     using XQuery. It then creates a <code>book</code> object from the
     resulting <code>XmlValue</code>:</p>

  <pre>
string query ("collection('new.bdbxml')/lib:catalog/book[@id='ES']");

// Find "The Elements of Style".
//
XmlValue v;
XmlResults results (manager.query (query, context));

if (results.next (v))
{
  // Create an object model from the document fragment.
  //
  auto_ptr&lt;book> b (
    new book (
      *static_cast&lt;xercesc::DOMElement*> (v.asNode ())));

  cerr &lt;&lt; *b &lt;&lt; endl;
}
  </pre>

<p>This code fragment prints:</p>

  <pre>
isbn: 20530902
title: The Elements of Style
genre: reference
author:
name: William Strunk, Jr.
born: 1869-07-01
died: 1946-09-26
available: 1
id: ES
  </pre>

  <p>Note that we had to perform a <code>static_cast</code> from
     <code>xercesc::DOMNode</code>
     returned by the <code>XmlValue::asNode</code> member function to
     <code>xercesc::DOMElement</code>. This is safe since we know
     that in our schema books are represented as XML elements.</p>

  <h1>Update Document Fragment from Object Model</h1>

  <p>Analogous to the create case, the following code fragment looks
     up the book with id <code>"ES"</code> using XQuery. It then creates
     a <code>book</code> object from the resulting <code>XmlValue</code>,
     adds another author, changes the availability status, and saves
     the changes back to the <code>XmlValue</code> object:
  </p>

  <pre>
string query ("collection('new.bdbxml')/lib:catalog/book[@id='ES']");

// Find "The Elements of Style".
//
XmlValue v;
XmlResults results (manager.query (query, context));

if (results.next (v))
{
  // Create an object model from the document fragment.
  //
  auto_ptr&lt;book> b (
    new book (
      *static_cast&lt;xercesc::DOMElement*> (v.asNode ())));

  // Add another author, change the availability status.
  //
  author white ("E.B. White", date (1899, 7, 11));
  white.died (date (1985, 10, 1));

  b->author ().push_back (white);
  b->available (false);

  // Update the document fragment from the object model.
  //
  *static_cast&lt;xercesc::DOMElement*> (v.asNode ()) &lt;&lt; *b;

  // Update the document in the container.
  //
  XmlDocument doc (v.asDocument ());
  container.updateDocument (doc, update_context);
}
  </pre>

  <p>If we now resolve the <code>new.xml</code> in the container and
     print its content, we will get:</p>

  <pre>
&lt;lib:catalog xmlns:lib="http://www.codesynthesis.com/library">
  &lt;book available="false" id="ES">
    &lt;isbn>20530902&lt;/isbn>
    &lt;title>The Elements of Style&lt;/title>
    &lt;genre>reference&lt;/genre>
    &lt;author>
      &lt;name>William Strunk, Jr.&lt;/name>
      &lt;born>1869-07-01&lt;/born>
      &lt;died>1946-09-26&lt;/died>
    &lt;/author>
    &lt;author>
      &lt;name>E.B. White&lt;/name>
      &lt;born>1899-07-11&lt;/born>
      &lt;died>1985-10-01&lt;/died>
    &lt;/author>
  &lt;/book>
&lt;/lib:catalog>
  </pre>

  </div>
  <div id="footer">
    &copy;2006-2010 <a href="http://www.codesynthesis.com">CODE SYNTHESIS TOOLS CC</a>

    <div id="terms">
      Permission is granted to copy, distribute and/or modify this
      document under the terms of the
      <a href="http://www.codesynthesis.com/licenses/fdl-1.2.txt">GNU Free
      Documentation License, version 1.2</a>; with no Invariant Sections,
      no Front-Cover Texts and no Back-Cover Texts.
    </div>
  </div>

</div>

</body>
</html>