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
|
<HTML
><HEAD
><TITLE
>XML parser functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Function Reference"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="wddx_deserialize"
HREF="function.wddx-deserialize.html"><LINK
REL="NEXT"
TITLE="xml_parser_create"
HREF="function.xml-parser-create.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="reference"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.wddx-deserialize.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.xml-parser-create.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.xml"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>LXXI. XML parser functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN39427"
></A
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="xml.partintro"
>Introduction</A
></H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.intro"
>About XML</A
></H2
><P
> XML (eXtensible Markup Language) is a data format for structured
document interchange on the Web. It is a standard defined by
The World Wide Web consortium (W3C). Information about XML and
related technologies can be found at <A
HREF="http://www.w3.org/XML/"
TARGET="_top"
>http://www.w3.org/XML/</A
>.
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.install"
>Installation</A
></H2
><P
> This extension uses <SPAN
CLASS="productname"
>expat</SPAN
>, which can
be found at <A
HREF="http://www.jclark.com/xml/"
TARGET="_top"
>http://www.jclark.com/xml/</A
>. The
Makefile that comes with expat does not build a library by
default, you can use this make rule for that:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> libexpat.a: $(OBJS)
ar -rc $@ $(OBJS)
ranlib $@
</PRE
></TD
></TR
></TABLE
>
A source RPM package of expat can be found at <A
HREF="http://www.guardian.no/~ssb/phpxml.html"
TARGET="_top"
>http://www.guardian.no/~ssb/phpxml.html</A
>.
</P
><P
> Note that if you are using Apache-1.3.7 or later, you already
have the required expat library. Simply configure PHP using
<TT
CLASS="option"
>--with-xml</TT
> (without any
additional path) and it will automatically use the expat library
built into Apache.
</P
><P
> On UNIX, run <B
CLASS="command"
>configure</B
> with the <TT
CLASS="option"
>--with-xml</TT
> option. The
<SPAN
CLASS="productname"
>expat</SPAN
> library should be installed
somewhere your compiler can find it. If you compile PHP as a
module for Apache 1.3.9 or later, PHP will automatically use the
bundled <SPAN
CLASS="productname"
>expat</SPAN
> library from Apache.
You may need to set <TT
CLASS="envar"
>CPPFLAGS</TT
> and
<TT
CLASS="envar"
>LDFLAGS</TT
> in your environment before running
configure if you have installed expat somewhere exotic.
</P
><P
> Build PHP. <I
CLASS="emphasis"
>Tada!</I
> That should be it.
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.about"
>About This Extension</A
></H2
><P
> This PHP extension implements support for James Clark's
<SPAN
CLASS="productname"
>expat</SPAN
> in PHP. This toolkit lets you
parse, but not validate, XML documents. It supports three
source <A
HREF="ref.xml.html#xml.encoding"
>character encodings</A
>
also provided by PHP: <TT
CLASS="literal"
>US-ASCII</TT
>,
<TT
CLASS="literal"
>ISO-8859-1</TT
> and <TT
CLASS="literal"
>UTF-8</TT
>.
<TT
CLASS="literal"
>UTF-16</TT
> is not supported.
</P
><P
> This extension lets you <A
HREF="function.xml-parser-create.html"
>create XML parsers</A
>
and then define <I
CLASS="emphasis"
>handlers</I
> for different XML
events. Each XML parser also has a few <A
HREF="function.xml-parser-set-option.html"
>parameters</A
> you
can adjust.
</P
><P
> The XML event handlers defined are:
<DIV
CLASS="table"
><A
NAME="AEN39466"
></A
><P
><B
>Table 1. Supported XML handlers</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>PHP function to set handler</TH
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>Event description</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><A
HREF="function.xml-set-element-handler.html"
><B
CLASS="function"
>xml_set_element_handler()</B
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> Element events are issued whenever the XML parser
encounters start or end tags. There are separate handlers
for start tags and end tags.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> <A
HREF="function.xml-set-character-data-handler.html"
><B
CLASS="function"
>xml_set_character_data_handler()</B
></A
>
</TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> Character data is roughly all the non-markup contents of
XML documents, including whitespace between tags. Note
that the XML parser does not add or remove any whitespace,
it is up to the application (you) to decide whether
whitespace is significant.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> <A
HREF="function.xml-set-processing-instruction-handler.html"
><B
CLASS="function"
>xml_set_processing_instruction_handler()</B
></A
>
</TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> PHP programmers should be familiar with processing
instructions (PIs) already. <?php ?> is a processing
instruction, where <TT
CLASS="replaceable"
><I
>php</I
></TT
> is called
the "PI target". The handling of these are
application-specific, except that all PI targets starting
with "XML" are reserved.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><A
HREF="function.xml-set-default-handler.html"
><B
CLASS="function"
>xml_set_default_handler()</B
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> What goes not to another handler goes to the default
handler. You will get things like the XML and document
type declarations in the default handler.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> <A
HREF="function.xml-set-unparsed-entity-decl-handler.html"
><B
CLASS="function"
>xml_set_unparsed_entity_decl_handler()</B
></A
>
</TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> This handler will be called for declaration of an unparsed
(NDATA) entity.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> <A
HREF="function.xml-set-notation-decl-handler.html"
><B
CLASS="function"
>xml_set_notation_decl_handler()</B
></A
>
</TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> This handler is called for declaration of a notation.
</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> <A
HREF="function.xml-set-external-entity-ref-handler.html"
><B
CLASS="function"
>xml_set_external_entity_ref_handler()</B
></A
>
</TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
> This handler is called when the XML parser finds a
reference to an external parsed general entity. This can
be a reference to a file or URL, for example. See <A
HREF="ref.xml.html#example.xml-external-entity"
>the external entity
example</A
> for a demonstration.
</TD
></TR
></TBODY
></TABLE
></DIV
>
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.case-folding"
>Case Folding</A
></H2
><P
> The element handler functions may get their element names
<I
CLASS="glossterm"
>case-folded</I
>. Case-folding is defined by
the XML standard as "a process applied to a sequence of
characters, in which those identified as non-uppercase are
replaced by their uppercase equivalents". In other words, when
it comes to XML, case-folding simply means uppercasing.
</P
><P
> By default, all the element names that are passed to the handler
functions are case-folded. This behaviour can be queried and
controlled per XML parser with the
<A
HREF="function.xml-parser-get-option.html"
><B
CLASS="function"
>xml_parser_get_option()</B
></A
> and
<A
HREF="function.xml-parser-set-option.html"
><B
CLASS="function"
>xml_parser_set_option()</B
></A
> functions,
respectively.
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.error-codes"
>Error Codes</A
></H2
><P
> The following constants are defined for XML error codes (as
returned by <A
HREF="function.xml-parse.html"
><B
CLASS="function"
>xml_parse()</B
></A
>):
<P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
>XML_ERROR_NONE</TD
></TR
><TR
><TD
>XML_ERROR_NO_MEMORY</TD
></TR
><TR
><TD
>XML_ERROR_SYNTAX</TD
></TR
><TR
><TD
>XML_ERROR_NO_ELEMENTS</TD
></TR
><TR
><TD
>XML_ERROR_INVALID_TOKEN</TD
></TR
><TR
><TD
>XML_ERROR_UNCLOSED_TOKEN</TD
></TR
><TR
><TD
>XML_ERROR_PARTIAL_CHAR</TD
></TR
><TR
><TD
>XML_ERROR_TAG_MISMATCH</TD
></TR
><TR
><TD
>XML_ERROR_DUPLICATE_ATTRIBUTE</TD
></TR
><TR
><TD
>XML_ERROR_JUNK_AFTER_DOC_ELEMENT</TD
></TR
><TR
><TD
>XML_ERROR_PARAM_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_UNDEFINED_ENTITY</TD
></TR
><TR
><TD
>XML_ERROR_RECURSIVE_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_ASYNC_ENTITY</TD
></TR
><TR
><TD
>XML_ERROR_BAD_CHAR_REF</TD
></TR
><TR
><TD
>XML_ERROR_BINARY_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_MISPLACED_XML_PI</TD
></TR
><TR
><TD
>XML_ERROR_UNKNOWN_ENCODING</TD
></TR
><TR
><TD
>XML_ERROR_INCORRECT_ENCODING</TD
></TR
><TR
><TD
>XML_ERROR_UNCLOSED_CDATA_SECTION</TD
></TR
><TR
><TD
>XML_ERROR_EXTERNAL_ENTITY_HANDLING</TD
></TR
></TBODY
></TABLE
><P
></P
>
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="xml.encoding"
>Character Encoding</A
></H2
><P
> PHP's XML extension supports the <A
HREF="http://www.unicode.org/"
TARGET="_top"
>Unicode</A
> character set through
different <I
CLASS="glossterm"
>character encoding</I
>s. There are
two types of character encodings, <I
CLASS="glossterm"
>source
encoding</I
> and <I
CLASS="glossterm"
>target encoding</I
>.
PHP's internal representation of the document is always encoded
with <TT
CLASS="literal"
>UTF-8</TT
>.
</P
><P
> Source encoding is done when an XML document is <A
HREF="function.xml-parse.html"
>parsed</A
>. Upon <A
HREF="function.xml-parser-create.html"
>creating an XML
parser</A
>, a source encoding can be specified (this encoding
can not be changed later in the XML parser's lifetime). The
supported source encodings are <TT
CLASS="literal"
>ISO-8859-1</TT
>,
<TT
CLASS="literal"
>US-ASCII</TT
> and <TT
CLASS="literal"
>UTF-8</TT
>. The
former two are single-byte encodings, which means that each
character is represented by a single byte.
<TT
CLASS="literal"
>UTF-8</TT
> can encode characters composed by a
variable number of bits (up to 21) in one to four bytes. The
default source encoding used by PHP is
<TT
CLASS="literal"
>ISO-8859-1</TT
>.
</P
><P
> Target encoding is done when PHP passes data to XML handler
functions. When an XML parser is created, the target encoding
is set to the same as the source encoding, but this may be
changed at any point. The target encoding will affect character
data as well as tag names and processing instruction targets.
</P
><P
> If the XML parser encounters characters outside the range that
its source encoding is capable of representing, it will return
an error.
</P
><P
> If PHP encounters characters in the parsed XML document that can
not be represented in the chosen target encoding, the problem
characters will be "demoted". Currently, this means that such
characters are replaced by a question mark.
</P
></DIV
></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="xml.examples"
>Some Examples</A
></H1
><P
> Here are some example PHP scripts parsing XML documents.
</P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="example.xml-structure"
>XML Element Structure Example</A
></H2
><P
> This first example displays the stucture of the start elements in
a document with indentation.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN39563"
></A
><P
><B
>Example 1. Show XML Element Structure</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $file = "data.xml";
$depth = array();
function startElement($parser, $name, $attrs) {
global $depth;
for ($i = 0; $i < $depth[$parser]; $i++) {
print " ";
}
print "$name\n";
$depth[$parser]++;
}
function endElement($parser, $name) {
global $depth;
$depth[$parser]--;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="example.xml-map-tags"
>XML Tag Mapping Example</A
></H2
><P
> <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN39569"
></A
><P
><B
>Example 2. Map XML to HTML</B
></P
><P
> This example maps tags in an XML document directly to HTML
tags. Elements not found in the "map array" are ignored. Of
course, this example will only work with a specific XML
document type.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $file = "data.xml";
$map_array = array(
"BOLD" => "B",
"EMPHASIS" => "I",
"LITERAL" => "TT"
);
function startElement($parser, $name, $attrs) {
global $map_array;
if ($htmltag = $map_array[$name]) {
print "<$htmltag>";
}
}
function endElement($parser, $name) {
global $map_array;
if ($htmltag = $map_array[$name]) {
print "</$htmltag>";
}
}
function characterData($parser, $data) {
print $data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
</PRE
></TD
></TR
></TABLE
>
</P
></DIV
></TD
></TR
></TABLE
>
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="example.xml-external-entity"
>XML External Entity Example</A
></H2
><P
> This example highlights XML code. It illustrates how to use an
external entity reference handler to include and parse other
documents, as well as how PIs can be processed, and a way of
determining "trust" for PIs containing code.
</P
><P
> XML documents that can be used for this example are found below
the example (<TT
CLASS="filename"
>xmltest.xml</TT
> and
<TT
CLASS="filename"
>xmltest2.xml</TT
>.)
</P
><P
> <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN39580"
></A
><P
><B
>Example 3. External Entity Example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $file = "xmltest.xml";
function trustedFile($file) {
// only trust local files owned by ourselves
if (!eregi("^([a-z]+)://", $file)
&& fileowner($file) == getmyuid()) {
return true;
}
return false;
}
function startElement($parser, $name, $attribs) {
print "&lt;<font color=\"#0000cc\">$name</font>";
if (sizeof($attribs)) {
while (list($k, $v) = each($attribs)) {
print " <font color=\"#009900\">$k</font>=\"<font
color=\"#990000\">$v</font>\"";
}
}
print "&gt;";
}
function endElement($parser, $name) {
print "&lt;/<font color=\"#0000cc\">$name</font>&gt;";
}
function characterData($parser, $data) {
print "<b>$data</b>";
}
function PIHandler($parser, $target, $data) {
switch (strtolower($target)) {
case "php":
global $parser_file;
// If the parsed document is "trusted", we say it is safe
// to execute PHP code inside it. If not, display the code
// instead.
if (trustedFile($parser_file[$parser])) {
eval($data);
} else {
printf("Untrusted PHP code: <i>%s</i>",
htmlspecialchars($data));
}
break;
}
}
function defaultHandler($parser, $data) {
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
printf('<font color="#aa00aa">%s</font>',
htmlspecialchars($data));
} else {
printf('<font size="-1">%s</font>',
htmlspecialchars($data));
}
}
function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
$publicId) {
if ($systemId) {
if (!list($parser, $fp) = new_xml_parser($systemId)) {
printf("Could not open entity %s at %s\n", $openEntityNames,
$systemId);
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($parser, $data, feof($fp))) {
printf("XML error: %s at line %d while parsing entity %s\n",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser), $openEntityNames);
xml_parser_free($parser);
return false;
}
}
xml_parser_free($parser);
return true;
}
return false;
}
function new_xml_parser($file) {
global $parser_file;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
xml_set_default_handler($xml_parser, "defaultHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
if (!($fp = @fopen($file, "r"))) {
return false;
}
if (!is_array($parser_file)) {
settype($parser_file, "array");
}
$parser_file[$xml_parser] = $file;
return array($xml_parser, $fp);
}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
die("could not open XML input");
}
print "<pre>";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
print "</pre>";
print "parse complete\n";
xml_parser_free($xml_parser);
?>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN39584"
></A
><P
><B
>Example 4. xmltest.xml</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?xml version='1.0'?>
<!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [
<!ENTITY plainEntity "FOO entity">
<!ENTITY systemEntity SYSTEM "xmltest2.xml">
]>
<chapter>
<TITLE>Title &plainEntity;</TITLE>
<para>
<informaltable>
<tgroup cols="3">
<tbody>
<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
</tbody>
</tgroup>
</informaltable>
</para>
&systemEntity;
<sect1 id="about">
<title>About this Document</title>
<para>
<!-- this is a comment -->
<?php print 'Hi! This is PHP version '.phpversion(); ?>
</para>
</sect1>
</chapter>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> This file is included from <TT
CLASS="filename"
>xmltest.xml</TT
>:
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN39589"
></A
><P
><B
>Example 5. xmltest2.xml</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?xml version="1.0"?>
<!DOCTYPE foo [
<!ENTITY testEnt "test entity">
]>
<foo>
<element attrib="value"/>
&testEnt;
<?php print "This is some more PHP code being executed."; ?>
</foo>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
></DIV
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="function.xml-parser-create.html"
>xml_parser_create</A
> — create an XML parser</DT
><DT
><A
HREF="function.xml-set-object.html"
>xml_set_object</A
> — Use XML Parser within an object</DT
><DT
><A
HREF="function.xml-set-element-handler.html"
>xml_set_element_handler</A
> — set up start and end element handlers</DT
><DT
><A
HREF="function.xml-set-character-data-handler.html"
>xml_set_character_data_handler</A
> — set up character data handler</DT
><DT
><A
HREF="function.xml-set-processing-instruction-handler.html"
>xml_set_processing_instruction_handler</A
> —
Set up processing instruction (PI) handler
</DT
><DT
><A
HREF="function.xml-set-default-handler.html"
>xml_set_default_handler</A
> — set up default handler</DT
><DT
><A
HREF="function.xml-set-unparsed-entity-decl-handler.html"
>xml_set_unparsed_entity_decl_handler</A
> —
Set up unparsed entity declaration handler
</DT
><DT
><A
HREF="function.xml-set-notation-decl-handler.html"
>xml_set_notation_decl_handler</A
> — set up notation declaration handler</DT
><DT
><A
HREF="function.xml-set-external-entity-ref-handler.html"
>xml_set_external_entity_ref_handler</A
> — set up external entity reference handler</DT
><DT
><A
HREF="function.xml-parse.html"
>xml_parse</A
> — start parsing an XML document</DT
><DT
><A
HREF="function.xml-get-error-code.html"
>xml_get_error_code</A
> — get XML parser error code</DT
><DT
><A
HREF="function.xml-error-string.html"
>xml_error_string</A
> — get XML parser error string</DT
><DT
><A
HREF="function.xml-get-current-line-number.html"
>xml_get_current_line_number</A
> — get current line number for an XML parser</DT
><DT
><A
HREF="function.xml-get-current-column-number.html"
>xml_get_current_column_number</A
> —
Get current column number for an XML parser
</DT
><DT
><A
HREF="function.xml-get-current-byte-index.html"
>xml_get_current_byte_index</A
> — get current byte index for an XML parser</DT
><DT
><A
HREF="function.xml-parse-into-struct.html"
>xml_parse_into_struct</A
> — Parse XML data into an array structure</DT
><DT
><A
HREF="function.xml-parser-free.html"
>xml_parser_free</A
> — Free an XML parser</DT
><DT
><A
HREF="function.xml-parser-set-option.html"
>xml_parser_set_option</A
> — set options in an XML parser</DT
><DT
><A
HREF="function.xml-parser-get-option.html"
>xml_parser_get_option</A
> — get options from an XML parser</DT
><DT
><A
HREF="function.utf8-decode.html"
>utf8_decode</A
> —
Converts a string with ISO-8859-1 characters encoded with UTF-8
to single-byte ISO-8859-1.
</DT
><DT
><A
HREF="function.utf8-encode.html"
>utf8_encode</A
> — encodes an ISO-8859-1 string to UTF-8</DT
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.wddx-deserialize.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.xml-parser-create.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>wddx_deserialize</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>xml_parser_create</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|