php-simplexml问题

php-simplexml问题,php,simplexml,Php,Simplexml,我有一个xml文件,它在提要中包含多个级别的属性/标记,但是simplexml没有在print\rdump中显示它们 例如: <types tag1="1287368759" tag2="1287368759"> <locations> <segment prefix="http" lastchecked="0">www.google.com</segment> <segment prefix="htt

我有一个xml文件,它在提要中包含多个级别的属性/标记,但是simplexml没有在
print\r
dump中显示它们

例如:

<types tag1="1287368759" tag2="1287368759">
    <locations>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
    </locations>
</types>

www.google.com
www.google.com
www.google.com
www.google.com
www.google.com

问题是
中的标记工作正常,并显示在xml转储中,但是每个段中的标记都不存在。有任何帮助吗?

SimpleXML将不显示属性。如果元素中包含正常数据,则需要使用以下示例:

<?php
$xml = '<types tag1="1287368759" tag2="1287368759">
    <locations>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
    </locations>
</types>';

$xml_data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

print_r($xml_data);

foreach ($xml_data->locations->segment as $segment) {
    print $segment['prefix'] . ' - ' . ((string) $segment) . "\n";
}
位置->段作为$segment){
打印$segment['prefix'].-'((字符串)$segment)。“\n”;
}
我不知道为什么,但我发现这很有效


希望有帮助。

SimpleXML更像一种资源,因此
var\u dump
ing/
print\r
ing将不会产生任何可用的结果

一个简单的
foreach($xml->types->segment->locationas$location)
应该可以循环遍历您的位置,并使用它获取节点的属性


我建议仔细看看手册中的示例和函数(也看看注释),因为在您知道如何使用SimpleXML后,使用SimpleXML可能很简单,您确实需要一些关于如何使用它的背景知识,因为通常的自省是不可能的。

此外,任何人都知道如何从“@attributes”中获取值simplexml中的数组?simplexml让我抓狂,抓狂。。。我真的很讨厌数组和对象的混合。我更喜欢用蟒蛇和漂亮的汤。我不知道像Soup这样的库是否可用于PHP,但simplexml绝不简单。我真的很讨厌它。不要使用print\u r()。说真的,不要@Alex JL:SimpleXML不使用数组或对象,所以是的,难怪你会对它失去理智。SimpleXML支持将节点作为对象属性访问,将属性作为关联数组项访问,将集合(节点列表)作为基于0的数字索引数组访问。所有这些都是神奇的属性,print_r()会告诉你这一点。@Josh Davis那么,你是说“作为对象属性访问的节点”不是对象,“关联数组”和“基于0的数字索引数组”不是数组?SimpleXML是一个扩展,它允许您使用节点的对象表示法和属性的数组表示法访问XML文档。然而,在核心问题上,两者都不是。它们是完全不同的数据结构,用熟悉的语法包装。如果你没有得到,请坚持你所知道的。这是可行的,但问题是我还需要获得前缀/lastchecked标记值,这些值不会显示在打印\r转储文件中。我更新了示例以包含属性的显示,但正如Wrikken在评论中所说,还有其他方法可以做到这一点……关于属性,你也可以在这里查看:非常感谢,奇怪的是,打印没有显示所有的数组级别,但是它们都在那里!还可以使用$location['prefix']获取prefix属性,等等。