2013-05-21 5 views
-2

내가있는 경우 :디스플레이는 PHP의 특정 속성을 가진 XML 노드

<listing> 
<name>Bob</name> 
<age>20</age> 
<hair>red</hair> 
</listing> 

<listing> 
<name>John</name> 
<age>24</age> 
<hair>black</hair> 
</listing> 

내가 만 표시 목록에 내 PHP 페이지를 코딩 어떻게하면 머리 단지에서 끌어 것이라고

그래서 = 블랙

<listing> 
<name>John</name> 
<age>24</age> 
<hair>black</hair> 
</listing> 

감사

+0

현재 XML을 어떻게 파싱하고 있습니까? 이미 가지고있는 것을 보는 것이 도움이됩니다. – Ryan

+0

나는 newb이지만 이것 [link] http://www.w3schools.com/xml/simple.xml과 같은 표준 XML 파일을 사용하고있다. – user2405912

답변

0

사용 XPath.

// First, your XML must be wraped with some root tag. 
$data = <<<XML 
<root> 
    <listing> 
     <name>Bob</name> 
     <age>20</age> 
     <hair>red</hair> 
    </listing> 

    <listing> 
     <name>John</name> 
     <age>24</age> 
     <hair>black</hair> 
    </listing> 
</root> 
XML; 

// Instancing the SimpleXMLElement within the XML(obviously) 
$xml = new SimpleXMLElement($data); 

// XPath 
$xpath = $xml->xpath("listing[contains(hair,'black')]"); 
/** 
* eXplained XPath: 
*  <listing> that 
*    <hair>'s content is equal black 
*/ 

foreach($xpath as $node){ 
    // just for fun echoes the <results> node 
    echo $node->asXml(); 
} 
관련 문제