2012-09-01 5 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 파일에 내가

답변

1

index="5.0" 당신은 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

아무런 문제가 없다는 것을 알면, 나중에 사용하기 위해 배열을 생성하거나 반향을 생성하지 않는다면, 하나의'record'를 갖는 것이 이전 반복으로 바뀌게 될 것이고, 단지 echo 만 추가하는 것입니다. :) –