How can I get the value of an attribute called xlink:href of an xml node by using php

半世苍凉 提交于 2019-12-12 11:50:50

问题


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

This is the XML Document

<?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>

回答1:


Use the DOM and one of the *NS functions, like 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') );

(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.)



来源:https://stackoverflow.com/questions/3542153/how-can-i-get-the-value-of-an-attribute-called-xlinkhref-of-an-xml-node-by-usin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!