| | |
|
|
|
XML handling in PHP
The Extensible Markup Language (XML) is a general purpose markup language that is commonly used to transfer data/information between two cooperating entities such as applications, servers or clients. The PHP support in Project Zero supports rendering PHP variables as XML and manipulation of XML strings as PHP variables.
There are a couple of different ways to handle XML data in PHP.
API
From PHP scripts you can directly manipulate XML data using Simple XML-like objects derived from the following extension functions.
xml_decode
xml_encode
The following example illustrates how to parse XML data that has been sent in the POST body in to PHP variables.
Consider the following XML document for the examples below.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="345435">
<name>John Smith</name>
</employee>
</employees>
The object returned from xml_decode has the following properties. Any element in the original xml document can be accessed as fields of the object. Any attribute of an element can be accessed as array indexed by the attribute name or using the getAttribute method of the object.
<?php
// The raw post body is available in the variable $HTTP_RAW_POST_DATA
$employees = xml_decode($HTTP_RAW_POST_DATA);
// Get the employee element
$employee = $employees->employee[0];
// Get the id attribute
$id = $employee['id'];
// Get the name
$name = $employee->name->toString();
?>
The same example using PHP streams, with the attribute accessed using getAttribute().
<?php
$input = fopen("php://input", 'r');
// The raw post body is available in the variable $HTTP_RAW_POST_DATA
$employees = xml_decode($HTTP_RAW_POST_DATA);
// Get the employee element
$employee = $employees->employee[0];
// Get the id attribute
$id = $employee['id'];
// Get the name
$name = $employee->name->toString();
?>
All the attributes of an element can be retrieved as an array of attribute name value pairs using the attributes() method of the object as illustrated below.
<?php
// The raw post body is available in the variable $HTTP_RAW_POST_DATA
$employees = xml_decode($HTTP_RAW_POST_DATA);
$employee = $employees->employee[0];
// Get all the attributes of the first employee
foreach ($employee->attributes() as $attrname => $attrvalue) {
}
?>
The function xml_encode can be used to encode PHP variables composed of primitive PHP types or arrays into an XML string. The following example shows an array being converted into a xml string.
<?php
$var = array('name' => "Smith", 'address' => array('line1' => 'This lane', 'line2' => 'Somewhere'));
echo xml_encode($var, false, "employee");
?>
This would return the following XML document.
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<employee>
<name>Smith</name>
<address><line1>This lane</line1><line2>Somewhere</line2></address>
</employee>
The first argument to xml_encode is the variable to be encoded. The second argument is a boolean argument indicating whether id references should be displayed and the third argument specifies the XML root element for the document.
Response rendering and transformations
XML data can be transformed or rendered using Project Zero's global context as follows.
<?php
$var = array('name' => "Smith", 'address' => array('line1' => 'This lane', 'line2' => 'Somewhere'));
zput('/request/view', 'XML');
zput('/request/xml/output', $var);
zput('/request/xml/rootElement', 'employees');
zput('/request/xml/idRefs',true);
render_view();
?>
|
-- madhu - 11 Dec 2007
|
r5 - 18 Jan 2008 - 20:46:11 - paynel
|
|
|
| | |