add

Friday, September 7, 2012

Simple XML parsing

Xml File
"
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>originTransactionID</name>
<value><string>102511650088881951</string></value>
</member>
<member>
<name>responseCode</name>
<value><i4>100</i4></value>
</member>
</struct>
</value>
</param>
</params
</methodResponse>"

PHP :
" $xml = new SimpleXMLElement($xml);
$items = $xml->xpath('/methodResponse/fault/value/struct/member/name');
                $firstArr=$this->objectsIntoArray($items[0]);//convert XML Object to Php Array


function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}

if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}


"

No comments:

Post a Comment