Introduction
In this article I describe the PHP XML Parser functions xml_get_current_line_number, xml_get_error_code, xml_parse, xml_parser_into_struct and xml_parser_create_ns. To learn some other PHP functions go to:
PHP xml_get_current_line_number() Function
The PHP XML Parser "xml_get_current_line_number" function gets the current line number for an XML parser. This function returns false if the parser does not refer to a valid parser or it returns which line the parser is currently at in its data buffer.
Syntax
| xml_get_current_line_number(parser) |
Parameter of the xml_get_current_line_number function
The parameter of the function is:
| Parameter | Description |
| parser | It specifies the XML Parser to use. |
Example
An example of the function is:
<?php
$file = 'test.xml';
$XmlParser = xml_parser_create();
$fp = fopen($file, 'r');
while ($xmldata = fread($fp, 4096))
{
if (!xml_parse($XmlParser,$xmldata,feof($fp)))
{
die( print "ERROR: "
. xml_error_string(xml_get_error_code($XmlParser))
. "<br />"
. "Line: "
. xml_get_current_line_number($XmlParser)
. "<br />"
. "Column: "
. xml_get_current_column_number($XmlParser)
. "<br />");
}
}
xml_parser_free($XmlParser);
?>
Output

PHP xml_get_error_code() Function
The PHP XML Parser "xml_get_current_line_number" function gets the XML parser error code. This function returns false if the parser does not refer to a valid parser.
Syntax
| xml_get_current_line_number(parser) |
Parameter of the xml_get_error_code function
The parameter of the function is:
| Parameter | Description |
| parser | It specifies the XML Parser to use. |
Example
An example of the function is:
<?php
$file = 'test.xml';
$XmlParser = xml_parser_create();
$fp = fopen($file, 'r');
while ($xmldata = fread($fp, 4096))
{
if (!xml_parse($XmlParser,$xmldata,feof($fp)))
{
die( print "ERROR: "
. xml_error_string(xml_get_error_code($XmlParser))
. "<br />"
. "Line: "
. xml_get_current_line_number($XmlParser)
. "<br />"
. "Column: "
. xml_get_current_column_number($XmlParser)
. "<br />");
}
}
xml_parser_free($XmlParser);
?>
Output
PHP xml_parse() Function
The PHP XML Parser "xml_parse" function starts parsing an XML document and this function returns 1 on success otherwise 0 (zero) on failure.
Syntax
| xml_parse (parser, xml, end) |
Parameters of the xml_parse function
The parameters of the function are:
| Parameter | Description |
| parser | It specifies the XML parser to use. |
| xml | It specifies the XML data to parse. |
| end | It is an optional parameter; if this parameter is true then the data in the "xml" parameter is the last piece of data sent to this parse. |
Example
An example of the function is:
<?php
$parser=xml_parser_create();


Join the conversation! Your thoughts help the community grow.