循环遍历 SimpleXML 对象,或将整个对象转换为数组 [英] Looping through a SimpleXML object, or turning the whole thing into an array

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

问题描述

我正在研究如何遍历返回的 SimpleXML 对象.

我正在使用一个名为 Tarzan AWS 的工具包,它连接到 Amazon Web Services(SimpleDB、S3、EC2 等).我专门使用 SimpleDB.

我可以将数据放入 Amazon SimpleDB 服务中,然后我可以取回它.我只是不知道如何处理返回的 SimpleXML 对象.

Tarzan AWS 文档是这样说的:

<块引用>

查看响应以浏览响应的标题和正文.请注意,这是一个对象,而不是数组,并且主体是一个 SimpleXML 对象.

这是返回的 SimpleXML 对象的示例:

<前>[body] => SimpleXMLElement 对象([QueryWithAttributesResult] => SimpleXMLElement 对象([项目] => 数组([0] => SimpleXMLElement 对象([名称] => 消息12413344443260[属性] => 数组([0] => SimpleXMLElement 对象([名称] => 活跃[值] => 1)[1] => SimpleXMLElement 对象([名称] => 用户[值] => 约翰)[2] => SimpleXMLElement 对象([名称] => 消息[值] => 这是一条消息.)[3] => SimpleXMLElement 对象([名称] => 时间[值] => 1241334444)[4] => SimpleXMLElement 对象([名称] => id[值] => 12413344443260)[5] => SimpleXMLElement 对象([名称] => ip[值] => 10.10.10.1)))[1] => SimpleXMLElement 对象([名称] => 消息12413346907303[属性] => 数组([0] => SimpleXMLElement 对象([名称] => 活跃[值] => 1)[1] => SimpleXMLElement 对象([名称] => 用户[值] => 弗雷德)[2] => SimpleXMLElement 对象([名称] => 消息[值] => 这是另一条消息)[3] => SimpleXMLElement 对象([名称] => 时间[值] => 1241334690)[4] => SimpleXMLElement 对象([名称] => id[值] => 12413346907303)[5] => SimpleXMLElement 对象([名称] => ip[值] => 10.10.10.2))))

那么我需要什么代码来遍历每个对象项?我想遍历它们中的每一个并像返回的 mySQL 查询一样处理它.例如,我可以查询 SimpleDB,然后通过 SimpleXML 循环,以便在页面上显示结果.

或者,你如何将整个shebang变成一个数组?

我是 SimpleXML 的新手,如果我的问题不够具体,我深表歉意.

解决方案

您可以在 foreach 循环中使用 SimpleXML 对象(或其属性).如果你想遍历所有的记录",像这样的东西可以用来访问和显示数据:

//循环遍历Item数组的所有成员//(本质上是您的两个数据库行).foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){//现在您可以在这种情况下使用 $Item 访问行"数据//两个元素,一个名称和一个键/值对数组echo $Item->Name;//循环遍历属性数组以访问字段".foreach($Item->Attribute as $Attribute){//每个属性有两个元素,名称和值.回声 $Attribute->Name .":.$属性->值;}}

请注意,$Item 将是一个 SimpleXML 对象,$Attribute 也是如此,因此它们需要作为对象而不是数组来引用.

虽然上面的示例代码循环访问 SimpleXML 对象中的数组($SimpleXML->body->QueryWithAttributesResult->Item),但您也可以循环访问 SimpleXML 对象(比如 $SimpleXML->body->QueryWithAttributesResult->Item[0]),这会给你每个对象的属性.

SimpleXML 对象的每个子元素都是一个 XML 实体.如果 XML 实体(标记)不是唯一的,则该元素只是表示每个实体的 SimpleXML 对象数组.

如果你愿意,这应该从你的 SimpleXML 对象创建一个更常见的行/字段数组(或者让你接近):

foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){foreach($Item->Attribute as $Attribute){$rows[$Item->Name][$Attribute->Name] = $Attribute->Value;}}//现在你有一个看起来像这样的数组:$rows['message12413344443260']['active'] = 1;$rows['message12413344443260']['user'] = 'john';//等等.

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

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

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.

The Tarzan AWS documentation says this:

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.

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
                                                )

                                        )

                                )

                        )

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?

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

解决方案

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;
    }
}

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

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.

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.

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天全站免登陆