PHP: Hypertext Preprocessor (original) (raw)

xml_parser_create

(PHP 4, PHP 5, PHP 7, PHP 8)

xml_parser_create — Create an XML parser

Description

Parameters

encoding

The input encoding is automatically detected, so that theencoding parameter specifies only the output encoding. If empty string is passed, the parser attempts to identify which encoding the document is encoded in by looking at the heading 3 or 4 bytes. The default output charset is UTF-8. The supported encodings are ISO-8859-1, UTF-8 andUS-ASCII.

Return Values

Returns a new XMLParser instance.

Changelog

Version Description
8.0.0 This function returns an XMLParser instance now; previously, a resource was returned, or false on failure.
8.0.0 encoding is nullable now.

See Also

Found A Problem?

marek995 at seznam dot cz

14 years ago

`I created a function, which combines xml_paresr_create and all functions around.

array=strsplit(array = str_split(array=strsplit(file, 1); $count = false; $text = ""; $end = false; foreach($array as $temp) { switch($temp) { case "<": between($text); $text = ""; $count = true; $end = false; break; case ">": if($end == true) {end_tag($text);} else {start_tag($text);} $text = ""; break; case "/": if($count == true) {$end = true;} else {$text = $text . "/";} break; default: count=false;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>e</mi><mi>x</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">text=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6151em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>text.count = false; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>e</mi><mi>x</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">text = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>text . count=false;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>e</mi><mi>x</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/xtex">text=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6151em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>text.temp; } } } ?>

The input value is a string.
It calls functions start_tag() , between() and end_tag() just like the original xml parser.

But it has a few differences:

definition of the handling functions is:

No other attributes

`

jcalvert at gmx dot net

21 years ago

`To maintain compatibility between PHP4 and PHP5 you should always pass a string argument to this function. PHP4 autodetects the format of the input if you leave it out whereas PHP5 will assume the format to be ISO-8859-1 (and choke on the byte order marker of UTF-8 files).

Calling the function as will cause both versions of PHP to autodetect the format.

`

Tobbe

19 years ago

`The above "XML to array" code does not work properly if you have several tags on the same level and with the same name, example:

This is a real error... This is a second error... Lots of errors today... This is the last error...

It will then only display the first -tag.
In this case you will need to number the tags automatically or maybe have several arrays for each new element.

`

php at stock-consulting dot com

20 years ago

Even though I passed "UTF-8" as encoding type PHP (Version 4.3.3) did *not* treat the input file as UTF-8. The input file was missing the BOM header bytes (which may indeed be omitted, according to RFC3629...but things are a bit unclear there. The RFC seems to make mere recommendations concering the BOM header). If you want to sure that PHP treats an UTF-8 encoded file correctly, make sure that it begins with the corresponding 3 byte BOM header (0xEF 0xBB 0xBF)

bishop at php dot net

6 years ago

`Internals has proposed[1] changing this extension from resource-based to object-based. When this change is made, xml_parser_create will return an object, not a resource. Application developers are encouraged to replace any checks for explicit success, like:

With a check for explicit failure:
<?php
$res = xml_parser_create(/.../);
if (false === $res) {
// ...
}

`