XML Parser Function in PHP: Part 2

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:

  1.  XML Parser Function in PHP: Part 1

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

xml-get-current-line-no-function-in-php.jpg

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

xml-get-error-code-function-in-php.jpg 

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();

function start($parser,$element_name,$element_attrs)

{

switch($element_name)

{

       case "Mesage":

 

        echo "-- Message --<br/>";

 

        break;

 

        case "TO":

 

        echo "To: ";

 

        break;

 

        case "FROM":

 

        echo "From: ";

 

        break;

 

        case "HEADING":

 

        echo "Heading: ";

 

        break;

 

        case "BODY":

 

        echo "Message: ";

 

}

}

function stop($parser,$element_name)

{

echo "<br/>";

}

function char($parser,$xml_data)

{

echo $xml_data;

}

xml_set_element_handler($parser,"start","stop");

xml_set_character_data_handler($parser,"char");

$fp=fopen("test.xml","r");

while ($xml_data=fread($fp,4096))

{

xml_parse($parser,$xml_data,feof($fp)) or

die (sprintf("XML Error: %s at line %d",

xml_error_string(xml_get_error_code($parser)),

xml_get_current_line_number($parser)));

}

xml_parser_free($parser);

?>

Output

xml-parse-function-in-php.jpg

PHP xml_parser_into_struct() Function

The PHP XML Parser "xml_parser_into_struct" function parses XML data into an array structure. This function returns 0 (zero) for failure otherwise returns 1 for success.

Syntax

xml_parser_into_struct(parser, xml, valueArr,indexArr)

Parameter of the xml_parser_into_struct 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.
valueArr It specifies the target array for the XML data.
indexArr It specifies the target array for the index data.

Example

An example of the function is:


<?
php

$file = 'test.xml';

$xmlparser = xml_parser_create();

echo "<pre>";

$fp = fopen($file, 'r');

$xmldata = fread($fp, 4096);

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);

print_r($values);

?>

Output

xml-parser-into-struct-function-in-php.jpg 

PHP xml_parser_create_ns() Function

The PHP XML Parser "xml_parser_create_ns" function creates an XML parser with namespace support and returns a resource handle for the new XML parser.

Syntax

xml_parser_create_ns(encoding, separator)

Parameters of the xml_parser_create_ns function

The parameters of the function are:

Parameter Description
encoding It specifies the output encoding; it's possible values are:
  • ISO-8859-1
  • UTF-8
  • US-ASCII
separator It specifies the output separator for tag name and namespace.

Example

An example of the function is:

<?php

$xmlparser = xml_parser_create_ns();

xml_parser_free($xmlparser);

?>