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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,