【问题标题】:How can I get the value of an attribute called xlink:href of an xml node by using php如何使用 php 获取 xml 节点的名为 xlink:href 的属性的值
【发布时间】:2010-08-22 15:58:14
【问题描述】:

我就是做不到,不知道乳清。如何使用 php.ini 获取 xml 节点的名为 xlink:href 的属性的值。请有人轻推我一下。我是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>

【问题讨论】:

    标签: php xml xlink


    【解决方案1】:

    使用the DOM 和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') );
    

    (这是未经测试的代码;print_r 可能无法按预期工作。getAttributeNS 返回一个node list,节点列表中的每个项目将是an attribute。getAttributeNS 页面上的文档有另一个示例。)

    【讨论】:

    • 感谢您的快速回复,我会尽快测试并报告。我也尝试过 xpath DOM,现在我正在使用 simplexml。我要试试这段代码。再次非常感谢
    • 您好,我收到此错误,我不知道为什么警告:DOMDocument::loadXML() [domdocument.loadxml]:需要开始标签,在实体中找不到'loadXML('Birds.xml');我很感激帮助。欢呼
    • 如果我将 xml 文件加载到 $doc->loadXML('Birds.xml');它给出了错误。但如果我将 xml 文件的全部内容复制到变量 $stuff='all the xml'; $doc->loadXML($stuff);它有效并给了我结果.....似乎自从我一直在工作以来我就遇到了这个问题
    • 嗨,谢谢它的工作我正在加载一个文件,所以我从文档中发现我必须使用 $doc->load('birds.xml');而不是 $doc->loadXML('Birds.xml');非常感谢
    猜你喜欢
    • 2023-03-15
    • 2012-08-05
    • 1970-01-01
    • 2013-05-14
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多