2010-08-05 4 views
0

나는이 같은 더미 XML이 : 내 질문은,SimpleXML을 같은 아이

<SOA> 
<cities> 
    <area>Area1</area> 
    <period>period1</period> 
    <center>center1</center> 
</cities> 
<cities> 
    <area>Area1</area> 
    <period>period1</period> 
    <center>center2</center> 
</cities> 
<cities> 
    <area>Area2</area> 
    <period>period1</period> 
    <center>center3</center> 
</cities> 
</SOA> 

내가 XML을 통해 루프를 원하는 : 어떻게 루프 쓰루 반복 할 수 있습니다 아이? 예 : 영역 이름이 Area1 인 루프 영역? (센터 1 및 센터 2 2 지역 1가,이 같은 쿼리 수 있도록합니다 : 그 지역은 지역 1 모든 센터를 찾을 수 있습니다) 미리 감사

+2

에 대한 읽기 아마 나를 그냥하지만 ​​난 질문을하지 않습니다. 달성하고자하는 것은 무엇입니까? – VolkerK

+0

문제가 표시되지 않습니다. 이것은 유효한 XML입니다. 왜 작동하지 않아야합니까? – Gaim

+0

내 질문을 편집했습니다. 도움을 주셔서 감사합니다 – TheNone

답변

4

< 수정 구슬 > 어쩌면 당신이 가진 모든 cities 요소를 반복 할 a area 텍스트 내용이있는 요소 Area1 </수정 구슬 >
예를 들어 XPath을 사용할 수 있습니다.

<?php 
$soa = getDoc(); 
foreach($soa->xpath('cities[area="Area1"]') as $ci) { 
    foreach($ci->children() as $child) { 
    echo $child->getName(), ': ', (string)$child, "\n"; 
    } 
} 

function getDoc() { 
    return new SimpleXMLElement('<SOA> 
    <cities> 
     <area>Area1</area> 
     <period>period1</period> 
     <center>center1</center> 
    </cities> 
    <cities> 
     <area>Area1</area> 
     <period>period1</period> 
     <center>center2</center> 
    </cities> 
    <cities> 
     <area>Area2</area> 
     <period>period1</period> 
     <center>center3</center> 
     </cities> 
    </SOA>'); 
} 

인쇄 과정의

area: Area1 
period: period1 
center: center1 
area: Area1 
period: period1 
center: center2 
+0

감사합니다! 마지막으로 나는 단편적인 것을 픽업하고 이해할 수있다. :) –

2

예. 예를 들어, PHP는 XPath는 설명서

http://php.net/manual/en/simplexmlelement.xpath.php

$xml = new SimpleXMLElement($yourXML); 

$result = $xml->xpath('/SOA/cities/SOA/cities[area="Area1"]'); 

while(list(, $node) = each($result)) { 
    //$node is a city node with Area1 area 
}