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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<!-- Copyright 1999,2000 Clark Cooper <coopercc@netheaven.com>
All rights reserved.
This is free software. You may distribute or modify according to
the terms of the MIT/X License -->
<title>Expat XML Parser</title>
<meta name="author" content="Clark Cooper, coopercc@netheaven.com">
<meta http-equiv="Content-Style-Type" content="text/css">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Expat XML Parser</h1>
<p>Expat is a library, written in C, for parsing XML documents. It's the
underlying XML parser for the open source Mozilla project, perl's
XML::Parser, and other open-source XML parsers.</p>
<p>This library is the creation of James Clark, who's also given us
groff (an nroff look-alike), Jade (an implemention of ISO's DSSSL stylesheet
language for SGML), XP (a Java XML parser package), XT (a Java XSL engine).
James was also the technical lead on the XML Working Group at W3 that produced
the XML specification.</p>
<p>This is free software, licensed under the
<a href="../COPYING">MIT/X Consortium license</a>. You may download it from
<a href="http://expat.sourceforge.net">the expat homepage on Source Forge</a>.
</p>
<p>The bulk of this document was originally commissioned as an article by
<a href="http://www.xml.com/">XML.com</a>. They graciously allowed me to retain
copyright and to distribute it with expat.
<hr>
<h2>Table of Contents</h2>
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#building">Building and Installing</a></li>
<li><a href="#using">Using expat</a></li>
<li><a href="#examples">Some examples</a></li>
<li><a href="#reference">Reference</a>
<ul>
<li><a href="#creation">Parser Creation Functions</a>
<ul>
<li><a href="#XML_ParserCreate">XML_ParserCreate</a></li>
<li><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></li>
<li><a href="#XML_ParserCreate_MM">XML_ParserCreate_MM</a></li>
<li><a href="#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></li>
<li><a href="#XML_ParserFree">XML_ParserFree</a></li>
</ul>
</li>
<li><a href="#parsing">Parsing Functions</a>
<ul>
<li><a href="#XML_Parse">XML_Parse</a></li>
<li><a href="#XML_ParseBuffer">XML_ParseBuffer</a></li>
<li><a href="#XML_GetBuffer">XML_GetBuffer</a></li>
</ul>
</li>
<li><a href="#setting">Handler Setting Functions</a>
<ul>
<li><a href="#XML_SetStartElementHandler">XML_SetStartElementHandler</a></li>
<li><a href="#XML_SetEndElementHandler">XML_SetEndElementHandler</a></li>
<li><a href="#XML_SetElementHandler">XML_SetElementHandler</a></li>
<li><a href="#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></li>
<li><a href="#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</a></li>
<li><a href="#XML_SetCommentHandler">XML_SetCommentHandler</a></li>
<li><a href="#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</a></li>
<li><a href="#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</a></li>
<li><a href="#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</a></li>
<li><a href="#XML_SetDefaultHandler">XML_SetDefaultHandler</a></li>
<li><a href="#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a></li>
<li><a href="#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></li>
<li><a href="#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</a></li>
<li><a href="#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</a></li>
<li><a href="#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</a></li>
<li><a href="#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a></li>
<li><a href="#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a></li>
<li><a href="#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</a></li>
<li><a href="#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</a></li>
<li><a href="#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</a></li>
<li><a href="#XML_SetElementDeclHandler">XML_SetElementDeclHandler</a></li>
<li><a href="#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</a></li>
<li><a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a></li>
<li><a href="#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</a></li>
<li><a href="#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</a></li>
<li><a href="#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</a></li>
</ul>
</li>
<li><a href="#position">Parse Position and Error Reporting Functions</a>
<ul>
<li><a href="#XML_GetErrorCode">XML_GetErrorCode</a></li>
<li><a href="#XML_ErrorString">XML_ErrorString</a></li>
<li><a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a></li>
<li><a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></li>
<li><a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></li>
<li><a href="#XML_GetCurrentByteCount">XML_GetCurrentByteCount</a></li>
<li><a href="#XML_GetInputContext">XML_GetInputContext</a></li>
</ul>
</li>
<li><a href="#miscellaneous">Miscellaneous Functions</a>
<ul>
<li><a href="#XML_SetUserData">XML_SetUserData</a></li>
<li><a href="#XML_GetUserData">XML_GetUserData</a></li>
<li><a href="#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</a></li>
<li><a href="#XML_SetBase">XML_SetBase</a></li>
<li><a href="#XML_GetBase">XML_GetBase</a></li>
<li><a href="#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</a></li>
<li><a href="#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</a></li>
<li><a href="#XML_SetEncoding">XML_SetEncoding</a></li>
<li><a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></li>
<li><a href="#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a></li>
<li><a href="#XML_ExpatVersion">XML_ExpatVersion</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<hr>
<h2><a name="overview">Overview</a></h2>
<p>Expat is a stream-oriented parser. You register callback (or handler)
functions with
the parser and then start feeding it the document.
As the parser recognizes parts of
the document, it will call the appropriate handler for that part (if you've
registered one.) The document is fed to the parser in pieces, so you can
start parsing before you have all the document. This also allows you to
parse really huge documents that won't fit into memory.</p>
<p>Expat can be intimidating due to the many kinds of handlers and options
you can set. But you only need to learn four functions in order to do 90%
of what you'll want to do with it:</p>
<dl>
<dt><code>XML_ParserCreate</code></dt>
<dd>Create a new parser object.</dd>
<dt><code>XML_SetElementHandler</code></dt>
<dd>Set handlers for start and end tags.</dd>
<dt><code>XML_SetCharacterDataHandler</code></dt>
<dd>Set handler for text.</dd>
<dt><code>XML_Parse</code></dt>
<dd>Pass a buffer full of document to the parser</dd>
</dl>
<p>These functions and others are described in the
<a href="#reference">reference</a> part of this document. The reference
section also describes in detail the parameters passed to the different
types of handlers.
<p>Let's look at a very simple example program that only uses 3 of the above
functions (it doesn't need to set a character handler.) The program
<a href="../examples/outline.c">outline.c</a>
prints an element outline, indenting child elements to distinguish them from
the parent element that contains them. The start handler does all the work.
It prints two indenting spaces for every level of ancestor elements, then
it prints the element and attribute information. Finally it increments the
global Depth variable.
<div class="eg">
<pre>
int Depth;
void
start(void *data, const char *el, const char **attr) {
int i;
for (i = 0; i < Depth; i++)
printf(" ");
printf("%s", el);
for (i = 0; attr[i]; i += 2) {
printf(" %s='%s'", attr[i], attr[i + 1]);
}
printf("\n");
Depth++;
} /* End of start handler */
</pre>
</div>
<p>The end tag simply does the bookkeeping work of decrementing the Depth.
<div class="eg">
<pre>
void
end(void *data, const char *el) {
Depth--;
} /* End of end handler */
</pre>
</div>
<p>After creating the parser, the main program just has the job of
shoveling the document to the parser so that it can do its work.
<hr>
<h2><a name="building">Building and Installing expat</a></h2>
<p>The expat distribution comes as a compressed (with GNU gzip) tar file.
You may download the latest version from
<a href="http://sourceforge.net/projects/expat/">Source Forge</a>.
After unpacking this, cd into the directory. Then follow either the
Win32 directions or Unix directions below.</p>
<h3>Building under Win32</h3>
<p>If you're using the GNU compiler under cygwin, follow the Unix directions
in the next section. Otherwise if you have Microsoft's Developer Studio
installed, then from Windows Explorer double-click on "expat.dsp" in the
lib directory and build and install in the usual manner.</p>
<p>Alternatively, you may download the win32 binary package that contains
the expat.h include file and a pre-built dll.</p>
<h3>Building under Unix (or GNU)</h3>
<p>First you'll need to run the configure shell script in order to configure
the Makefiles and headers for your system.</p>
<p>If you're happy with all the defaults that configure picks for you,
and you have permission on your system to install into /usr/local, you can
install expat with this sequence of commands:</p>
<pre>
./configure
make
make install
</pre>
<p>There are some options that you can provide to this script, but the
only one we'll mention here is the <code>--prefix</code> option. You can
find out all the options available by running configure with just the
<code>--help</code> option.
</p>
By default, the configure script sets things up so that the library gets
installed in <code>/usr/local/lib</code> and the associated header file in
<code>/usr/local/include</code>.
But if you were to give the option, <code>--prefix=/home/me/mystuff</code>,
then the library and header would get installed in
<code>/home/me/mystuff/lib</code> and <code>/home/me/mystuff/include</code>
respectively.
</p>
<hr>
<h2><a name="using">Using Expat</a></h2>
<h3>Compiling and Linking against expat</h3>
<p>Unless you installed expat in a location not expected by your compiler
and linker, all you have to do to use expat in your programs is to include
the expat header (<code>#include <expat.h></code>) in your files that
make calls to it and to tell the linker
that it needs to link against the expat library. On Unix systems, this would
be the <code>-lexpat</code> argument.
Otherwise, you'll need to tell the compiler where to look for the expat header
and the linker where to find the expat library. You may also need to take
steps to tell the operating system where to find this libary at run time.
</p>
<p>On a Unix based system, here's what a Makefile might look like when expat
is installed in a standard location:</p>
<div class="eg">
<pre>
CC=cc
LDFLAGS=
LIBS= -lexpat
xmlapp: xmlapp.o
$(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
</pre>
</div>
<p>If you installed expat in, say, <code>/home/me/mystuff</code>, then
the Makefile would look like this:</p>
<div class="eg">
<pre>
CC=cc
CFLAGS= -I/home/me/mystuff/include
LDFLAGS=
LIBS= -L/home/me/mystuff/lib -lexpat
xmlapp: xmlapp.o
$(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)
</pre>
</div>
<p>You'd also have to set the environment variable <code>LD_LIBRARY_PATH</code>
to <code>/home/me/mystuff/lib</code> (or to
<code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib</code> if LD_LIBRARY_PATH
already has some directories in it) in order to run your application.
</p>
<h3>Expat Basics</h3>
<p>As we saw in the example in the overview, the first step in parsing an
XML document with expat is to create a parser object. There are
<a href="#creation">three functions</a> in the expat API for creating a
parser object.
However, only two of these
(<a href="#XML_ParserCreate"><code>XML_ParserCreate</code></a> and
<a href="#XML_ParserCreateNS"><code>XML_ParserCreateNS</code></a>)
can be used for constructing a parser for a top-level document.
The object returned by these functions is an opaque pointer
(i.e. expat.h declares it as void *) to data with further internal structure.
In order to free the memory associated with this object you must call
<a href="#XML_ParserFree"><code>XML_ParserFree</code></a>. Note that if
you have provided any <a href="userdata">user data</a> that gets stored
in the parser, then your application is responsible for freeing it prior to
calling XML_ParserFree.
</p>
<p>The objects returned by the parser creation functions are good for
parsing only one XML document or external parsed entity. If your application
needs to parse many XML documents, then it needs to create a parser object
for each one. The best way to deal with this is to create a higher level
object that contains all the default initialization you want for your parser
objects.
<p>Walking through a document hierarchy with a stream oriented parser will
require a good stack mechanism in order to keep track of current context.
For instance, to answer the simple question,
"What element does this text belong to?" requires a stack, since the
parser may have descended into other elements that are children of the
current one and has encountered this text on the way out.
<p>The things you're likely to want to keep on a stack are the currently
opened element and it's attributes. You push this information onto the
stack in the start handler and you pop it off in the end handler.
<p>For some tasks, it is sufficient to just keep information on what the
depth of the stack is (or would be if you had one.) The outline program shown
above presents one example. Another such task would be skipping over a
complete element. When you see the start tag for the element you want to
skip, you set a skip flag and record the depth at which the element started.
When the end tag handler encounters the same depth, the skipped element has
ended and the flag may be cleared. If you follow the convention that the
root element starts at 1, then you can use the same variable for skip flag
and skip depth.
<div class="eg">
<pre>
void
init_info(Parseinfo *info) {
info->skip = 0;
info->depth = 1;
/* Other initializations here */
} /* End of init_info */
void
rawstart(void *data, const char *el, const char **attr) {
Parseinfo *inf = (Parseinfo *) data;
if (! inf->skip) {
if (should_skip(inf, el, attr)) {
inf->skip = inf->depth;
}
else
start(inf, el, attr); /* This does rest of start handling */
}
inf->depth++;
} /* End of rawstart */
void
rawend(void *data, const char *el) {
Parseinfo *inf = (Parseinfo *) data;
inf->depth--;
if (! inf->skip)
end(inf, el); /* This does rest of end handling */
if (inf->skip == inf->depth)
inf->skip = 0;
} /* End rawend */
</pre>
</div>
<p>Notice in the above example the difference in how depth is manipulated
in the start and end handlers. The end tag handler should be the mirror
image of the start tag handler. This is necessary to properly model
containment. Since, in the start tag handler, we
incremented depth <em>after</em> the main body of start tag code, then in
the end handler, we need to manipulate it <em>before</em> the main body.
If we'd decided to increment it first thing in the start handler, then
we'd have had to decrement it last thing in the end handler.
<h3>Communicating between handlers</h3>
<p>In order to be able to pass information between different handlers
without using globals, you'll need to define a data structure to hold
the shared variables. You can then tell expat (with the XML_SetUserData
function) to pass a pointer to this
structure to the handlers. This is typically the first argument received
by most handlers.
<h3>Namespace Processing</h3>
<p>When the parser is created using the <code>XML_ParserCreateNS</code>,
function, expat performs namespace processing. Under namespace processing,
expat consumes <code>xmlns</code> and <code>xmlns:...</code> attributes,
which declare namespaces for the scope of the element in which they
occur. This means that your start handler will not see these attributes.
Your application can still be informed of these declarations by setting
namespace declaration handlers with
<a href="#XML_SetNamespaceDeclHandler">
<code>XML_SetNamespaceDeclHandler</code></a>.
<p>Element type and attribute names that belong to a given namespace are
passed to the appropriate handler in expanded form. By default this expanded
form is a concatenation of the namespace URI, the separator character (which
is the 2nd argument to <code>XML_ParserCreateNS</code>), and the local
name (i.e. the part after the colon). Names with undeclared prefixes are
passed through to the handlers unchanged, with the prefix and colon still
attached. Unprefixed attribute names are never expanded, and unprefixed
element names are only expanded when they are in the scope of a default
namespace.
<p>However if <a href="XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a>
has been called with a non-zero <code>do_nst</code> parameter, then the
expanded form for names with an explicit prefix is a concatenation of:
URI, separator, local name, separator, prefix.
</p>
<p>You can set handlers for the start of a namespace declaration and for
the end of a scope of a declaration with the
<code>XML_SetNamespaceDeclHandler</code> function.
The StartNamespaceDeclHandler is called prior to the start tag handler
and the EndNamespaceDeclHandler is called before the corresponding end tag
that ends the namespace's scope.
The namespace start handler gets passed the prefix and URI for the namespace.
For a default namespace declaration (xmlns='...'), the prefix will be null.
The URI will be null for the case where the default namespace is being unset.
The namespace end handler just gets the prefix for the closing scope.
<p>These handlers are called for each declaration. So if, for instance, a
start tag had three namespace declarations, then the StartNamespaceDeclHandler
would be called three times before the start tag handler is called, once for
each declaration.
<p>The <a href="src/namespace.c">namespace.c</a> example demonstrates the
use of these features. Like outline.c, it produces an outline, but
in addition it annotates when a namespace scope starts and when it ends.
This example also demonstrates use of application user data.
<h3>Character Encodings</h3>
<p>While XML is based on Unicode, and every XML processor is required to
recognized UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode), other
encodings may be declared in XML documents or entities. For the main
document, an XML declaration may contain an encoding declaration:
<pre>
<?xml version="1.0" encoding="ISO-8859-2"?>
</pre>
<p>External parsed entities may begin with a text declaration, which
looks like an XML declaration with just an encoding declaration:
<pre>
<?xml encoding="Big5"?>
</pre>
<p>With expat, you may also specify an encoding at the time of creating a
parser. This is useful when the encoding information may come from a source
outside the document itself (like a higher level protocol.)
<p><a name="builtin_encodings"></a>There are four built-in encodings in expat:
<ul>
<li>UTF-8
<li>UTF-16
<li>ISO-8859-1
<li>US-ASCII
</ul>
<p>Anything else discovered in an encoding declaration or in the
protocol encoding specified in the parser constructor, triggers a call
to the <code>UnknownEncodingHandler</code>. This handler gets passed
the encoding name and a pointer to an <code>XML_Encoding</code> data
structure. Your handler must fill in this structure and return 1 if
it knows how to deal with the encoding. Otherwise the handler should
return 0.
The handler also gets passed a pointer to an
optional application data structure that you may indicate when you set
the handler.
<p>Expat places restrictions on character encodings that it can support
by filling in the <code>XML_Encoding</code> structure.
include file:
<ol>
<li>Every ASCII character that can appear in a well-formed XML document
must be represented by a single byte, and that byte must correspond to
it's ASCII encoding (except for the characters $@\^'{}~)</li>
<li>Characters must be encoded in 4 bytes or less.</li>
<li>All characters encoded must have Unicode scalar values less than or
equal to 65535 (0xFFFF)<em>This does not apply to the built-in support
for UTF-16 and UTF-8</em></li>
<li>No character may be encoded by more that one distinct sequence of
bytes</li>
</ol>
<p><code>XML_Encoding</code> contains an array of integers that correspond
to the 1st byte of an encoding sequence. If the value in the array for a
byte is zero or positive, then the byte is a single byte encoding that
encodes the Unicode scalar value contained in the array. A -1 in this array
indicates a malformed byte. If the value is
-2, -3, or -4, then the byte is the beginning of a 2, 3, or 4 byte sequence
respectively. Multi-byte sequences are sent to the convert function pointed
at in the <code>XML_Encoding</code> structure. This function should return
the Unicode scalar value for the sequence or -1 if the sequence is malformed.
<p>One pitfall that novice expat users are likely to fall into is that
although expat may accept input in various encodings, the strings that it
passes to the handlers are always encoded in UTF-8. Your application is
responsible for any translation of these strings into other encodings.
<h3>Handling External Entity References</h3>
<p>Expat does not read or parse external entities directly. Note that any
external DTD is a special case of an external entity.
If you've set no <code>ExternalEntityRefHandler</code>, then external
entity references are silently ignored. Otherwise, it calls your handler with
the information needed to read and parse the external entity.
<p>Your handler
isn't actually responsible for parsing the entity, but it is responsible
for creating a subsidiary parser with
<code>XML_ExternalEntityParserCreate</code> that will do the job. This returns
an instance of <code>XML_Parser</code> that has handlers and other data
structures initialized from the parent parser. You may then use
<code>XML_Parse</code> or <code>XML_ParseBuffer</code> calls against this
parser.
Since external entities my refer to other external entities, your handler
should be prepared to be called recursively.
<h3>Parsing DTDs</h3>
<p>In order to parse parameter entities, before starting the parse, you must
call <code>XML_SetParamEntityParsing</code> with one of the following
arguments:
<dl>
<dt><code>XML_PARAM_ENTITY_PARSING_NEVER</code></dt>
<dd>Don't parse parameter entities or the external subset</dd>
<dt><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></dt>
<dd>Parse parameter entites and the external subset unless
<code>standalone</code> was set to "yes" in the XML declaration.</dd>
<dt><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></dt>
<dd>Always parse parameter entities and the external subset</dd>
</dl>
<p>In order to read an external DTD, you also have to set an
external entity reference handler as described above.
<hr>
<h2><a name="examples">Some expat Examples</a></h2>
<hr>
<!-- ================================================================ -->
<h2><a name="reference">Expat Reference</a></h2>
<h3><a name="creation">Parser Creation</a></h3>
<div class="fcndec"><a name="XML_ParserCreate"><pre>
XML_Parser
XML_ParserCreate(const XML_Char *encoding)
</pre></a></div>
<div class="fcndef">
Construct a new parser. If encoding is non-null, it specifies a
character encoding to use for the document. This overrides the document
encoding declaration. There are four built-in encodings:
<ul>
<li>US-ASCII
<li>UTF-8
<li>UTF-16
<li>ISO-8859-1
</ul>
Any other value will invoke a call to the UnknownEncodingHandler.
</div>
<div class="fcndec"><a name="XML_ParserCreateNS"><pre>
XML_Parser
XML_ParserCreateNS(const XML_Char *encoding,
XML_Char sep)
</pre></a></div>
<div class="fcndef">
Constructs a new parser that has namespace processing in effect. Namespace
expanded element names and attribute names are returned as a concatenation
of the namespace URI, <em>sep</em>, and the local part of the name. This
means that you should pick a character for <em>sep</em> that can't be
part of a legal URI.</div>
<div class="fcndec"><a name="XML_ParserCreate_MM"><pre>
XML_Parser
XML_ParserCreate_MM(const XML_Char *encoding,
const XML_Memory_Handling_Suite *ms,
const XML_Char *sep)
</pre></a></div>
<div class="fcndef">
<p>Construct a new parser using the suite of memory handling functions
specified in <code>ms</code>. If <code>ms</code> is NULL, then use the
standard set of memory management functions. If <code>sep</code> is
non NULL, then namespace processing is enabled in the created parser
and the character pointed at by sep is used as the separator between
the namespace URI and the local part of the name</p>
<div class="eg"><pre>
typedef struct {
void *(*malloc_fcn)(size_t size);
void *(*realloc_fcn)(void *ptr, size_t size);
void (*free_fcn)(void *ptr);
} XML_Memory_Handling_Suite;
</pre></div>
</div>
<div class="fcndec"><a name="XML_ExternalEntityParserCreate"><pre>
XML_Parser
XML_ExternalEntityParserCreate(XML_Parser p,
const XML_Char *context,
const XML_Char *encoding)
</pre></a></div>
<div class="fcndef">
Construct a new XML_Parser object for parsing an external general
entity. Context is the context argument passed in a call to a
ExternalEntityRefHandler. Other state information such as handlers, user data,
namespace processing is inherited from the parser passed as the 1st
argument. So you shouldn't need to call any of the behavior changing
functions on this parser (unless you want it to act differently than the
parent parser.)
</div>
<div class="fcndec"><a name="XML_ParserFree"><pre>
void
XML_ParserFree(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Free memory used by the parser. Your application is responsible for
freeing any memory associated with <a href="#userdata">UserData</a>.
</div>
<h3><a name="parsing">Parsing</a></h3>
<div class="fcndec"><a name="XML_Parse"><pre>
int
XML_Parse(XML_Parser p,
const char *s,
int len,
int isFinal)
</pre></a></div>
<div class="fcndef">
Parse some more of the document. The string <code>s</code> is a buffer
containing part (or perhaps all) of the document. The number of bytes of s
that are part of the document is indicated by <code>len</code>. This means
that <code>s</code> doesn't have to be null terminated. It also means that
if <code>len</code> is larger than the number of bytes in the block of
memory that <code>s</code> points at, then a memory fault is likely. The
<code>isFinal</code> parameter informs the parser that this is the last
piece of the document. Frequently, the last piece is empty (i.e.
<code>len</code> is zero.)
If a parse error occurred, it returns 0. Otherwise it returns a non-zero
value.
</div>
<div class="fcndec"><a name="XML_ParseBuffer"><pre>
int
XML_ParseBuffer(XML_Parser p,
int len,
int isFinal)
</pre></a></div>
<div class="fcndef">
This is just like XML_Parse, except in this case expat provides the buffer.
By obtaining the buffer from expat with the <code>XML_GetBuffer</code>
function, the application can avoid double copying of the input.
</div>
<div class="fcndec"><a name="XML_GetBuffer"><pre>
void *
XML_GetBuffer(XML_Parser p,
int len)
</pre></a></div>
<div class="fcndef">
Obtain a buffer of size <code>len</code> to read a piece of the document
into. A NULL value is returned if expat can't allocate enough memory for
this buffer. This has to be called prior to every call to
<code>XML_ParseBuffer</code>. A typical use would look like this:
<div id="eg">
<pre>
for (;;) {
int bytes_read;
void *buff = XML_GetBuffer(p, BUFF_SIZE);
if (buff == NULL) {
/* handle error */
}
bytes_read = read(docfd, buff, BUFF_SIZE);
if (bytes_read < 0) {
/* handle error */
}
if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) {
/* handle parse error */
}
if (bytes_read == 0)
break;
}
</pre>
</div>
</div>
<h3><a name="setting">Handler Setting</a></h3>
<p>Although handlers are typically set prior to parsing and left alone, an
application may choose to set or change the handler for a parsing event
while the parse is in progress. For instance, your application may choose
to ignore all text not descended from a <code>para</code> element. One
way it could do this is to set the character handler when a para start tag
is seen, and unset it for the corresponding end tag.
<p>A handler may be <em>unset</em> by providing a NULL pointer to the
appropriate handler setter. None of the handler setting functions have
a return value.
<p>Your handlers will be receiving strings in arrays of type
<code>XML_Char</code>. This type is defined in expat.h as <code>char *</code>
and contains bytes encoding UTF-8.
Note that you'll receive them in this form independent of the original
encoding of the document.</p>
<div class="handler">
<div class="setter"><a name="XML_SetStartElementHandler"><pre>
XML_SetStartElementHandler(XML_Parser p,
XML_StartElementHandler start);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_StartElementHandler)(void *userData,
const XML_Char *name,
const XML_Char **atts);
</pre></div>
<p>Set handler for start (and empty) tags. Attributes are passed to the start
handler as a pointer to a vector of char pointers. Each attribute seen in
a start (or empty) tag occupies 2 consecutive places in this vector: the
attribute name followed by the attribute value. These pairs are terminated
by a null pointer.</p>
<p>Note that an empty tag generates a call to both start and end handlers
(in that order).</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetEndElementHandler"><pre>
XML_SetEndElementHandler(XML_Parser p,
XML_EndElementHandler);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_EndElementHandler)(void *userData,
const XML_Char *name);
</pre></div>
<p>Set handler for end (and empty) tags. As noted above, an empty tag
generates a call to both start and end handlers.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetElementHandler"><pre>
XML_SetElementHandler(XML_Parser p,
XML_StartElementHandler start,
XML_EndElementHandler end);
</pre></a></div>
<p>Set handlers for start and end tags with one call.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetCharacterDataHandler"><pre>
XML_SetCharacterDataHandler(XML_Parser p,
XML_CharacterDataHandler charhndl)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_CharacterDataHandler)(void *userData,
const XML_Char *s,
int len);
</pre></div>
<p>Set a text handler. The string your handler receives
is <em>NOT zero terminated</em>. You have to use the length argument
to deal with the end of the string. A single block of contiguous text
free of markup may still result in a sequence of calls to this handler.
In other words, if you're searching for a pattern in the text, it may
be split across calls to this handler.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetProcessingInstructionHandler"><pre>
XML_SetProcessingInstructionHandler(XML_Parser p,
XML_ProcessingInstructionHandler proc)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_ProcessingInstructionHandler)(void *userData,
const XML_Char *target,
const XML_Char *data);
</pre></div>
<p>Set a handler for processing instructions. The target is the first word
in the processing instruction. The data is the rest of the characters in
it after skipping all whitespace after the initial word.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetCommentHandler"><pre>
XML_SetCommentHandler(XML_Parser p,
XML_CommentHandler cmnt)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_CommentHandler)(void *userData,
const XML_Char *data);
</pre></div>
<p>Set a handler for comments. The data is all text inside the comment
delimiters.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetStartCdataSectionHandler"><pre>
XML_SetStartCdataSectionHandler(XML_Parser p,
XML_StartCdataSectionHandler start);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_StartCdataSectionHandler)(void *userData);
</pre></div>
<p>Set a handler that gets called at the beginning of a CDATA section.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetEndCdataSectionHandler"><pre>
XML_SetEndCdataSectionHandler(XML_Parser p,
XML_EndCdataSectionHandler end);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_EndCdataSectionHandler)(void *userData);
</pre></div>
<p>Set a handler that gets called at the end of a CDATA section.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetCdataSectionHandler"><pre>
XML_SetCdataSectionHandler(XML_Parser p,
XML_StartCdataSectionHandler start,
XML_EndCdataSectionHandler end)
</pre></a></div>
<p>Sets both CDATA section handlers with one call.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetDefaultHandler"><pre>
XML_SetDefaultHandler(XML_Parser p,
XML_DefaultHandler hndl)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_DefaultHandler)(void *userData,
const XML_Char *s,
int len);
</pre></div>
<p>Sets a handler for any characters in the document which wouldn't
otherwise be handled. This includes both data for which no handlers can be
set (like some kinds of DTD declarations) and data which could be reported
but which currently has no handler set. Note that a contiguous piece of
data that is destined to be reported to the default handler may actually
be reported over several calls to the handler. Setting the handler with
this call has the side effect of turning off expansion of references
to internally defined general entities. Instead these references are
passed to the default handler.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetDefaultHandlerExpand"><pre>
XML_SetDefaultHandlerExpand(XML_Parser p,
XML_DefaultHandler hndl)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_DefaultHandler)(void *userData,
const XML_Char *s,
int len);
</pre></div>
<p>This sets a default handler, but doesn't affect expansion of internal
entity references.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetExternalEntityRefHandler"><pre>
XML_SetExternalEntityRefHandler(XML_Parser p,
XML_ExternalEntityRefHandler hndl)
</pre></a></div>
<div class="signature"><pre>
typedef int
(*XML_ExternalEntityRefHandler)(XML_Parser p,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId);
</pre></div>
<p>Set an external entity reference handler. This handler is also
called for processing an external DTD subset if parameter entity parsing
is in effect. (See <a href="#XML_SetParamEntityParsing">
<code>XML_SetParamEntityParsing</code></a>.)</p>
<p>The base parameter is the base to use for relative system identifiers.
It is set by <a href="#XML_SetBase">XML_SetBase</a> and may be null. The
public id parameter is the public id given in the entity declaration and
may be null. The system id is the system identifier specified in the entity
declaration and is never null.</p>
<p>There are a couple of ways in which this handler differs from others.
First, this handler returns an integer. A non-zero value should be returned
for successful handling of the external entity reference. Returning a zero
indicates failure, and causes the calling parser to return
an XML_ERROR_EXTERNAL_ENTITY_HANDLING error.</p>
<p>Second, instead of having userData as its first argument, it receives the
parser that encountered the entity reference. This, along with the context
parameter, may be used as arguments to a call to
<a href="#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a>.
Using the returned parser, the body of the external entity can be recursively
parsed.</p>
<p>Since this handler may be called recursively, it should not be saving
information into global or static variables.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetUnknownEncodingHandler"><pre>
XML_SetUnknownEncodingHandler(XML_Parser p,
XML_UnknownEncodingHandler enchandler,
void *encodingHandlerData)
</pre></a></div>
<div class="signature"><pre>
typedef int
(*XML_UnknownEncodingHandler)(void *encodingHandlerData,
const XML_Char *name,
XML_Encoding *info);
</pre></div>
<p>Set a handler to deal with encodings other than the
<a href="#builtin_encodings">built in set</a>. If the handler knows how
to deal with an encoding with the given name, it should fill in the info
data structure and return 1. Otherwise it should return 0.</p>
<pre>
typedef struct {
int map[256];
void *data;
int (*convert)(void *data, const char *s);
void (*release)(void *data);
} XML_Encoding;
</pre>
<p>The map array contains information for every possible possible leading
byte in a byte sequence. If the corresponding value is >= 0, then it's
a single byte sequence and the byte encodes that Unicode value. If the
value is -1, then that byte is invalid as the initial byte in a sequence.
If the value is -n, where n is an integer > 1, then n is the number of
bytes in the sequence and the actual conversion is accomplished by a
call to the function pointed at by convert. This function may return -1
if the sequence itself is invalid. The convert pointer may be null if
there are only single byte codes. The data parameter passed to the convert
function is the data pointer from XML_Encoding. The string s is <em>NOT</em>
null terminated and points at the sequence of bytes to be converted.</p>
<p>The function pointed at by release is called by the parser when it is
finished with the encoding. It may be null.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetStartNamespaceDeclHandler"><pre>
XML_SetStartNamespaceDeclHandler(XML_Parser p,
XML_StartNamespaceDeclHandler start);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_StartNamespaceDeclHandler)(void *userData,
const XML_Char *prefix,
const XML_Char *uri);
</pre></div>
<p>Set a handler to be called when a namespace is declared. Namespace
declarations occur inside start tags. But the namespace declaration start
handler is called before the start tag handler for each namespace declared
in that start tag.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetEndNamespaceDeclHandler"><pre>
XML_SetEndNamespaceDeclHandler(XML_Parser p,
XML_EndNamespaceDeclHandler end);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_EndNamespaceDeclHandler)(void *userData,
const XML_Char *prefix);
</pre></div>
<p>Set a handler to be called when leaving the scope of a namespace
declaration. This will be called, for each namespace declaration, after
the handler for the end tag of the element in which the namespace was declared.
</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetNamespaceDeclHandler"><pre>
XML_SetNamespaceDeclHandler(XML_Parser p,
XML_StartNamespaceDeclHandler start,
XML_EndNamespaceDeclHandler end)
</pre></a></div>
<p>Sets both namespace declaration handlers with a single call</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetXmlDeclHandler"><pre>
XML_SetXmlDeclHandler(XML_Parser p,
XML_XmlDeclHandler xmldecl);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_XmlDeclHandler) (void *userData,
const XML_Char *version,
const XML_Char *encoding,
int standalone);
</pre></div>
<p>Sets a handler that is called for XML declarations and also for
text declarations discovered in external entities. The way to distinguish
is that the <code>version</code> parameter will be NULL for text
declarations. The <code>encoding</code> parameter may be NULL for
an XML declaration. The <code>standalone</code> argument will contain
-1, 0, or 1 indicating respectively that there was no standalone parameter in
the declaration, that it was given as no, or that it was given as yes.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetStartDoctypeDeclHandler"><pre>
XML_SetStartDoctypeDeclHandler(XML_Parser p,
XML_StartDoctypeDeclHandler start);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_StartDoctypeDeclHandler)(void *userData,
const XML_Char *doctypeName,
const XML_Char *sysid,
const XML_Char *pubid,
int has_internal_subset);
</pre></div>
<p>Set a handler that is called at the start of a DOCTYPE declaration,
before any external or internal subset is parsed. Both <code>sysid</code>
and <code>pubid</code> may be NULL. The <code>has_internal_subset</code>
will be non-zero if the DOCTYPE declaration has an internal subset.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetEndDoctypeDeclHandler"><pre>
XML_SetEndDoctypeDeclHandler(XML_Parser p,
XML_EndDoctypeDeclHandler end);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_EndDoctypeDeclHandler)(void *userData);
</pre></div>
<p>Set a handler that is called at the end of a DOCTYPE declaration,
after parsing any external subset.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetDoctypeDeclHandler"><pre>
XML_SetDoctypeDeclHandler(XML_Parser p,
XML_StartDoctypeDeclHandler start,
XML_EndDoctypeDeclHandler end);
</pre></a></div>
<p>Set both doctype handlers with one call.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetElementDeclHandler"><pre>
XML_SetElementDeclHandler(XML_Parser p,
XML_ElementDeclHandler eldecl);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_ElementDeclHandler)(void *userData,
const XML_Char *name,
XML_Content *model);
</pre></div>
<div class="signature"><pre>
enum XML_Content_Type {
XML_CTYPE_EMPTY = 1,
XML_CTYPE_ANY,
XML_CTYPE_MIXED,
XML_CTYPE_NAME,
XML_CTYPE_CHOICE,
XML_CTYPE_SEQ
};
enum XML_Content_Quant {
XML_CQUANT_NONE,
XML_CQUANT_OPT,
XML_CQUANT_REP,
XML_CQUANT_PLUS
};
typedef struct XML_cp XML_Content;
struct XML_cp {
enum XML_Content_Type type;
enum XML_Content_Quant quant;
const XML_Char * name;
unsigned int numchildren;
XML_Content * children;
};
</pre></div>
<p>Sets a handler for element declarations in a DTD. The handler gets called
with the name of the element in the declaration and a pointer to a structure
that contains the element model. It is the application's responsibility to
free this data structure.</p>
<p>The <code>model</code> argument is the root of a tree of
<code>XML_Content</code> nodes. If <code>type</code> equals
<code>XML_CTYPE_EMPTY</code> or <code>XML_CTYPE_ANY</code>, then
<code>quant</code> will be <code>XML_CQUANT_NONE</code>, and the other fields
will be zero or NULL.
If <code>type</code> is <code>XML_CTYPE_MIXED</code>, then <code>quant</code>
will be <code>XML_CQUANT_NONE</code> or <code>XML_CQUANT_REP</code> and
<code>numchildren</code> will contain the number of elements that are allowed
to be mixed in and <code>children</code> points to an array of
<code>XML_Content</code> structures that will all have type XML_CTYPE_NAME
with no quantification.
Only the root node can be type <code>XML_CTYPE_EMPTY</code>, <code>XML_CTYPE_ANY</code>, or <code>XML_CTYPE_MIXED</code>.</p>
<p>For type <code>XML_CTYPE_NAME</code>, the <code>name</code> field points
to the name and the <code>numchildren</code> and <code>children</code> fields
will be zero and NULL. The <code>quant</code> field will indicate any
quantifiers placed on the name.</p>
<p>Types <code>XML_CTYPE_CHOICE</code> and <code>XML_CTYPE_SEQ</code>
indicate a choice or sequence respectively. The <code>numchildren</code>
field indicates how many nodes in the choice or sequence and
<code>children</code> points to the nodes.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetAttlistDeclHandler"><pre>
XML_SetAttlistDeclHandler(XML_Parser p,
XML_AttlistDeclHandler attdecl);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_AttlistDeclHandler) (void *userData,
const XML_Char *elname,
const XML_Char *attname,
const XML_Char *att_type,
const XML_Char *dflt,
int isrequired);
</pre></div>
<p>Set a handler for attlist declarations in the DTD. This handler is called
for <em>each</em> attribute. So a single attlist declaration with multiple
attributes declared will generate multiple calls to this handler. The
<code>elname</code> parameter returns the name of the element for which the
attribute is being declared. The attribute name is in the <code>attname</code>
parameter. The attribute type is in the <code>att_type</code> parameter.
It is the string representing the type in the declaration with whitespace
removed.</p>
<p>The <code>dflt</code> parameter holds the default value. It will
be NULL in the case of "#IMPLIED" or "#REQUIRED" attributes. You can
distinguish these two cases by checking the <code>isrequired</code>
parameter, which will be true in the case of "#REQUIRED" attributes.
Attributes which are "#FIXED" will have also have a true
<code>isrequired</code>, but they will have the non-NULL fixed value in the
<code>dflt</code> parameter.
</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetEntityDeclHandler"><pre>
XML_SetEntityDeclHandler(XML_Parser p,
XML_EntityDeclHandler handler);
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_EntityDeclHandler) (void *userData,
const XML_Char *entityName,
int is_parameter_entity,
const XML_Char *value,
int value_length,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName);
</pre></div>
<p>Sets a handler that will be called for all entity declarations.
The <code>is_parameter_entity</code> argument will be non-zero in the case
of parameter entities and zero otherwise.
</p>
<p>For internal entities (<code><!ENTITY foo "bar"></code>),
<code>value</code> will be non-NULL and <code>systemId</code>,
<code>publicId</code>, and <code>notationName</code> will all be NULL.
The value string is <em>not</em> NULL terminated; the length is provided in
the <code>value_length</code> parameter. Do not use <code>value_length</code>
to test for internal entities, since it is legal to have zero-length
values. Instead check for whether or not <code>value</code> is NULL.</p>
<p>The <code>notationName</code> argument will have a non-NULL value only
for unparsed entity declarations.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetUnparsedEntityDeclHandler"><pre>
XML_SetUnparsedEntityDeclHandler(XML_Parser p,
XML_UnparsedEntityDeclHandler h)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_UnparsedEntityDeclHandler)(void *userData,
const XML_Char *entityName,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName);
</pre></div>
<p>Set a handler that receives declarations of unparsed entities. These
are entity declarations that have a notation (NDATA) field:</p>
<div id="eg"><pre>
<!ENTITY logo SYSTEM "images/logo.gif" NDATA gif>
</pre></div>
<p>This handler is obsolete and is provided for backwards compatibility.
Use instead <a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a>.
</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetNotationDeclHandler"><pre>
XML_SetNotationDeclHandler(XML_Parser p,
XML_NotationDeclHandler h)
</pre></a></div>
<div class="signature"><pre>
typedef void
(*XML_NotationDeclHandler)(void *userData,
const XML_Char *notationName,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId);
</pre></div>
<p>Set a handler that receives notation declarations.</p>
</div>
<div class="handler">
<div class="setter"><a name="XML_SetNotStandaloneHandler"><pre>
XML_SetNotStandaloneHandler(XML_Parser p,
XML_NotStandaloneHandler h)
</pre></a></div>
<div class="signature"><pre>
typedef int
(*XML_NotStandaloneHandler)(void *userData);
</pre></div>
<p>Set a handler that is called if the document is not "standalone".
This happens when there is an external subset or a reference to a parameter
entity, but does not have standalone set to "yes" in an XML declaration.
If this handler returns 0, then the parser will throw an
XML_ERROR_NOT_STANDALONE error.</p>
</div>
<h3><a name="position">Parse position and error reporting functions</a></h3>
<p>These are the functions you'll want to call when the parse functions
return 0 (i.e. a parse error has ocurred), although the position reporting
functions are useful outside of errors. The position reported is the byte
position (in the original document or entity encoding) of the first of the
sequence of characters that generated the current event (or the error that
caused the parse functions to return 0.)</p>
<p>The position reporting functions are accurate only outside of the DTD.
In other words, they usually return bogus information when called from within
a DTD declaration handler.</p>
<div class="fcndec"><a name="XML_GetErrorCode"><pre>
enum XML_Error
XML_GetErrorCode(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Return what type of error has occurred.
</div>
<div class="fcndec"><a name="XML_ErrorString"><pre>
const XML_LChar *
XML_ErrorString(int code)
</pre></a></div>
<div class="fcndef">
Return a string describing the error corresponding to code.
The code should be one of the enums that can be returned from
XML_GetErrorCode.
</div>
<div class="fcndec"><a name="XML_GetCurrentByteIndex"><pre>
long
XML_GetCurrentByteIndex(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Return the byte offset of the position.
</div>
<div class="fcndec"><a name="XML_GetCurrentLineNumber"><pre>
int
XML_GetCurrentLineNumber(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Return the line number of the position.
</div>
<div class="fcndec"><a name="XML_GetCurrentColumnNumber"><pre>
int
XML_GetCurrentColumnNumber(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Return the offset, from the beginning of the current line, of
the position.
</div>
<div class="fcndec"><a name="XML_GetCurrentByteCount"><pre>
int
XML_GetCurrentByteCount(XML_Parser p);
</pre></a></div>
<div class="fcndef">
Return the number of bytes in the current event. Returns 0 if the event is
inside a reference to an internal entity.
</div>
<div class="fcndec"><a name="XML_GetInputContext"><pre>
const char *
XML_GetInputContext(XML_Parser p,
int *offset,
int *size);
</pre></a></div>
<div class="fcndef">
<p>Returns the parser's input buffer, sets the integer pointed at by
<code>offset</code> to the offset within this buffer of the current
parse position, and set the integer pointed at by <code>size</code>
to the size of the returned buffer.</p>
<p>This should only be called from within a handler during an active
parse and the returned buffer should only be referred to from within
the handler that made the call. This input buffer contains the untranslated
bytes of the input.</p>
<p>Only a limited amount of context is kept, so if the event triggering
a call spans over a very large amount of input, the actual parse position
may be before the beginning of the buffer.</p>
</div>
<h3><a name="miscellaneous">Miscellaneous functions</a></h3>
<p>The functions in this section either obtain state information from the
parser or can be used to dynamicly set parser options.
<div class="fcndec"><a name="XML_SetUserData"><pre>
void
XML_SetUserData(XML_Parser p,
void *userData)
</pre></a></div>
<div class="fcndef">
This sets the user data pointer that gets passed to handlers.
It overwrites any previous value for this pointer. Note that the
application is responsible for freeing the memory associated with
<code>userData</code> when it is finished with the parser. So if
you call this when there's already a pointer there, and you haven't
freed the memory associated with it, then you've probably just leaked
memory.
</div>
<div class="fcndec"><a name="XML_GetUserData"><pre>
void *
XML_GetUserData(XML_Parser p)
</pre></a></div>
<div class="fcndef">
This returns the user data pointer that gets passed to handlers.
It is actually implemented as a macro.
</div>
<div class="fcndec"><a name="XML_UseParserAsHandlerArg"><pre>
void
XML_UseParserAsHandlerArg(XML_Parser p)
</pre></a></div>
<div class="fcndef">
After this is called, handlers receive the parser in the
userData argument. The userData information can still be obtained using
the XML_GetUserData function.
</div>
<div class="fcndec"><a name="XML_SetBase"><pre>
int
XML_SetBase(XML_Parser p,
const XML_Char *base)
</pre></a></div>
<div class="fcndef">
Set the base to be used for resolving relative URIs in system identifiers.
The return value is 0 if there's no memory to store base, otherwise it's
non-zero.
</div>
<div class="fcndec"><a name="XML_GetBase"><pre>
const XML_Char *
XML_GetBase(XML_Parser p)
</pre></a></div>
<div class="fcndef">
Return the base for resolving relative URIs.
</div>
<div class="fcndec"><a name="XML_GetSpecifiedAttributeCount"><pre>
int
XML_GetSpecifiedAttributeCount(XML_Parser p)
</pre></a></div>
<div class="fcndef">
When attributes are reported to the start handler in the atts vector,
attributes that were explicitly set in the element occur before any
attributes that receive their value from default information in an
ATTLIST declaration. This function returns the number of attributes that
were explicitly set times two, thus giving the offset in the <code>atts</code>
array passed to the start tag handler of the first attribute set
due to defaults. It supplies information for the last call to a start
handler. If called inside a start handler, then that means the current call.
</div>
<div class="fcndec"><a name="XML_GetIdAttributeIndex"><pre>
int
XML_GetIdAttributeIndex(XML_Parser p);
</pre></a></div>
<div class="fcndef">
Returns the index of the ID attribute passed in the atts array
in the last call to XML_StartElementHandler, or -1 if there is no ID
attribute. If called inside a start handler, then that means the current call.
</div>
<div class="fcndec"><a name="XML_SetEncoding"><pre>
int
XML_SetEncoding(XML_Parser p,
const XML_Char *encoding)
</pre></a></div>
<div class="fcndef">
Set the encoding to be used by the parser. It is equivalent to
passing a non-null encoding argument to the parser creation functions.
It must not be called after XML_Parser or XML_ParseBuffer have been
called on the given parser.
</div>
<div class="fcndec"><a name="XML_SetParamEntityParsing"><pre>
int
XML_SetParamEntityParsing(XML_Parser p,
enum XML_ParamEntityParsing code)
</pre></a></div>
<div class="fcndef">
This enables parsing of parameter entities, including the external
parameter entity that is the external DTD subset, according to
<code>code</code>.
The choices for <code>code</code> are:
<ul>
<li>XML_PARAM_ENTITY_PARSING_NEVER
<li>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
<li>XML_PARAM_ENTITY_PARSING_ALWAYS
</ul>
</div>
<div class="fcndec"><a name="XML_SetReturnNSTriplet"><pre>
void
XML_SetReturnNSTriplet(XML_Parser parser,
int do_nst);
</pre></a></div>
<div class="fcndef">
<p>
This function only has an effect when using a parser created with
<a href="#XML_ParserCreateNS">XML_ParserCreateNS</a>, i.e. when namespace
processing is in effect. The <code>do_nst</code> sets whether or not prefixes
are returned with names qualified with a namespace prefix. If this function
is called with <code>do_nst</code> non-zero, then afterwards namespace
qualified names (that is qualified with a prefix as opposed to belonging
to a default namespace) are returned as a triplet with the three parts
separated by the namespace separator specified when the parser was created.
The order of returned parts is URI, local name, and prefix.</p>
<p>If <code>do_nst</code> is zero, then namespaces are reported in the
default manner, URI then local_name separated by the namespace separator.</p>
</div>
<div class="fcndec"><a name="XML_ExpatVersion"><pre>
XML_LChar *
XML_ExpatVersion();
</pre></a></div>
<div class="fcndef">
Return the library version string (e.g. "expat_1.95.1").
</div>
</body>
</html>
|