循环访问 SimpleXML 对象,或将整个对象转换为数组
我正在尝试弄清楚如何迭代返回的SimpleXML对象。
我正在使用一个名为Tarzan AWS的工具包,它连接到Amazon Web Services(SimpleDB,S3,EC2等)。我专门使用SimpleDB。
我可以将数据放入 Amazon SimpleDB 服务中,并且可以将其取回。我只是不知道如何处理返回的SimpleXML对象。
Tarzan AWS 文档是这样说的:
查看响应以浏览响应的标头和正文。请注意,这是一个对象,而不是一个数组,并且主体是一个 SimpleXML 对象。
下面是返回的 SimpleXML 对象的示例:
[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 ) ) ) )
那么我需要什么代码来遍历每个对象项呢?我想遍历它们中的每一个,并像返回的mySQL查询一样处理它。例如,我可以查询 SimpleDB,然后循环通过 SimpleXML,以便在页面上显示结果。
或者,如何将整个shebang变成一个数组?
我是SimpleXML的新手,所以如果我的问题不够具体,我很抱歉。