Salman Raza
Salman Raza

Reputation: 350

Manipulation of XML in PHP

I have an API call which returns a XML string, I have converted that response with new SimpleXmlElement so that it returns me a XML object. Below is the result

SimpleXMLElement Object
(
    [0] => <root>
                <PINCODELIST>
                    <PINCODE>152001</PINCODE>
                    <CITYNAME>GURDASPUR</CITYNAME>
                    <ACTIVEFLAG>Y</ACTIVEFLAG>
                    <EDIT_DATE>09 May 2015 10:47:20</EDIT_DATE>
                    <ROUTINGCODE>N/PB/1026/FRZPR</ROUTINGCODE>
                </PINCODELIST>
            </root>
)

How do I access each element in root tag assuming it can have X number of PINCODELIST tags.

I tried $res[0]->root but it gaves me SimpleXMLElement Object ( )

Any ideas ?

Upvotes: 0

Views: 83

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33823

I can't comment on SimpleXmlElement or the associated methods but using the standard DOMDocument is fairly straightforward. It takes a string as input, though can easily be modifed to load an XML file if preferred.

/* helper function to return the value of a node */
function gnv( $node,$tag ){
    return $node->getElementsByTagName( $tag )->item(0)->nodeValue;
}

$data="
    <root>
        <PINCODELIST>
            <PINCODE>152001</PINCODE>
            <CITYNAME>GURDASPUR</CITYNAME>
            <ACTIVEFLAG>Y</ACTIVEFLAG>
            <EDIT_DATE>09 May 2015 10:47:20</EDIT_DATE>
            <ROUTINGCODE>N/PB/1026/FRZPR</ROUTINGCODE>
        </PINCODELIST>
        <PINCODELIST>
            <PINCODE>152003</PINCODE>
            <CITYNAME>Mumbai</CITYNAME>
            <ACTIVEFLAG>N</ACTIVEFLAG>
            <EDIT_DATE>09 May 2015 11:21:20</EDIT_DATE>
            <ROUTINGCODE>O/PB/1036/FRZPR</ROUTINGCODE>
        </PINCODELIST>
    </root>";

$url='http://<SERVER>/jhds/services/ws_webx_dataexchange.asmx/PincodeSync?FromDate=&CustomerCode=CC000200115';
$data=file_get_contents( $url );


/* For storing pincodes and data */
$codes=array();

libxml_use_internal_errors( true );

$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->preserveWhiteSpace=true;
$dom->strictErrorChecking=false;
$dom->substituteEntities=false;
$dom->recover=true;
$dom->formatOutput=false;

/* Here you load your xml data as a string */
$dom->loadXML( html_entity_decode( $data ) );
$parse_errs=serialize( libxml_get_last_error() );

libxml_clear_errors();

$pincodes=$dom->getElementsByTagName('PINCODELIST');
foreach( $pincodes as $index => $node ) {
    if( $node->nodeType==XML_ELEMENT_NODE ){

        $codes[ gnv( $node, 'PINCODE' ) ]=(object)array(
            'CITYNAME'      =>  gnv( $node, 'CITYNAME' ),
            'ACTIVEFLAG'    =>  gnv( $node, 'ACTIVEFLAG' ),
            'EDIT_DATE'     =>  gnv( $node, 'EDIT_DATE' ),
            'ROUTINGCODE'   =>  gnv( $node, 'ROUTINGCODE' )
        );
    }
}
$dom=null;
/* debug output */
echo '<pre>',print_r($codes,true),'</pre>';

/* To access specific items by their pincode later */
echo $codes['152001']->ROUTINGCODE;

Upvotes: 2

Alex Andrei
Alex Andrei

Reputation: 7283

After loading the xml file you access the nodes after root.
In your example is an array or list of xml nodes which after parsing become an array of SimpleXMLElement objects.

Assuming the below xml structure

<root>
    <PINCODELIST>
        <PINCODE>152001</PINCODE>
        <CITYNAME>GURDASPUR</CITYNAME>
        <ACTIVEFLAG>Y</ACTIVEFLAG>
        <EDIT_DATE>09 May 2015 10:47:20</EDIT_DATE>
        <ROUTINGCODE>N/PB/1026/FRZPR</ROUTINGCODE>
    </PINCODELIST>
    <PINCODELIST>
        <PINCODE>152002</PINCODE>
        <CITYNAME>GURDASPUR</CITYNAME>
        <ACTIVEFLAG>Y</ACTIVEFLAG>
        <EDIT_DATE>09 May 2015 10:47:20</EDIT_DATE>
        <ROUTINGCODE>N/PB/1026/FRZPR</ROUTINGCODE>
    </PINCODELIST>
</root>

Use the following php to access each element

$pincodelist = simplexml_load_file('xml.xml');

foreach($pincodelist as $pincode){
    print $pincode->PINCODE . PHP_EOL;
}

Will output

152001
152002

If you'd like to access a single node from your object, you can do so like this:

print $res[0]->PINCODE;

Where $res is the result of simplexml_load_*, [0] is the index of the array and PINCODE is the node name.

Upvotes: 0

Related Questions