2012-09-01 55 views
0

我有一个简单的xml文件,其中包含节点和一些其他信息。我想从系列节点中提取索引。xml节点和节点内的其他信息

XML:

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

$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"

回答

1

您使用getAttribute()方法。

$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 
+0

谢谢!忘了那个。 – rackemup420

+0

没有probs,我看到的一件事是,如果你没有回应或创建一个数组供以后使用,有更多的一个'记录'只会被前面的迭代替换,我只加回声作为一个例子。 :) –

相关问题