我有一个要使用 PHP 导入的 SimpleXMLElement 对象。但它只循环通过 XML 文件中的第一行。我还想用 $item->columm1 显示特定的列,而不是用它的所有值显示整行。
$url = 'file.xml';
$sxml = simplexml_load_file($url);
foreach($sxml->Departure->attributes() as $item)
{
echo $item;
}
编辑:这是输出,请注意我已经用 text1、text2 编辑了原始值。
SimpleXMLElement Object (
[Departure] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => text1 [type] => text2 [stop] => text3 [time] => text4 [date] => text4 [direction] => text5 )
EDIT2:输出->
object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(6) { ["name"]=> string(6) "text1" ["type"]=> string(3) "text2" ["stop"]=> string(12) "text3" ["time"]=> string(5) "text4" ["date"]=> string(8) "text5" ["direction"]=> string(10) "text6" } ["JourneyDetailRef"]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["ref"]=> string(125) "text7" } } }
EDIT3:输出->
object(SimpleXMLElement)#7 (1) { [0]=> string(6) "text1" } object(SimpleXMLElement)#8 (1) { [0]=> string(3) "text2" } object(SimpleXMLElement)#7 (1) { [0]=> string(12) "text3" } object(SimpleXMLElement)#8 (1) { [0]=> string(5) "text4" } object(SimpleXMLElement)#7 (1) { [0]=> string(8) "text5" } object(SimpleXMLElement)#8 (1) { [0]=> string(10) "text6" }