Search code examples
phparrayssimplexml

getting elemints out of simpleXML Array


I am trying to get elements out of a simpleXML array and for some reason I am unable to call them.

Here is the array.

Array
(
    [0] => SimpleXMLElement Object
        (
            [NameChangeIndicator] => N
            [NameChangeDate] => SimpleXMLElement Object
                (
                )

            [PreviousName] => SimpleXMLElement Object
                (
                )

            [Score] => 53
            [NumberOfSubs] => SimpleXMLElement Object
                (
                )

            [NumberOfJU] => SimpleXMLElement Object
                (
                )

            [DateLastJU] => SimpleXMLElement Object
                (
                )

            [NumberActPrincipals] => 1
            [NumberActPrincipalsJU] => SimpleXMLElement Object
                (
                )

            [LastestBankCode] => SimpleXMLElement Object
                (
                )

            [LastestBankCodeDate] => SimpleXMLElement Object
                (
                )

            [NumberRDs] => SimpleXMLElement Object
                (
                )

            [LiqIndicator] => SimpleXMLElement Object
                (
                )

            [TotEnqLast12Mth] => SimpleXMLElement Object
                (
                )

            [TotEnqLast3Mth] => SimpleXMLElement Object
                (
                )

            [RefsNoOfReferences] => SimpleXMLElement Object
                (
                )

            [RefsHighMthPurchases] => SimpleXMLElement Object
                (
                )

            [RefsHighMthPurchasesTermGiven] => SimpleXMLElement Object
                (
                )

            [RefsHighMthPurchasesTermTaken] => SimpleXMLElement Object
                (
                )

            [RefsLowMthPurchases] => SimpleXMLElement Object
                (
                )

            [RefsLowMthPurchasesTermGiven] => SimpleXMLElement Object
                (
                )

            [RefsLowMthPurchasesTermTaken] => SimpleXMLElement Object
                (
                )

            [KissNoOfSuppliers] => SimpleXMLElement Object
                (
                )

            [KissNoOfODSuppliers] => SimpleXMLElement Object
                (
                )

            [KissAmountOS] => SimpleXMLElement Object
                (
                )

            [KissAmountOD] => SimpleXMLElement Object
                (
                )

            [KissPercntage] => SimpleXMLElement Object
                (
                )

            [LatestBankCodeDesc] => SimpleXMLElement Object
                (
                )

            [HoldingCmpName] => SimpleXMLElement Object
                (
                )

        )

)

So I am doing the following call to get the array.

$new_str = htmlspecialchars_decode($str);
$new_str = str_replace('<?xml version="1.0" encoding="UTF-8"?>','',$new_str);
$xml = simplexml_load_string($new_str);
$dom = new SimpleXMLElement($new_str);
$xml_array = $dom->xpath("//*[name()='ReportSummary']");
echo "{$xml_array[0]['Score']}"; 

But I am unable to pull the object out of the Array. I am not sure if the array is being correctly sent back to me due to the fact that if I don't decode the string I don't get a array back. The weird thing is that in the array I keep on seeing "SimpleXMLElement Object" and I am not sure if that is correct.

Any help will be appreciated.


Solution

  • As the dump output says, SimpleXML is a type of object, not a way of creating arrays.

    These two lines are different ways of writing the same thing, you only need one of them; in either case you end up with a SimpleXMLElement object:

    $xml = simplexml_load_string($new_str);
    $xml = new SimpleXMLElement($new_str);
    

    The outer array you are seeing is to hold the results of the XPath query, since they can come from anywhere in the XML tree. It is an array of SimpleXMLElement objects.

    For how to access data using SimpleXML, see the basic usage page in the PHP manual.

    In your case, Score is an element of the document, so needs to be accessed with the $node->property syntax.

    Here's a tidied up version of your code:

    $new_str = htmlspecialchars_decode($str);
    // Are you sure the next line is necessary? That looks like a valid XML opening to me.
    $new_str = str_replace('<?xml version="1.0" encoding="UTF-8"?>','',$new_str);
    
    $xml = simplexml_load_string($new_str);
    
    // I think this simpler XPath expression means the same as yours, but I might be wrong
    $xpath_results = $xml->xpath('//ReportSummary');
    
    // Beware that the XPath could return no matches, in which case the following 
    // would give an error. Best to check count($xpath_results) > 0 first.
    echo $xpath_results[0]->Score;