doudao8283 2015-02-17 05:43
浏览 31
已采纳

从easyXML数组中获取elemints

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.

  • 写回答

2条回答 默认 最新

  • duanbin198788 2015-02-18 15:14
    关注

    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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算