duanbinian2243 2010-11-19 09:29
浏览 43
已采纳

无需循环即可访问SimpleXMLElement属性

I've tried both the SimpleXMLElement XPath approach as well as the DOMDocument XPath approach, but cannot seem to get things straight either way, but at least with the SimpleXMLElement route, I could actually print out the results var and see what's going on. (See Results Below)

I want to use XPath querys without having to loop through the results. I guess I just want to access the results like an array, but the issue seems to be getting past the SimpleXMLElement objects within that results array.

I'm querying an HTML snippet by div with a specific class. There are several divs of this particular class though, so all of them are being returned, which is fine. That's what I want. But now I want to access each and get their values, only they have child divs in most cases.

Here's the line I'm using to execute the XPath query:

$stats = $xml->xpath("//div[contains(@class,'line-item')]");

Here's a print_r() output of the results:

// Output of $stats
Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item
                )
            [div] => Array
                (
                    [0] => Total items shipped
                    [1] => 50
                )
        )
    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item-total
                )
            [div] => Array
                (
                    [0] => TOTAL EARNINGS *
                    [1] => $267.23
                )
        )
    [2] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item-links
                )
            [a] => View full report
        )
    [3] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item
                )
            [div] => Array
                (
                    [0] => Ordered items
                    [1] => 42
                )
        )
    [4] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item
                )
            [div] => Array
                (
                    [0] => Clicks
                    [1] => 428
                )

        )
    [5] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item
                )
            [div] => Array
                (
                    [0] => Conversion
                    [1] => 9.81%
                )
        )
    [6] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line-item-links
                )
            [a] => View full report
        )
)

Now, since I know where each value is, I thought I could just access them like so:

$y['clicks'] = "{$stats[4]['div'][0]}: {$stats[5]['div'][1]}
";
$y['ordered'] = "{$stats[3]['div'][0]}: {$stats[4]['div'][1]}
";
$y['shipped'] = "{$stats[0]['div'][0]}: {$stats[0]['div'][1]}
";
$y['conversion'] = "{$stats[5]['div'][0]}: {$stats[6]['div'][1]}
"; // Doesn't work
$y['rate'] = "{$stats[1]['div'][0]}: {$stats[4]['div'][1]}
"; // Neither encased in {}
$y['total'] = 'Yesterday\'s Earnings: '.$stats[1]['div'][1]."
"; // Nor as concatenated

The obvious problem is the SimpleXMLElement object wedged in between the items in the $stats array and the ['div'] array whose sub-items' values I'm after, which are hiding behind those SimpleXMLElement objects.

So how would I get at those values beyond the SimpleXMLElement objects, without looping through the $stats array? Anything similar to what I've already tried above?

  • 写回答

2条回答 默认 最新

  • drh19790711 2010-11-19 16:37
    关注

    I see that you're accessing stuff like $stats[4]['div'][0]. Array notation is for attributes. Please post your XML and never ever ever rely on print_r() when using SimpleXML.

    I guess that what you want to use is $stats[4]->div although I have no idea what that [0] is for. I assume you've randomly tried to add [0] because of something you saw using print_r(), which is incorrect. Do not use print_r() when dealing with SimpleXML. Do not consider SimpleXML as objects and arrays, they're just ways to access XML. What's important is your XML. SimpleXML uses the familiar object syntax and array syntax, but don't think of it as a collection of objects and arrays, because it's not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?