如何使用 php 获取 xml 节点的名为 xlink:href 的属性的值 [英] How can I get the value of an attribute called xlink:href of an xml node by using php

查看:22
本文介绍了如何使用 php 获取 xml 节点的名为 xlink:href 的属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就是做不到,不知道乳清.如何使用 php 获取 xml 节点的名为 xlink:href 的属性的值.请有人给我一个轻推.我是 php 新手

i just cant do it, dont kno whey. How can I get the value of an attribute called xlink:href of an xml node by using php. Please please someone just give me a nudge. i am new to php

这是 XML 文档

<?xml version="1.0" encoding="UTF-8"?>
<topicMap id="1HLCM3FXT-28MTV0W-50"
    xmlns="http://www.topicmaps.org/xtm/1.0/" xmlns:xlink="http://www.w3.org/1999/xlink">
    <topic id="1HLCM7CDQ-21WQN9G-66">
        <instanceOf>
            <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#concept"/>
        </instanceOf>
        <baseName>
            <baseNameString><![CDATA[feathers]]></baseNameString>
        </baseName>
        <occurrence>
            <resourceRef xlink:type="simple" xlink:href="file:/./Birds_concept - about birds/feathers.txt"/>
        </occurrence>
    </topic>
</topicMap>

推荐答案

使用 DOM*NS 函数之一,例如 getAttributeNS:

$doc = new DOMDocument();
$doc->loadXML($your_xml_string);
$resource_refs = $doc->getElementsByTagName('resourceRef');
foreach($resource_refs as $rr)
    print_r( $rr->getAttributeNS('http://www.w3.org/1999/xlink', 'href') );

(这是未经测试的代码;print_r 可能无法按预期工作.getAttributeNS 返回一个 节点列表,节点列表中的每一项都会是一个属性.getAttributeNS 页面上的文档还有另一个例子.)

(This is untested code; the print_r might not work as expected. getAttributeNS returns a node list, each item in the node list will be an attribute. The documentation on the getAttributeNS page has another example.)

这篇关于如何使用 php 获取 xml 节点的名为 xlink:href 的属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆