XML Parser Function in PHP: Part 1

Introduction

The eXtensible Markup language (XML) is  a data format for structured document interchange on the web and the PHP XML functions lets you parse, but not validate, XML documents. Basically XML parser functions creates parser objects and defines handlers for XML events. The XML parser functions are part of the PHP core so there is no installation needed to use these functions. The PHP XML Parser has many functions and I describe some of them here. This article describes the PHP XML Parser  functions utf8_decode, utf8_encode, xml_error_string, xml_get_current_byte_index and xml_get_current_column_number.

PHP utf8_decode() Function

The PHP XML Parser "utf8_decode" function is used to convert a string with ISO-8859-1 characters encoded with UTF-8 single-byte ISO-8859-1 or you can say this function returns the ISO-8859-1 translation of data.

Syntax

utf8_decode(string)

Parameter of the utf8_decode function

The parameter of the function is:
 
Parameter Description
string It specifies the string to be decoded.

PHP utf8_encode() Function

The PHP XML Parser "utf8_encode" function encodes an ISO-8859 string to UTF-8 and returns the UTF-8 translation of data.

Syntax

utf8_encode(string)

Parameter of the utf8_decode function

The parameter of the function is:
 
Parameter Description
string It specifies the string to be encoded.

PHP xml_error_string() Function

The PHP XML Parser "xml_error_string" function gets the XML parser error string and returns a string with a textual description of the error code otherwise false if no description was found.

Syntax

xml_error_string(errorcode)

Parameter of the xml_error_string function

The parameter of the function is:
 
Parameter Description
errorcode It specifies the error code to use.

Example

An example of the function is:

 

<?php

$xmlfile = 'test.xml';

$XmlParser = xml_parser_create();

// open a file and read data

 $fp = fopen($xmlfile, '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-string-function-in-php.jpg

PHP xml_get_current_byte_index() Function

The PHP XML Parser "xml_get_current_byte_index" function gets the current byte index for an XML Parser and returns false if the parser does not refer to a valid parser, or else it returns which byte index the parser is currently at in its data buffer.

Syntax

xml_get_current_byte_index(parser)

Parameter of the xml_get_current_byte_index 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

$xmlfile = 'test.xml';

$xmlparser = xml_parser_create();

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

while ($xmldata = fread($fp, 4096)) {

if (xml_parse($xmlparser, $xmldata, feof($fp))) {

print "The current byte index is: "

. xml_get_current_byte_index($xmlparser) . "<BR />";

}

else

{

// if parsing fails print the error description and line number

die(print "ERROR: "

. xml_error_string(xml_get_error_code($xmlparser))

. "<BR />"

. "Line: "

. xml_get_current_line_number($xmlparser));

}

}

xml_parser_free($xmlparser);

?>


Output

xml-get-current-byte-index-function-in-php.jpg

PHP xml_get_current_column_number() Function

The PHP XML Parser "xml_get_current_column_number" function gets the current column number for an XML parser and returns false if the parser does not refer to a valid parser.

Syntax

xml_get_current_column_number(parser)

Parameter of the xml_get_current_column_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))

{

// parse the data chunk

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-column-number-function-in-php.jpg


Similar Articles