PHP XML Parser
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 » http://www.w3.org/XML/.
This PHP extension implements support for James Clark's expat in PHP.
This toolkit lets you parse, but not validate, XML documents.
It supports three source character encodings also provided by PHP: US-ASCII, ISO-8859-1 and UTF-8. UTF-16 is not supported.
This extension lets you create XML parsers and then define handlers for different XML events.
Each XML parser also has a few parameters you can adjust.
An example to show XML element's structure
<?php
$file = "data.xml";
$depth = array();
function startElement($parser, $name, $attrs)
{
global $depth;
if (!isset($depth[$parser])) {
$depth[$parser] = 0;
}
for ($i = 0; $i < $depth[$parser]; $i++) {
echo " ";
}
echo "$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);
?>
Requirements
This extension requires the libxml PHP extension.
This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.
This extension uses an expat compat layer by default. It can use also expat, which can be found at » http://www.jclark.com/xml/expat.html.
The Makefile that comes with expat does not build a library by default, you can use this make rule for that:
Installation
This extension is enabled by default. It may be disabled by using the following option at compile time: --disable-xml.
These functions are enabled by default, using the bundled expat library.
You can disable XML support with --disable-xml. If you compile PHP as a module for Apache 1.3.9 or later, PHP will automatically use the bundled expat library from Apache.
If you don't want to use the bundled expat library, configure PHP with --with-expat-dir=DIR , where DIR should point to the base installation directory of expat.
The Windows version of PHP has built-in support for this extension.
You do not need to load any additional extensions in order to use these functions.
Run-time Configuration
This extension has no configuration directives defined in php.ini.
Resource Types
The xml resource as returned by xml_parser_create() and xml_parser_create_ns() references an xml parser instance to be used with the functions provided by this extension.
Predefined Constants
This XML Parser extension also provided some predefined constants.
In order to use this extension has either been compiled into PHP or dynamically loaded at runtime.
- XML_ERROR_NONE (integer)
- XML_ERROR_NO_MEMORY (integer)
- XML_ERROR_SYNTAX (integer)
- XML_ERROR_NO_ELEMENTS (integer)
- XML_ERROR_INVALID_TOKEN (integer)
- XML_ERROR_UNCLOSED_TOKEN (integer)
- XML_ERROR_PARTIAL_CHAR (integer)
- XML_ERROR_TAG_MISMATCH (integer)
- XML_ERROR_DUPLICATE_ATTRIBUTE (integer)
- XML_ERROR_JUNK_AFTER_DOC_ELEMENT (integer)
- XML_ERROR_PARAM_ENTITY_REF (integer)
- XML_ERROR_UNDEFINED_ENTITY (integer)
- XML_ERROR_RECURSIVE_ENTITY_REF (integer)
- XML_ERROR_ASYNC_ENTITY (integer)
- XML_ERROR_BAD_CHAR_REF (integer)
- XML_ERROR_BINARY_ENTITY_REF (integer)
- XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF (integer)
- XML_ERROR_MISPLACED_XML_PI (integer)
- XML_ERROR_UNKNOWN_ENCODING (integer)
- XML_ERROR_INCORRECT_ENCODING (integer)
- XML_ERROR_UNCLOSED_CDATA_SECTION (integer)
- XML_ERROR_EXTERNAL_ENTITY_HANDLING (integer)
- XML_OPTION_CASE_FOLDING (integer)
- XML_OPTION_TARGET_ENCODING (integer)
- XML_OPTION_SKIP_TAGSTART (integer)
- XML_OPTION_SKIP_WHITE (integer)
- XML_SAX_IMPL (string)
XML Parser Error Codes
These constants defined to show XML error codes.
-
XML_ERROR_NONE
-
XML_ERROR_NO_MEMORY
-
XML_ERROR_SYNTAX
-
XML_ERROR_NO_ELEMENTS
-
XML_ERROR_INVALID_TOKEN
-
XML_ERROR_UNCLOSED_TOKEN
-
XML_ERROR_PARTIAL_CHAR
-
XML_ERROR_TAG_MISMATCH
-
XML_ERROR_DUPLICATE_ATTRIBUTE
-
XML_ERROR_JUNK_AFTER_DOC_ELEMENT
-
XML_ERROR_PARAM_ENTITY_REF
-
XML_ERROR_UNDEFINED_ENTITY
-
XML_ERROR_RECURSIVE_ENTITY_REF
-
XML_ERROR_ASYNC_ENTITY
-
XML_ERROR_BAD_CHAR_REF
-
XML_ERROR_BINARY_ENTITY_REF
-
XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
-
XML_ERROR_MISPLACED_XML_PI
-
XML_ERROR_UNKNOWN_ENCODING
-
XML_ERROR_INCORRECT_ENCODING
-
XML_ERROR_UNCLOSED_CDATA_SECTION
-
XML_ERROR_EXTERNAL_ENTITY_HANDLING
XML Parser Functions
-
utf8_decode — Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
-
utf8_encode — Encodes an ISO-8859-1 string to UTF-8
-
xml_error_string — Get XML parser error string
-
xml_get_current_byte_index — Get current byte index for an XML parser
-
xml_get_current_column_number — Get current column number for an XML parser
-
xml_get_current_line_number — Get current line number for an XML parser
-
xml_get_error_code — Get XML parser error code
-
xml_parse_into_struct — Parse XML data into an array structure
-
xml_parse — Start parsing an XML document
-
xml_parser_create_ns — Create an XML parser with namespace support
-
xml_parser_create — Create an XML parser
-
xml_parser_free — Free an XML parser
-
xml_parser_get_option — Get options from an XML parser
-
xml_parser_set_option — Set options in an XML parser
-
xml_set_character_data_handler — Set up character data handler
-
xml_set_default_handler — Set up default handler
-
xml_set_element_handler — Set up start and end element handlers
-
xml_set_end_namespace_decl_handler — Set up end namespace declaration handler
-
xml_set_external_entity_ref_handler — Set up external entity reference handler
-
xml_set_notation_decl_handler — Set up notation declaration handler
-
xml_set_object — Use XML Parser within an object
-
xml_set_processing_instruction_handler — Set up processing instruction (PI) handler
-
xml_set_start_namespace_decl_handler — Set up start namespace declaration handler
-
xml_set_unparsed_entity_decl_handler — Set up unparsed entity declaration handler