通过SimpleXML对象循环,或转动整个事情到一个数组 [英] Looping through a SimpleXML object, or turning the whole thing into an array

查看:137
本文介绍了通过SimpleXML对象循环,或转动整个事情到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何遍历虽然返回的SimpleXML对象。

I'm trying to work out how to iterate though a returned SimpleXML object.

我使用了一个名为泰山AWS 的工具包,它连接到亚马逊网络服务(SimpleDB的,S3,EC2,等)。我专门用SimpleDB的。

I'm using a toolkit called Tarzan AWS, which connects to Amazon Web Services (SimpleDB, S3, EC2, etc). I'm specifically using SimpleDB.

我可以把数据放到亚马逊SimpleDB的服务,我可以拿回来。我只是不知道如何处理返回的SimpleXML的对象。

I can put data into the Amazon SimpleDB service, and I can get it back. I just don't know how to handle the SimpleXML object that is returned.

泰山AWS文件说的:

看的响应来浏览响应的报头和主体。注意,这是一个目的,不是一个数组,并且该主体是SimpleXML对象

Look at the response to navigate through the headers and body of the response. Note that this is an object, not an array, and that the body is a SimpleXML object.

下面是返回的SimpleXML对象的示例:

Here's a sample of the returned SimpleXML object:


 [body] => SimpleXMLElement Object
        (
            [QueryWithAttributesResult] => SimpleXMLElement Object
                (
                    [Item] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [Name] => message12413344443260
                                    [Attribute] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [Name] => active
                                                    [Value] => 1
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [Name] => user
                                                    [Value] => john
                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [Name] => message
                                                    [Value] => This is a message.
                                                )

                                            [3] => SimpleXMLElement Object
                                                (
                                                    [Name] => time
                                                    [Value] => 1241334444
                                                )

                                            [4] => SimpleXMLElement Object
                                                (
                                                    [Name] => id
                                                    [Value] => 12413344443260
                                                )

                                            [5] => SimpleXMLElement Object
                                                (
                                                    [Name] => ip
                                                    [Value] => 10.10.10.1
                                                )

                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [Name] => message12413346907303
                                    [Attribute] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [Name] => active
                                                    [Value] => 1
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [Name] => user
                                                    [Value] => fred
                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [Name] => message
                                                    [Value] => This is another message
                                                )

                                            [3] => SimpleXMLElement Object
                                                (
                                                    [Name] => time
                                                    [Value] => 1241334690
                                                )

                                            [4] => SimpleXMLElement Object
                                                (
                                                    [Name] => id
                                                    [Value] => 12413346907303
                                                )

                                            [5] => SimpleXMLElement Object
                                                (
                                                    [Name] => ip
                                                    [Value] => 10.10.10.2
                                                )

                                        )

                                )

                        )

那么,code,我需要打通各个对象的项目?我想通过他们每个人的循环和处理它像一个返回的MySQL查询。例如,我可以查询SimpleDB的,然后循环尽管SimpleXML的这样我就可以在页面上显示的结果。

So what code do I need to get through each of the object items? I'd like to loop through each of them and handle it like a returned mySQL query. For example, I can query SimpleDB and then loop though the SimpleXML so I can display the results on the page.

另外,你怎么把整个事情到一个数组?

Alternatively, how do you turn the whole shebang into an array?

我是新来的SimpleXML,所以我道歉,如果我的问题不够具体。

I'm new to SimpleXML, so I apologise if my questions aren't specific enough.

推荐答案

您可以使用的SimpleXML 对象的(或它的属性)的foreach 循环。如果你想通过所有的记录这样的循环可用于访问和显示数据:

You can use the SimpleXML object (or its properties) in a foreach loop. If you want to loop through all the 'records' something like this can be used to access and display the data:

//Loop through all the members of the Item array 
//(essentially your two database rows).
foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){
    //Now you can access the 'row' data using $Item in this case 
    //two elements, a name and an array of key/value pairs
    echo $Item->Name;
    //Loop through the attribute array to access the 'fields'.
    foreach($Item->Attribute as $Attribute){
        //Each attribute has two elements, name and value.
        echo $Attribute->Name . ": " . $Attribute->Value;
    }
}

注意$档案将SimpleXML对象,如为$属性,所以它们需要被引用为对象,而不是阵列。

Note that $Item will be a SimpleXML object, as is $Attribute, so they need to be referenced as objects, not arrays.

而例如code以上是通过在SimpleXML的对象($ SimpleXML->体 - > QueryWithAttributesResult->项目)的阵列循环,您还可以通过SimpleXML对象(比如循环$ SimpleXML->体 - > QueryWithAttributesResult->项目[0]),这将让你的每个对象的属性。

While the example code above is looping through the arrays in the SimpleXML object ($SimpleXML->body->QueryWithAttributesResult->Item), you can also loop through a SimpleXML object (say $SimpleXML->body->QueryWithAttributesResult->Item[0]), and that would give you each of the object's properties.

SimpleXML对象的每个子元素是XML实体。如果XML实体(标签)不是唯一的,则该元素仅仅是SimpleXML的对象的数组再presenting每个实体。

Each child element of a SimpleXML object is an XML entity. If the XML entity (tag) is not unique, then the element is simply an array of SimpleXML objects representing each entity.

如果你想,这应该创建您的SimpleXML对象中比较常见的行/场阵列(或让你关闭):

If you want, this should create a more common row/fields array from your SimpleXML object (or get you close):

foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){
    foreach($Item->Attribute as $Attribute){
        $rows[$Item->Name][$Attribute->Name] = $Attribute->Value;
    }
}

//Now you have an array that looks like:
$rows['message12413344443260']['active'] = 1;
$rows['message12413344443260']['user'] = 'john';
//etc.

这篇关于通过SimpleXML对象循环,或转动整个事情到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆