aboutsummaryrefslogtreecommitdiff
path: root/doc/intro.xhtml
blob: 77ebf3f36d0b4a3f2a08814da09ca05155b3c7e1 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
<?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>XML Parsing and Serialization in C++ with libstudxml</title>

  <meta name="copyright" content="&copy; 2013-2014 Code Synthesis Tools CC"/>
  <meta name="keywords" content="xml,c++,parsing,serialization,api,streaming,persistence"/>
  <meta name="description" content="XML Parsing and Serialization in C++ with libstudxml"/>
  <meta name="revision" content="1.0"/>
  <meta name="version" content="1.1.0"/>

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

<style type="text/css">
  pre {
    padding    : 0 0 0 0em;
    margin     : 0em 0em 0em 0;

    font-size  : 102%
  }

  body {
    min-width: 48em;
  }

  h1 {
    font-weight: bold;
    font-size: 200%;
    line-height: 1.2em;
  }

  h2 {
    font-weight : bold;
    font-size   : 150%;

    padding-top : 0.8em;
  }

  h3 {
    font-size   : 140%;
    padding-top : 0.8em;
  }

  /* Force page break for both PDF and HTML (when printing). */
  hr.page-break {
    height: 0;
    width: 0;
    border: 0;
    visibility: hidden;

    page-break-after: always;
  }

  /* Adjust indentation for three levels. */
  #container {
    max-width: 48em;
  }

  #content {
    padding: 0 0.1em 0 4em;
    /*background-color: red;*/
  }

  #content h1 {
    margin-left: -2.06em;
  }

  #content h2 {
    margin-left: -1.33em;
  }

  /* Title page */

  #titlepage {
    padding: 2em 0 1em 0;
    border-bottom: 1px solid black;
  }

  #titlepage .title {
    font-weight: bold;
    font-size: 200%;
    text-align: center;
    padding: 1em 0 2em 0;
  }

  #titlepage #first-title {
    padding: 1em 0 0.4em 0;
  }

  #titlepage #second-title {
    padding: 0.4em 0 2em 0;
  }

  #titlepage p {
    padding-bottom: 1em;
  }

  #titlepage #revision {
    padding-bottom: 0em;
  }

  /* Lists */
  ul.list li, ol.list li {
    padding-top      : 0.3em;
    padding-bottom   : 0.3em;
  }

  div.img {
    text-align: center;
    padding: 2em 0 2em 0;
  }

  /*  */
  dl dt {
    padding   : 0.8em 0 0 0;
  }

  /* TOC */
  table.toc {
    border-style      : none;
    border-collapse   : separate;
    border-spacing    : 0;

    margin            : 0.2em 0 0.2em 0;
    padding           : 0 0 0 0;
  }

  table.toc tr {
    padding           : 0 0 0 0;
    margin            : 0 0 0 0;
  }

  table.toc * td, table.toc * th {
    border-style      : none;
    margin            : 0 0 0 0;
    vertical-align    : top;
  }

  table.toc * th {
    font-weight       : normal;
    padding           : 0em 0.1em 0em 0;
    text-align        : left;
    white-space       : nowrap;
  }

  table.toc * table.toc th {
    padding-left      : 1em;
  }

  table.toc * td {
    padding           : 0em 0 0em 0.7em;
    text-align        : left;
  }

</style>


</head>

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

  <div class="noprint">

  <div id="titlepage">
    <div class="title" id="first-title">XML Parsing and Serialization in C++</div>
    <div class="title" id="second-title">With <code>libstudxml</code></div>

    <p>Copyright &copy; 2013-2014 Code Synthesis Tools CC. Permission is
       granted to copy, distribute and/or modify this document under the
       terms of the MIT license.</p>

    <!-- REMEMBER TO CHANGE VERSIONS IN THE META TAGS ABOVE! -->
    <p id="revision">Revision 1.0, May 2014</p>
    <p>This revision of the document describes <code>libstudxml</code> 1.1.0.</p>
  </div>

  <hr class="page-break"/>
  <h1>Table of Contents</h1>

  <table class="toc">
    <tr>
      <th></th><td><a href="#0">About This Document</a></td>
    </tr>
    <tr>
      <th>1</th><td><a href="#1">Terminology</a></td>
    </tr>
    <tr>
      <th>2</th><td><a href="#2">Low-Level API</a></td>
    </tr>
    <tr>
      <th>3</th><td><a href="#3">High-Level API</a></td>
    </tr>
    <tr>
      <th>4</th><td><a href="#4">Object Persistence</a></td>
    </tr>
    <tr>
      <th>5</th><td><a href="#5">Inheritance</a></td>
    </tr>
    <tr>
      <th>6</th><td><a href="#6">Implementation Notes</a></td>
    </tr>
  </table>
  </div>

  <hr class="page-break"/>
  <h1><a name="0">About This Document</a></h1>

  <p>This document is based on the presentation given by Boris Kolpackov at
     the C++Now 2014 conference where <code>libstudxml</code> was
     first made publicly available. Its goal is to introduce a new,
     modern C++ API for XML by showing how to handle the most common
     use cases. Compared to the talk, this introduction omits some of
     the discussion relevant to XML in general and its handling
     in C++. It also provides more complete code examples that would not
     fit onto slides during the presentation. If, however, you would
     like to get a more complete picture of the "state of XML in C++", then
     you may prefer to first
     <a href="http://youtu.be/AuamDUrG5ZU?list=UU5e__RG9K3cHrPotPABnrwg">watch
     the video</a> of the talk.</p>

  <p>While this document uses some C++11 features in the examples, the
     library itself can be used in C++98 applications as well.</p>

  <h1><a name="1">Terminology</a></h1>

  <p>Before we begin, let's define a few terms to make sure we are on
     the same page.</p>

  <p>When we say "XML format" that is a bit loose. XML is actually
     a meta-format that we specialize for our needs. That is, we decide
     what element and attribute names we will use, which elements will
     be valid where, what they will mean, and so on. This specialization
     of XML to a specific format is called an <em>XML Vocabulary</em>.</p>

  <p>Often, but not always, when we parse XML, we store extracted data
     in the application's memory. Usually, we would create classes
     specific to our XML vocabulary. For example, if we have an element
     called <code>person</code> then we may create a C++ class also
     called <code>person</code>. we will call such classes an
     <em>Object Model</em>.</p>

  <p>The content of an element in XML can be empty, text, nested
     elements, or a mixture of the two:</p>

  <pre class="xml">
&lt;empty name="a" id="1"/>

&lt;simple name="b" id="2">text&lt;simple/>

&lt;complex name="c" id="3">
  &lt;nested>...&lt;/nested>
  &lt;nested>...&lt;/nested>
&lt;complex/>

&lt;mixed name="d" id="4">
  te&lt;nested>...&lt;/nested>
  x
  &lt;nested>...&lt;/nested>t
&lt;mixed/>
  </pre>

  <p>These are called the <em>empty</em>, <em>simple</em>,
     <em>complex</em>, and <em>mixed</em> content models,
     respectively.</p>

  <h1><a name="2">Low-Level API</a></h1>

  <p><code>libstudxml</code> provides the streaming XML pull parser and
     streaming XML serializer. The parser is a conforming, non-validating
     XML 1.0 implementation (see <a href="#6">Implementation Notes</a>
     for details). The application character encoding (that is, the
     encoding used in the application's memory) for both parser and
     serializer is UTF-8. The output encoding of the serializer is
     UTF-8 as well. The parser supports UTF-8, UTF-16, ISO-8859-1,
     and US-ASCII input encodings.</p>

  <pre class="c++">
#include &lt;xml/parser>

namespace xml
{
  class parser;
}
  </pre>

  <pre class="c++">
#include &lt;xml/serializer>

namespace xml
{
  class serializer;
}
  </pre>

  <p>C++ is often used to implement XML converters and filters, especially
     where speed is a concern. Such applications require the lowest-level
     API with minimum overhead. So we will start there (see the
     <code>roundtrip</code> example in the <code>libstudxml</code>
     distribution).</p>

  <pre class="c++">
class parser
{
  typedef unsigned short feature_type;

  static const feature_type receive_elements;
  static const feature_type receive_characters;
  static const feature_type receive_attributes;
  static const feature_type receive_namespace_decls;

  static const feature_type receive_default =
    receive_elements |
    receive_characters |
    receive_attributes;

  parser (std::istream&amp;,
          const std::string&amp; input_name,
          feature_type = receive_default);
  ...
};
  </pre>

  <p>The parser constructor takes three arguments: the stream to parse,
     input name that is used in diagnostics to identify the document
     being parsed, and the list of events we want the parser to report.</p>

  <p>As an example of an XML filter, let's write one that removes a
     specific attribute from the document, say <code>id</code>. The
     first step in our filter would then be to create the parser
     instance:</p>

  <pre class="c++">
int main (int argc, char* argv[])
{
  ...

  try
  {
    using namespace xml;

    ifstream ifs (argv[1]);
    parser p (ifs, argv[1]);

    ...
  }
  catch (const xml::parsing&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>Here we also see how to handle parsing errors. So far so good.
     Let's see the next piece of the API.</p>

  <pre class="c++">
class parser
{
  enum event_type
  {
    start_element,
    end_element,
    start_attribute,
    end_attribute,
    characters,
    start_namespace_decl,
    end_namespace_decl,
    eof
  };

  event_type next ();
};
  </pre>

  <p>We call the <code>next()</code> function when we are ready to handle
     the next piece of XML. And now we can implement our filter a bit
     further:</p>

  <pre class="c++">
parser p (ifs, argv[1]);

for (parser::event_type e (p.next ());
     e != parser::eof;
     e = p.next ())
{
  switch (e)
  {
  case parser::start_element:
    ...
  case parser::end_element:
    ...
  case parser::start_attribute:
    ...
  case parser::end_attribute:
    ...
  case parser::characters:
    ...
  }
}
  </pre>

  <p>In C++11 we can use the range-based <code>for</code> loop to tidy
     things up a bit:</p>

  <pre class="c++">
parser p (ifs, argv[1]);

for (parser::event_type e: p)
{
  switch (e)
  {
    ...
  }
}
  </pre>

  <p>The next piece of the API puzzle:</p>

  <pre class="c++">
class parser
{
  const std::string&amp; name () const;
  const std::string&amp; value () const;

  unsigned long long line () const;
  unsigned long long column () const;
};
  </pre>

  <p>The <code>name()</code> accessor returns the name of the current element
     or attribute. The <code>value()</code> function returns the text of the
     characters event for an element or attribute. The <code>line()</code> and
     <code>column()</code> accessors return the current position in the document.
     Here is how we could print all the element positions for debugging:</p>

  <pre class="c++">
switch (e)
{
case parser::start_element:
  cerr &lt;&lt; p.line () &lt;&lt; ':' &lt;&lt; p.column () &lt;&lt; ": start "
       &lt;&lt; p.name () &lt;&lt; endl;
  break;
case parser::end_element:
  cerr &lt;&lt; p.line () &lt;&lt; ':' &lt;&lt; p.column () &lt;&lt; ": end "
       &lt;&lt; p.name () &lt;&lt; endl;
  break;
}
  </pre>

  <p>We have now seen enough of the parsing side to complete our filter.
     What's missing is the serialization. So let's switch to that for a
     moment:</p>

  <pre class="c++">
class serializer
{
  serializer (std::ostream&amp;,
              const std::string&amp; output_name,
              unsigned short indentation = 2);

  ...
};
  </pre>

  <p>The constructor is pretty similar to the <code>parser</code>'s. The
     <code>indentation</code> argument specifies the number of indentation
     spaces that should be used for pretty-printing. We can disable it by
     passing <code>0</code>.</p>

  <p>Now we can create the serializer instance for our filter:</p>

  <pre class="c++">
int main (int argc, char* argv[])
{
  ...

  try
  {
    using namespace xml;

    ifstream ifs (argv[1]);
    parser p (ifs, argv[1]);
    serializer s (cout, "output", 0);

    ...
  }
  catch (const xml::parsing&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
  catch (const xml::serialization&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>Notice that we have also added an exception handler for the
     <code>serialization</code> exception. Instead of handling
     the <code>parsing</code> and <code>serialization</code>
     exceptions separately, we can catch just
     <code>xml::exception</code>, which is a common base for the
     other two:</p>

  <pre class="c++">
int main (int argc, char* argv[])
{
  try
  {
    ...
  }
  catch (const xml::exception&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>The next chunk of the serializer API:</p>

  <pre class="c++">
class serializer
{
  void start_element (const std::string&amp; name);
  void end_element ();

  void start_attribute (const std::string&amp; name);
  void end_attribute ();

  void characters (const std::string&amp; value);
};
  </pre>

  <p>Everything should be pretty self-explanatory here. And we have
     now seen enough to finish our filter:</p>

  <pre class="c++">
parser p (ifs, argv[1]);
serializer s (cout, "output", 0);

bool skip (false);

for (parser::event_type e: p)
{
  switch (e)
  {
  case parser::start_element:
    {
      s.start_element (p.name ());
      break;
    }
  case parser::end_element:
    {
      s.end_element ();
      break;
    }
  case parser::start_attribute:
    {
      if (p.name () == "id")
        skip = true;
      else
        s.start_attribute (p.name ());
      break;
    }
  case parser::end_attribute:
    {
      if (skip)
        skip = false;
      else
        s.end_attribute ();
      break;
    }
  case parser::characters:
    {
      if (!skip)
        s.characters (p.value ());
      break;
    }
  }
}
  </pre>

  <p>Do you see any problems with our filter? Well, one problem is
     that this implementation doesn't handle XML namespaces. Let's
     see how we can fix this. The first issue is with the element
     and attribute names. When namespaces are used, those may be
     qualified. <code>libstudxml</code> uses the <code>qname</code>
     class to represent such names:</p>

  <pre class="c++">
#include &lt;xml/qname>

namespace xml
{
  class qname
  {
  public:
    qname ();
    qname (const std::string&amp; name);
    qname (const std::string&amp; namespace_,
           const std::string&amp; name);

    const std::string&amp; namespace_ () const;
    const std::string&amp; name () const;
  };
}
  </pre>

  <p>The parser, in addition to the <code>name()</code> accessor also
     has <code>qname()</code> which returns the potentially qualified
     name. Similarly, the <code>start_element()</code> and
     <code>start_attribute()</code> functions in the serializer are
     overloaded to accept <code>qname</code>:</p>

  <pre class="c++">
class parser
{
  const qname&amp; qname () const;
};

class serializer
{
  void start_element (const qname&amp;);
  void start_attribute (const qname&amp;);
};
  </pre>

  <p>The first thing we need to do to make our filter namespace-aware
     is to use qualified names instead of the local ones. This one is
     easy:</p>

  <pre class="c++">
switch (e)
{
case parser::start_element:
  {
    s.start_element (p.qname ());
    break;
  }
case parser::start_attribute:
  {
    if (p.qname () == "id") // Unqualified name.
      skip = true;
    else
      s.start_attribute (p.qname ());
    break;
  }
}
  </pre>


  <p>There is, however, another thing that we have to do. Right now our
     code does not propagate the namespace-prefix mappings from the input
     document to the output. At the moment, where the input XML might have
     meaningful prefixes assigned to namespaces, the output will have
     automatically generated ones like <code>g1</code>, <code>g2</code>,
     and so on.</p>

  <p>To fix this, first we need to tell the parser to report to us
     namespace-prefix mappings, called namespace declarations in XML:</p>

  <pre class="c++">
parser p (ifs,
          argv[1]
          parser::receive_default |
          parser::receive_namespace_decls);
  </pre>

  <p>We then also need to propagate this information to the serializer by
     handling the <code>start_namespace_decl</code> event:</p>

  <pre class="c++">
for (...)
{
  switch (e)
  {
    ...

  case parser::start_namespace_decl:
    s.namespace_decl (p.namespace_ (), p.prefix ());
    break;

    ...
  }
}
  </pre>

  <p>Well, that wasn't too bad.</p>

  <h1><a name="3">High-Level API</a></h1>

  <p>So that was pretty low level XML work where we didn't care about
     the semantics of the stored data, or, in fact the XML vocabulary that
     we dealt with.</p>

  <p>However, this API will quickly become tedious once we try to handle
     a specific XML vocabulary and do something useful with the stored
     data. Why is that? There are several areas where we could use some
     help:</p>

  <ul>
    <li>Validation and error handling</li>
    <li>Attribute access</li>
    <li>Data extraction</li>
    <li>Content model processing</li>
    <li>Control flow</li>
  </ul>

  <p>Let's examine each area using our object position vocabulary as a
     test case (see the <code>processing</code> example in the
     <code>libstudxml</code> distribution).</p>

  <pre class="xml">
&lt;object id="123">
  &lt;name>Lion's Head&lt;/name>
  &lt;type>mountain&lt;/type>

  &lt;position lat="-33.8569" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5082"/>
&lt;/object>
  </pre>

  <p>If you cannot assume the XML you are parsing is valid, and you
     generally shouldn't, then you will quickly realize that the biggest
     pain in dealing with XML is making sure that what we got is actually
     valid.</p>

  <p>This stuff is pervasive. What if the root element is spelled
     wrong? Maybe the <code>id</code> attribute is missing? Or there
     is some stray text before the <code>name</code> element? Things
     can be broken in an infinite number of ways.</p>

  <p>To illustrate this point, here is the parsing code of just the
     root element with proper error handling:</p>

  <pre class="c++">
parser p (ifs, argv[1]);

if (p.next () != parser::start_element ||
    p.qname () != "object")
{
  // error
}

...

if (p.next () != parser::end_element) // object
{
  // error
}
  </pre>

  <p>Not very pretty. To help with this, the parser API provides the
     <code>next_expect()</code> function:</p>

  <pre class="c++">
class parser
{
  void next_expect (event_type);
  void next_expect (event_type, const std::string&amp; name);
};
  </pre>

  <p>This function gets the next event and makes sure it is what's
     expected. If not, it throws an appropriate parsing exception.
     This simplifies our root element parsing quite a bit:</p>

  <pre class="c++">
parser p (ifs, argv[1]);

p.next_expect (parser::start_element, "object");
...
p.next_expect (parser::end_element); // object
  </pre>

  <p>Let's now take the next step and try to handle the <code>id</code>
     attribute. According to what we have seen so far, it will look
     something along these lines:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");

p.next_expect (parser::start_attribute, "id");
p.next_expect (parser::characters);
cout &lt;&lt; "id: " &lt;&lt; p.value () &lt;&lt; endl;
p.next_expect (parser::end_attribute);

...

p.next_expect (parser::end_element); // object
  </pre>

  <p>Not too bad but there is a bit of a problem. What if our <code>object</code>
     element had several attributes? The order of attributes in XML
     is arbitrary so we should be prepared to get them in any order.
     This fact complicates our attribute parsing code quite a bit:</p>

  <pre class="c++">
while (p.next () == parser::start_attribute)
{
  if (p.qname () == "id")
  {
    p.next_expect (parser::characters);
    cout &lt;&lt; "id: " &lt;&lt; p.value () &lt;&lt; endl;
  }
  else if (...)
  {
  }
  else
  {
    // error: unknown attribute
  }

  p.next_expect (parser::end_attribute);
}
  </pre>

  <p>There is also a bug in this version. Can you see it? We now
     don't make sure that the <code>id</code> attribute was actually
     specified.</p>

  <p>If you think about it, at this level, it is actually not that
     convenient to receive attributes as events. In fact, a map of
     attributes would be much more usable.</p>

  <p>Remember we talked about the parser features that specify which
     events we want to see:</p>

  <pre class="c++">
class parser
{
  static const feature_type receive_elements;
  static const feature_type receive_characters;
  static const feature_type receive_attributes;

  ...
};
  </pre>

  <p>Well, in reality, there is no <code>receive_attributes</code>. Rather,
     there are these two options:

  <pre class="c++">
class parser
{
  static const feature_type receive_attributes_map;
  static const feature_type receive_attributes_event;

  ...
};
  </pre>

  <p>That is, we can ask the parser to send us attributes as events or
     as a map. And the default is to send them as a map.</p>

  <p>In case of a map, we have the following attribute access API to work
     with:</p>

  <pre class="c++">
class parser
{
  const std::string&amp; attribute (const std::string&amp; name) const;

  std::string attribute (const std::string&amp; name,
                         const std::string&amp; default_value) const;

  bool attribute_present (const std::string&amp; name) const;
};
  </pre>

  <p>If the attribute is not found, then the version without the default
     value throws an appropriate parsing exception while the version with
     the default value returns that value. There are also the
     <code>qname</code> versions of these functions.</p>

  <p>Let's see how this simplifies our code:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");

cout &lt;&lt; "id: " &lt;&lt; p.attribute ("id") &lt;&lt; endl;

...

p.next_expect (parser::end_element); // object
  </pre>

  <p>Much better.</p>

  <p>If the <code>id</code> attribute is not present, then we get an
     exception. But what happens if we have a stray attribute in our
     document? The attribute map is magical in this sense. After
     the <code>end_element</code> event for the <code>object</code>
     element the parser will examine the attribute map. If there is
     an attribute that hasn't been retrieved with one of the attribute
     access functions, then the parser will throw the unexpected
     attribute exception.</p>

  <p>Error handling out of the way, the next thing that will annoy us is data
     extractions. In XML everything is text. While our <code>id</code> value
     is an integer, XML stores it as text and the low-level API returns it to
     us as text. To help with this the parser provides the following data
     extraction functions:</p>

  <pre class="c++">
class parser
{
  template &lt;typename T>
  T value () const;

  template &lt;typename T>
  T attribute (const std::string&amp; name) const;

  template &lt;typename T>
  T attribute (const std::string&amp; name,
               const T&amp; default_value) const;
};
  </pre>

  <p>Now we can get the <code>id</code> as an integer without much fuss:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");

unsigned int id = p.attribute&lt;unsigned int> ("id");

...

p.next_expect (parser::end_element); // object
  </pre>

  <p>Ok, let's try to parse our vocabulary a bit further:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");
unsigned int id = p.attribute&lt;unsigned int> ("id");

p.next_expect (parser::start_element, "name");

...

p.next_expect (parser::end_element); // name

p.next_expect (parser::end_element); // object
  </pre>

  <p>Here is the part of the document that we are parsing:</p>

  <pre class="xml">
&lt;object id="123">
  &lt;name>Lion's Head&lt;/name>
  </pre>

  <p>What do you think, is everything alright with our code? When we
     try to parse our document, we will get an exception here:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "name");
  </pre>

  <p>Any idea why? Let's try to print the event that we get:</p>

  <pre class="c++">
// p.next_expect (parser::start_element, "name");
cerr &lt;&lt; p.next () &lt;&lt; endl;
  </pre>

  <p>We expect <code>start_element</code> but get <code>characters</code>!
     Wait a minute, but there are characters after <code>object</code> and
     before <code>name</code>. There is a newline and two spaces that are
     replaced with hashes for illustration here:</p>

  <pre class="xml">
&lt;object id="123">#
##&lt;name>Lion's Head&lt;/name>
  </pre>

  <p>If you go to a forum or a mailing list for any XML parser, this will
     be the most common question. Why do I get text when I should clearly
     get an element!?</p>

  <p>The reason why we get this whitespace text is because the parser has no
     idea whether it is significant or not. The significance of whitespaces is
     determined by the XML content model that we talked about earlier. Here is
     the table:</p>

  <pre class="c++">
#include &lt;xml/content>

namespace xml
{
  enum class content
  {          //  element   characters  whitespaces
    empty,   //    no          no        ignored
    simple,  //    no          yes       preserved
    complex, //    yes         no        ignored
    mixed    //    yes         yes       preserved
  };
}
  </pre>

  <p>In empty content neither nested elements nor characters are allowed with
     whitespaces ignored. Simple content allows no nested elements with
     whitespaces preserved. Complex content allows nested elements only with
     whitespaces which are ignored. Finally, the mixed content allows anything
     in any order with everything preserved.</p>

  <p>If we specify the content model for an element, then the parser
     will do automatic whitespace processing for us:</p>

  <pre class="c++">
class parser
{
  void content (content);
};
  </pre>

  <p>That is, in empty and complex content, whitespaces will be silently
     ignored. By knowing the content model, the parser also has a chance to do
     more error handling for us. It will automatically throw appropriate
     exceptions if there are nested elements in empty or simple content or
     non-whitespace characters in complex content.</p>

  <p>Ok, let's now see how we can take advantage of this feature in
     our code:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");
p.content (content::complex);

unsigned int id = p.attribute&lt;unsigned int> ("id");

p.next_expect (parser::start_element, "name"); // Ok.

...

p.next_expect (parser::end_element); // name

p.next_expect (parser::end_element); // object
  </pre>

  <p>Now whitespaces are ignored and everything works as we expected.
     Here is how we can parse the content of the <code>name</code>
     element:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "name");
p.content (content::simple);

p.next_expect (parser::characters);
string name = p.value ();

p.next_expect (parser::end_element); // name
  </pre>

  <p>As you can see, parsing a simple content element is quite a bit more
     involved compared to getting a value of an attribute. Element markup also
     has a higher overhead in the resulting XML. That's why in our case it would
     have been wiser to make <code>name</code> and <code>type</code>
     attributes.</p>

  <p>But if we are stuck with a lot of simple content elements, then
     the parser provides the following helper functions:</p>

  <pre class="c++">
class parser
{
  std::string element ();

  template &lt;typename T>
  T element ();

  std::string element (const std::string&amp; name);

  template &lt;typename T>
  T element (const std::string&amp; name);

  std::string element (const std::string&amp; name,
                       const std::string&amp; default_value);

  template &lt;typename T>
  T element (const std::string&amp; name,
             const T&amp; default_value);
};
  </pre>

  <p>The first two assume that you have already handled the
     <code>start_element</code> event. They should be used if the element also
     has attributes. The other four parse the complete element. Overloaded
     <code>qname</code> versions are also provided.</p>

  <p>Here is how we can simplify our parsing code thanks to these
     functions:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");
p.content (content::complex);

unsigned int id = p.attribute&lt;unsigned int> ("id");
string name = p.element ("name");

p.next_expect (parser::end_element); // object
  </pre>

  <p>For the <code>type</code> element we would like to use this <code>enum
     class</code>:</p>

  <pre class="c++">
enum class object_type
{
  building,
  mountain,
  ...
};
  </pre>

  <p>The parsing code is similar to the <code>name</code> element. Now
     we use the data extracting version of the <code>element()</code>
     function:</p>

  <pre class="c++">
object_type type = p.element&lt;object_type> ("type");
  </pre>

  <p>Except that this won't compile. The parser doesn't know how to
     convert the text representation to our <code>enum.</code> By
     default the parser will try to use the <code>iostream</code>
     extraction operator but we haven't provided any.</p>

  <p>We can provide conversion code specifically for XML by specializing
     the <code>value_traits</code> class template:</p>

  <pre class="c++">
namespace xml
{
  template &lt;>
  struct value_traits&lt;object_type>
  {
    static object_type
    parse (std::string, const parser&amp;)
    {
      ...
    }

    static std::string
    serialize (object_type, const serializer&amp;)
    {
      ...
    }
  };
}
  </pre>

  <p>The last bit that we need to handle is the <code>position</code>
     elements. The interesting part here is how to stop without going
     too far since there can be several of them. To help with this task
     the parser allows us to peek into the next event:</p>

  <pre class="c++">
p.next_expect (parser::start_element, "object");
p.content (content::complex);
...

do
{
  p.next_expect (parser::start_element, "position");
  p.content (content::empty);

  float lat = p.attribute&lt;float> ("lat");
  float lon = p.attribute&lt;float> ("lon");

  p.next_expect (parser::end_element);

} while (p.peek () == parser::start_element);

p.next_expect (parser::end_element); // object
  </pre>

  <p>Do you see anything else that we can improve? Actually, there is
     one thing. Look at the <code>next_expect()</code> calls in the
     above code. They are both immediately followed by the setting
     of the content model. We can tidy this up a bit by passing the
     content model as a third argument to <code>next_expect()</code>.
     This even reads like prose: "Next we expect the start of an
     element called <code>position</code> that shall have empty
     content."</p>

  <p>Here is the complete, production-quality parsing code for our XML
     vocabulary. 13 lines. With validation and everything:</p>

  <pre class="c++">
parser p (ifs, argv[1]);

p.next_expect (parser::start_element, "object", content::complex);

unsigned int id = p.attribute&lt;unsigned int> ("id");
string name = p.element ("name");
object_type type = p.element&lt;object_type> ("type");

do
{
  p.next_expect (parser::start_element, "position", content::empty);

  float lat = p.attribute&lt;float> ("lat");
  float lon = p.attribute&lt;float> ("lon");

  p.next_expect (parser::end_element); // position
} while (p.peek () == parser::start_element)

p.next_expect (parser::end_element); // object
  </pre>

  <p>So that was the high-level parsing API. Let's now catch up with the
     corresponding additions to the serializer.</p>

  <p>Similar to parsing, calling <code>start_attribute()</code>,
     <code>characters()</code>, and then <code>end_attribute()</code>
     might not be convenient. Instead we can add an attribute with
     a single call:</p>

  <pre class="c++">
class serializer
{
  void attribute (const std::string&amp; name,
                  const std::string&amp; value);

  void element (const std::string&amp; value);

  void element (const std::string&amp; name,
                const std::string&amp; value);
};
  </pre>

  <p>The same works for elements with simple content. The first version finishes
     the element that we have started, while the second writes the complete
     element. There are also the <code>qname</code> versions of these
     functions that are not shown.</p>

  <p>Instead of strings we can also serialize value types. This uses the
     same <code>value_traits</code> specialization mechanism that we have
     used for parsing:</p>

  <pre class="c++">
class serializer
{
  template &lt;typename T>
  void attribute (const std::string&amp; name,
                  const T&amp; value);

  template &lt;typename T>
  void element (const T&amp; value);

  template &lt;typename T>
  void element (const std::string&amp; name,
                const T&amp; value);

  template &lt;typename T>
  void characters (const T&amp; value);
};
  </pre>

  <p>Let's now see now how we can serialize a complete sample document for
     our object position vocabulary using this high-level API:</p>

  <pre class="c++">
serializer s (cout, "output");

s.start_element ("object");

s.attribute ("id", 123);
s.element ("name", "Lion's Head");
s.element ("type", object_type::mountain);

for (...)
{
  s.start_element ("position");

  float lat (...), lon (...);

  s.attribute ("lat", lat);
  s.attribute ("lon", lon);

  s.end_element (); // position
}

s.end_element (); // object
  </pre>

  <p>Pretty straightforward stuff.</p>

  <h1><a name="4">Object Persistence</a></h1>

  <p>So far we have used our API to first implement a filter that doesn't
     really care about the data and then an application that processes the
     data without creating any kind of object model. Let's now try to handle
     the other end of the spectrum: objects that know how to persist
     themselves into XML (see the <code>persistence</code> example in
     the <code>libstudxml</code> distribution).</p>

  <p>But before we continue, let's fix our XML to be slightly more idiomatic.
     That is we make <code>name</code> and <code>type</code> to be attributes
     rather than elements:</p>

  <pre class="xml">
&lt;object name="Lion's Head" type="mountain" id="123">
  &lt;position lat="-33.8569" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5082"/>
&lt;/object>
  </pre>

  <p>Generally, the API works best with idiomatic XML and will nudge you
     gently in that direction with minor inconveniences.</p>

  <p>For this vocabulary, the object model might look like this:</p>

  <pre class="c++">
enum class object_type {...};

class position
{
  ...

  float lat_;
  float lon_;
};

class object
{
  ...

  std::string name_;
  object_type type_;
  unsigned int id_;
  std::vector&lt;position> positions_;
};
  </pre>

  <p>Here I omit sensible constructors, accessors and modifiers that our
     classes would probably have.</p>

  <p>Let me also mention that what I am going to show next is what I
     believe is the sensible structure for XML persistence using this
     API. But that doesn't mean it is the only way. For example, we
     are going to do parsing in a constructor:</p>

  <pre class="c++">
class position
{
  position (xml::parser&amp;);

  void
  serialize (xml::serializer&amp;) const;

  ...
};

class object
{
  object (xml::parser&amp;);

  void
  serialize (xml::serializer&amp;) const;

  ...
};
  </pre>

  <p>But you may prefer to first create an instance, say with the default
     constructor, and then have a separate function do the parsing.
     There is nothing wrong with this approach.</p>

  <p>Let's start with the <code>position</code> constructor. Here, we are
     immediately confronted with this choice: do we parse the start and end
     element events in position or expect our caller to handle them.</p>

  <p>I suggest that we let our caller do this. We may have different elements
     in our vocabulary that use the same <code>position</code> type. If we
     assume the element name in the constructor, then we won't be able to use
     the same class for all these elements. We will see the second advantage
     of this arrangement in a moment, when we deal with inheritance. But, if
     you have a simple model with one-to-one mapping between types and
     elements and no inheritance, then there is nothing wrong with going the
     other route.</p>

  <pre class="c++">
position::
position (parser&amp; p)
  : lat_ (p.attribute&lt;float> ("lat")),
    lon_ (p.attribute&lt;float> ("lon"))
{
  p.content (content::empty);
}
  </pre>

  <p>Ok, nice and clean so far. Let's look at the <code>object</code>
     constructor:</p>

  <pre class="c++">
object::
object (parser&amp; p)
  : name_ (p.attribute ("name")),
    type_ (p.attribute&lt;object_type> ("type")),
    id_ (p.attribute&lt;unsigned int> ("id"))
{
  p.content (content::complex);

  do
  {
    p.next_expect (parser::start_element, "position");
    positions_.push_back (position (p));
    p.next_expect (parser::end_element);

  } while (p.peek () == parser::start_element);
}
  </pre>

  <p>The only mildly interesting line here is where we call the position
     constructor to parse the content of the nested elements.</p>

  <p>Before we look into serialization, let me also mention one other
     thing. In our vocabulary all the attributes are required but it is
     quite common to have optional attributes. The API functions with
     default values make it really convenient to handle such attributes
     in the initializer lists.</p>

  <p>Let's say the <code>type</code> attribute is optional. Then we
     could do this:</p>

  <pre class="c++">
object::
object (parser&amp; p)
  : ...
    type_ (p.attribute ("type", object_type::other))
    ...
  </pre>

  <p>We use the same arrangement for serialization, that is, the
    containing object starts and ends the element allowing us to
    reuse the same type for different elements:</p>

  <pre class="c++">
void position::serialize (serializer&amp; s) const
{
  s.attribute ("lat", lat_);
  s.attribute ("lon", lon_);
}

void object::serialize (serializer&amp; s) const
{
  s.attribute ("name", name_);
  s.attribute ("type", type_);
  s.attribute ("id", id_);

  for (const auto&amp; p: positions_)
  {
    s.start_element ("position");
    p.serialize (s);
    s.end_element ();
  }
}
  </pre>

  <p>Ok, also nice and tidy.</p>

  There is one thing, however, that is not so nice: the start of
  the parser or serializer. Here is the code:</p>

  <pre class="c++">
parser p (ifs, argv[1]);
p.next_expect (parser::start_element, "object");
object o (p);
p.next_expect (parser::end_element);

serializer s (cout, "output");
s.start_element ("object");
o.serialize (s);
s.end_element ();
  </pre>

  <p>Remember, we made the caller responsible for handling the start and
    end of the element. This works beautifully inside the object model but
    not so much in the client code. What we would like to see instead
    is this:</p>

  <pre class="c++">
parser p (ifs, argv[1]);
object o (p);

serializer s (cout, "output");
o.serialize (s);
  </pre>

  <p>The main reason for choosing this structure was the ability to reuse the
     same type for different elements. The other reason was inheritance which
     we haven't gotten to yet. If we think about it, it is very unlikely for a
     class corresponding to the root of our vocabulary to also be used inside
     as a local element. I can't remember ever seeing a vocabulary like
     this.</p>

  <p>So what we can do here is make an exception: the root type of our
     object model handles the top-level element. Here is the parser:</p>

  <pre class="c++">
object::
object (parser&amp; p)
{
  p.next_expect (
    parser::start_element, "object", content::complex);

  name_ = p.attribute ("name");
  type_ = p.attribute&lt;object_type> ("type");
  id_ = p.attribute&lt;unsigned int> ("id");

  ...

  p.next_expect (parser::end_element);
}
  </pre>

  <p>And here is the serializer:</p>

  <pre class="c++">
void object::
serialize (serializer&amp; s) const
{
  s.start_element ("object");

  ...

  s.end_element ();
}
  </pre>

  <p>The only minor drawback of going this route is that we can no longer
     parse attributes in the initializer list for the root object.</p>

  <h1><a name="5">Inheritance</a></h1>

  <p>So far we have had a smooth sailing with the streaming approach but things get
     a bit bumpy once we start dealing with inheritance. This is normally
     where the in-memory approach has its day.</p>

  <p>Say we have <code>elevated-object</code> which adds the
     <code>units</code> attribute and the <code>elevation</code> elements.
     Here is the XML:</p>

  <pre class="xml">
&lt;elevated-object name="Lion's Head" type="mountain"
                 units="m" id="123">
  &lt;position lat="-33.8569" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5083"/>
  &lt;position lat="-33.8568" lon="18.5082"/>

  &lt;elevation val="668.9"/>
  &lt;elevation val="669"/>
  &lt;elevation val="669.1"/>
&lt;/elevated-object>
  </pre>

  <p>And here is the object model:</p>

  <pre class="c++">
enum class units {...};

class elevation {...};

class elevated_object: public object
{
  ...

  units units_;
  std::vector&lt;elevation> elevations_;
};
  </pre>

  <p>Streaming assumes linearity. We start an element, add some attributes,
     add some nested elements, and end the element.  In contrast, with an
     in-memory approach we can add some attributes, then add some nested
     elements, then go back and add more attributes. This kind of back and
     forth is exactly what inheritance often requires. So this is a bit of
     problem for us.</p>

  <p>Consider the <code>elevated_object</code> constructor:</p>

  <pre class="c++">
elevated_object::
elevated_object (parser&amp; p)
  : object (p),
    units_ (p.attribute&lt;units> ("units"))
{
  do
  {
    p.next_expect (parser::start_element, "elevation");
    elevations_.push_back (elevation (p));
    p.next_expect (parser::end_element);

  } while (p.peek () == parser::start_element &amp;&amp;
           p.name () == "elevation")
}
  </pre>

  <p>Note that here I assume we went back to our original architecture
     where the caller handles the start and end of the element (this is
     the other advantage of this architecture: it allows us to reuse
     base parsing and serialization code in derived classes).</p>

  <p>So we would like to reuse the parsing code from <code>object</code>
     so we call the base constructor first.</p>

  <p>Then we parse the derived attribute and elements. Do you see
     the problem? The <code>object</code> constructor will parse its
     attributes and then move on to nested elements. When this constructor
     returns, we need to go back to parsing attributes! This is not
     something that a streaming approach would normally allow.</p>

  <p>To resolve this, the lifetime of the attribute map was extended until
     after the <code>end_element</code> event. That is, we can access
     attributes any time we are at the element's level. As a result,
     the above code just works.</p>

  <p>We have the same problem in serialization. Let's say we write
     the straightforward code like this:</p>

  <pre class="c++">
void elevated_object::
serialize (serializer&amp; s) const
{
  object::serialize (s);

  s.attribute ("units", units_);

  for (const auto&amp; e: elevations_)
  {
    s.start_element ("elevation");
    e.serialize (s);
    s.end_element ();
  }
}
  </pre>

  <p>This is not going to work since we will try to add the <code>units</code>
     attribute after the nested <code>position</code> elements have already
     been written.</p>

  <p>To handle inheritance in serialization we have to split the
     <code>serialize()</code> function into two. One serializes
     the attributes while the other &mdash; content:</p>

  <pre class="c++">
void object::
serialize_attributes (serializer&amp; s) const
{
  s.attribute ("name", name_);
  s.attribute ("type", type_);
  s.attribute ("id", id_);
}

void object::
serialize_content (serializer&amp; s) const
{
  for (const auto&amp; p: positions_)
  {
    s.start_element ("position");
    p.serialize (s);
    s.end_element ();
  }
}
  </pre>

  <p>The <code>serialize()</code> function then simply calls these two
     in the correct order.</p>

  <pre class="c++">
void object::
serialize (serializer&amp; s) const
{
  serialize_attributes (s);
  serialize_content (s);
}
  </pre>

  <p>I bet you can guess what the <code>elevated_object</code>'s
     implementation looks like:</p>

  <pre class="c++">
void elevated_object::
serialize_attributes (serializer&amp; s) const
{
  object::serialize_attributes (s);
  s.attribute ("units", units_);
}

void elevated_object::
serialize_content (serializer&amp; s) const
{
  object::serialize_content (s);

  for (const auto&amp; e: elevations_)
  {
    s.start_element ("elevation");
    e.serialize (s);
    s.end_element ();
  }
}
  </pre>

  <p>The <code>serialize()</code> function for <code>elevated_object</code>
     is exactly the same:</p>

  <pre class="c++">
void elevated_object::
serialize (serializer&amp; s) const
{
  serialize_attributes (s);
  serialize_content (s);
}
  </pre>

  <h1><a name="6">Implementation Notes</a></h1>

  <p><code>libstudxml</code>is an open source (MIT license), portable
     (autotools and VC++ projects provided), and external dependency-free
     implementation.</p>

  <p>It provides a conforming, non-validating XML 1.0 parser by using
     the mature and tested Expat XML parser. <code>libstudxml</code>
     includes the Expat source code (also distributed under the MIT
     license) as an implementation detail. However, you can link to
     an external Expat library if you prefer.</p>

  <p>If you are familiar with Expat, you are probably wondering how
     the push interface provided by Expat was adapted to the pull
     API shown earlier. Expat allows us to suspend and resume parsing
     after every event and that's exactly what this implementation
     does. The performance cost of this constant suspension and
     resumption is about 35% of Expat's performance, which is not
     negligible but not the end of the world either.</p>

  <p>All in, with all the name splitting and string constructions,
     parsing throughput on a 2010 Intel Core i7 laptop is about
     37 MByte/sec, which should be sufficient for most applications.</p>

  <p>While it is much easier to implement a conforming serializer
     from scratch, <code>libstudxml</code> reuses an existing and
     tested implementation in this case as well. It includes source
     code of a small C library for XML serialization called Genx
     (also MIT licensed) that was initially created by Tim Bray
     and significantly improved and extended over the past years
     as part of the XSD/e project.</p>

  </div>
</div>

</body>
</html>