我正在尝试找出如何迭代返回的 SimpleXML 对象。
我正在使用一个名为 Tarzan AWS 的工具包,它连接到 Amazon Web Services(SimpleDB、S3、EC2 等)。我专门使用 SimpleDB。
我可以将数据放入 Amazon SimpleDB 服务中,也可以将其取回。我只是不知道如何处理返回的 SimpleXML 对象。
Tarzan AWS 文档是这样说的:
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 对象的示例:
[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 新手,因此如果我的问题不够具体,我深表歉意。
最佳答案
您可以在 foreach
循环中使用 SimpleXML
对象(或其属性)。如果您想循环遍历所有“记录”,可以使用类似这样的方法来访问和显示数据:
//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;
}
}
请注意,$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;
}
}
//Now you have an array that looks like:
$rows['message12413344443260']['active'] = 1;
$rows['message12413344443260']['user'] = 'john';
//etc.
关于php - 循环访问 SimpleXML 对象,或将整个对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/871422/