PclXml is able to walk through the XML hierarchy to find a
specific XML token and return its value.
Get the value fo the top most XML token :
<?php
if (($v_result = $v_xml->get_value()) === PCLXML_ERR_ERROR) {
echo 'Unable to find a value :'.$v_xml->error_message(true);
exit;
}
else {
echo "Value is : ".$v_value;
}
?>
|
Get the value of a child XML token of the current object :
File is :
<members>
<user>
<name>Martin</name>
</user>
</members>
PHP Code is :
<?php
if (($v_result = $v_xml->get_value('members.user.name')) === PCLXML_ERR_ERROR) {
echo 'Unable to find a value :'.$v_xml->error_message(true);
exit;
}
else {
echo "Value is : ".$v_value;
}
?>
|
If a token has several childs of same name, then the index
of the requested child must be used. Else only the first one
in the child list will be returned :.
File is :
<members>
<user>
<name>Martin</name>
</user>
<user>
<name>Lernou</name>
</user>
</members>
PHP Code is :
<?php
if (($v_result = $v_xml->get_value('members.user[1].name')) === PCLXML_ERR_ERROR) {
echo 'Unable to find a value :'.$v_xml->error_message(true);
exit;
}
else {
echo "Value is : ".$v_value;
}
?>
|
2 - Set XML Token value
The value of the current XML token or of any token in the hierarchy
can be set by the set_value() method.
To set the value of the current XML token, just give the value
:
<?php
if (($v_result = $v_xml->set_value('The new value to set')) === PCLXML_ERR_ERROR) {
echo 'Unable to set a value :'.$v_xml->error_message(true);
exit;
}
?>
|
To set the value of a child XML token of a PclXmlTag Object,
give the value and the path to a valid child object :
<?php
if (($v_result = $v_xml->set_value('Martin', 'members.user.name')) === PCLXML_ERR_ERROR) {
echo 'Unable to set a value :'.$v_xml->error_message(true);
exit;
}
?>
|
3 - Get XML Token attribute
Each XML token can have a value but also attributes. PclXml
allow for access of the attributes in the same way as getting
the value of a token :
File is :
<members>
<user name="Malin" first_name="John"></user>
<user name="Marlin" first_name="Jake"></user>
<user name="Lernou" first_name="Paul"></user>
</members>
PHP Code is :
<?php
if (($v_result = $v_xml->get_attribute('members.user[2].name')) === PCLXML_ERR_ERROR) {
echo 'Unable to find a value :'.$v_xml->error_message(true);
exit;
}
else {
// Result will be "Lernou"
echo "Length attribute value is : ".$v_value;
}
?>
|
4 - Set XML Token attribute
Attributes can be set for current PclXmlTag object and for
childs of this object.
Setting attribute for the current object :
File is :
<members type="list">
<user name="Malin" first_name="John"></user>
<user name="Marlin" first_name="Jake"></user>
<user name="Lernou" first_name="Paul"></user>
</members>
PHP Code is :
<?php
if (($v_result = $v_xml->set_attribute('type', 'tree')) === PCLXML_ERR_ERROR) {
echo 'Unable to set an attribute :'.$v_xml->error_message(true);
exit;
}
?>
|
Setting attribute for a child object :
<?php
if (($v_result = $v_xml->set_attribute('members.user[2].name', 'Lernou')) === PCLXML_ERR_ERROR) {
echo 'Unable to set an attribute :'.$v_xml->error_message(true);
exit;
}
?>
|
Note : If the attribute does not exist, then it is created.
If you want to set only existing attribute, use first get_attribute()
to see if attribute exists.
5 - Walk Through Childs
Sometimes you don't know which childs are in a XML Token. The
walk through child method will allow you to access the childs
of a specific PclXmlObject in a sequential way :
<?php
$v_xml->walk_reset();
while (($v_result = $v_xml->walk_childs()) !== PCLXML_ERR_ERROR) {
echo "Child name : '".$v_result['name']."'<br>";
// Also a direct reference to the PclXmltag child
echo "Child name : '".$v_result['child']->get_name()."'<br>";
}
$v_xml->walk_reset();
?>
|
Child walk can also be done on child objects :
<?php
$v_xml->walk_reset();
while (($v_result = $v_xml->walk_childs('core.mysql')) !== PCLXML_ERR_ERROR) {
echo "Child name : '".$v_result['name']."'<br>";
// Also a direct reference to the PclXmltag child
echo "Child name : '".$v_result['child']->get_name()."'<br>";
}
$v_xml->walk_reset();
?>
|
walk_reset() must be used to initiate the walk through the
PclXmlTag object. Notice that walk_reset() apply both for child
walk and attribute walk.
Note also that it is good practice to reset the walk after operation
to limit ressource usage and side effects.
6 - Walk Through Attributes
Sometimes you don't know which attributes are defined in a
XML Token. The walk through attribute method will allow you
to access the attribute of a specific PclXmlObject in a sequential
way :
<?php
$v_xml->walk_reset();
while (($v_result = $v_xml->walk_attributes()) !== PCLXML_ERR_ERROR) {
echo "Attribute name : '".$v_result['name']."' = '".$v_result['value']."'<br>";
}
$v_xml->walk_reset();
?>
|
Attribute walk can also be done on child objects :
<?php
$v_xml->walk_reset();
while (($v_result = $v_xml->walk_attributes('core.mysql')) !== PCLXML_ERR_ERROR) {
echo "Attribute name : '".$v_result['name']."' = '".$v_result['value']."'<br>";
}
$v_xml->walk_reset();
?>
|
walk_reset() must be used to initiate the walk through the
PclXmlTag object. Notice that walk_reset() apply both for child
walk and attribute walk.
Note also that it is good practice to reset the walk after operation
to limit ressource usage and side effects.
7 - Get a Child by Path
You may want to have access to a specific child of the current
PclXmlTag object. The get_child() method gives an access to
a reference of the requested child :
<?php
if (($v_child = &$v_xml->get_child('core.mysql')) === PCLXML_ERR_ERROR) {
echo 'Unable to get a child with this path :'.$v_xml->error_message(true);
exit;
}
?>
|
7 - Get or Set Name of an XML Token
<?php
$v_name = $v_xml->get_name();
if (($v_result = $v_xml->set_name('new_name')) === PCLXML_ERR_ERROR) {
echo 'Unable to set a name for this object :'.$v_xml->error_message(true);
exit;
}
?>
|
Advanced
Features
To Be Completed