简体   繁体   English

在php中获取xml名称空间属性

[英]getting a xml namespace attribute in php

is it possible to get the attribute: name="Total" and the value ? 是否可以获取attribute: name="Total"value I know it's possible in an array, but the row is not always the same. 我知道有可能在数组中,但是行并不总是相同的。

This is the XML: 这是XML:

<XML>
<resultaten>
<resultaat>
<id>3123709</id>
<antwoord name="Total">9,0</antwoord>
<antwoord name="Communicatie">9,0</antwoord>
<antwoord name="Advies">9,0</antwoord>
<antwoord name="Deskundigheid">9,0</antwoord>
<antwoord name="Betrokkenheid">9,0</antwoord>
<antwoord name="Gemiddelde">9,0</antwoord>
</resultaat>
</resultaten>
</XML>

And i tried this: 我尝试了这个:

$xml = simplexml_load_file($url)

$date = $xml->resultaten->resultaat->attributes();
echo $date;

I do not use simplexml so cannot advise on it's methods but using the standard DOMDocument and it's associated methods it is fairly trivial to get the name attribute values from the XML you show in the question. 我不使用simplexml因此无法就其方法提供建议,但是使用标准DOMDocument及其相关方法,从问题中显示的XML中获取name属性值相当简单。

$xml='<XML>
    <resultaten>
        <resultaat>
            <id>3123709</id>
            <antwoord name="Total">9,0</antwoord>
            <antwoord name="Communicatie">9,0</antwoord>
            <antwoord name="Advies">9,0</antwoord>
            <antwoord name="Deskundigheid">9,0</antwoord>
            <antwoord name="Betrokkenheid">9,0</antwoord>
            <antwoord name="Gemiddelde">9,0</antwoord>
        </resultaat>
    </resultaten>
</XML>';


$dom=new DOMDocument;
$dom->loadXML( $xml );
$col=$dom->getElementsByTagName('antwoord');
foreach( $col as $node )echo $node->getAttribute('name') . '<br />';

outputs: 输出:

Total
Communicatie
Advies
Deskundigheid
Betrokkenheid
Gemiddelde

update 更新

To use a url as opposed to a string simply use the load method as below. 要使用url而不是字符串,只需使用如下的load方法。

    $url='https://klantenvertellen.nl/xml/van_essen_advocaten';
    $dom=new DOMDocument;
    $dom->load( $url );
    $col=$dom->getElementsByTagName('antwoord');
    foreach( $col as $node )echo $node->getAttribute('name') . '<br />';

outputs: 输出:

datum
referentie
Uw voornaam/bedrijf:
Aanbeveling:
Geholpen door:
Ervaring:
Totaal oordeel
Communicatie
Advies
Deskun....etc

To use SimpleXML as you originally wanted is well - simple. 像您最初想要的那样使用SimpleXML很简单。 If you just want the Total value, then you can use XPath to say get the antwoord element with the name attribute which has a value of Total . 如果只需要Total值,则可以使用XPath来说说获得带有name属性的antwoord元素,该属性的值为Total The xpath() call returns a list of matching elements, so use [0] to get the first one. xpath()调用返回匹配元素的列表,因此请使用[0]获取第一个。 Also this returns a SimpleXMLElement, so to ensure the value is a string - cast it to (string) before using the value... 同样这会返回一个SimpleXMLElement,因此要确保该值是一个字符串-在使用该值之前将其强制转换为(string) ...

$xml = simplexml_load_file($url);

$total = $xml->xpath("//antwoord[@name='Total']")[0];
echo (string)$total;

gives... 给...

9,0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM