首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xml节点和节点内部的其他信息

xml节点和节点内部的其他信息
EN

Stack Overflow用户
提问于 2012-09-02 00:45:46
回答 1查看 56关注 0票数 0

我有一个包含节点和其他信息的简单xml文件。我想从序列节点中取出索引。

XML:

代码语言:javascript
复制
  <record>
    <id>5055</id>
    <uuid>83885ffc-93d8-41ba-aee2-e5c0ae48fc68</uuid>
    <publisher>Now Comics</publisher>
    <size>5803436</size>
    <title sort="Terminator - The Burning Earth 5, The">The Terminator - The Burning Earth 5</title>
    <authors sort="Unknown">
      <author>Unknown</author>
    </authors>
    <timestamp>2012-05-13T19:38:03-07:00</timestamp>
    <pubdate>2012-05-13T19:38:03-07:00</pubdate>
    <series index="5.0">The Terminator: The Burning Earth</series>
    <cover>M:/Comics/Unknown/The Terminator - The Burning Earth 5 (5055)/cover.jpg</cover>
    <formats>
      <format>M:/Comics/Unknown/The Terminator - The Burning Earth 5 (5055)/The Terminator - The Burning Earth 5 - Unknown.cbr</format>
    </formats>
  </record>

PHP:

代码语言:javascript
复制
$dom = new DOMDocument();  
$dom->load($loc);  
foreach ($dom->getElementsByTagName('record') as $e) {

$publisher = $e->getElementsByTagName('publisher')->item(0)->textContent; 
$arc = $e->getElementsByTagName('series')->item(0)->textContent;    
$uuid = $e->getElementsByTagName('uuid')->item(0)->textContent;

}

现在,在<series index="5.0">The Terminator: The Burning Earth</series>的xml文件中,我想取出该index="5.0"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-02 01:01:01

您可以使用getAttribute()方法。

代码语言:javascript
复制
$dom = new DOMDocument();
$dom->load($loc);
foreach ($dom->getElementsByTagName('record') as $e) {

    $publisher = $e->getElementsByTagName('publisher')->item(0)->textContent;
    $uuid = $e->getElementsByTagName('uuid')->item(0)->textContent;

    $series = $e->getElementsByTagName('series')->item(0);
    $series_index = $series->getAttribute('index');
    $arc = $series->textContent;
}

echo 'Publisher: '.$publisher.'<br />', //Now Comics
     'UUID: '.$uuid.'<br />', //UUID: 83885ffc-93d8-41ba-aee2-e5c0ae48fc68
     'Index: '.$series_index.'<br />', //Index: 5.0
     'Title: '.$arc.'<hr />'; //Title: The Terminator: The Burning Earth
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12229579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档