2011-07-15 69 views
1

我與Xpath的掙扎的孩子數據,我有一個XML列表,我需要得到一個基於父ID子數據...需要顯示在父ID

我的XML文件:

<projecten> 
<project id="1"> 
    <titel>Shop 1</titel> 
    <siteurl>http://test.be</siteurl> 
    <screenshot>test.jpg</screenshot> 
    <omschrijving>comment 1</omschrijving> 
</project> 
<project id="2"> 
    <titel>Shop 2</titel> 
    <siteurl>http://test2.be</siteurl> 
    <screenshot>test2.jpg</screenshot> 
    <omschrijving>comment</omschrijving> 
</project> 
</projecten> 

我用它來獲得例如項目1個數據的代碼(不工作):

$xmlDoc = new DOMDocument(); 
$xmlDoc->load(data.xml); 
$xpath = new DOMXPath($xmlDoc); 

$projectId = '1'; 
$query = '//projecten/project[@id='.$projectId.']'; 

$details = $xpath->query($query); 

foreach($details as $detail) 
{ 
    echo $detail->titel; 
    echo $detail->siteurl; 
    echo $detail->screenshot; 
    echo $detail->omschrijving;  
} 

但這並不顯示任何東西,如果有人能指出我了...感謝

回答

0

訪問的孩子,你做的節點:

echo $detail->title; 

是無效的,如果你使用DOM*功能。如果您使用的是SimpleXML,這可能會起作用。

對於DOM *試試這個:

$dom = new DOMDocument; 
$dom->loadXml('<projecten> 
<project id="1"> 
    <titel>Shop 1</titel> 
    <siteurl>http://test.be</siteurl> 
    <screenshot>test.jpg</screenshot> 
    <omschrijving>comment 1</omschrijving> 
</project> 
<project id="2"> 
    <titel>Shop 2</titel> 
    <siteurl>http://test2.be</siteurl> 
    <screenshot>test2.jpg</screenshot> 
    <omschrijving>comment</omschrijving> 
</project> 
</projecten> 
'); 

$id = 2; 
$xpath = new DOMXPath($dom); 
foreach ($xpath->query(sprintf('/projecten/project[@id="%s"]', $id)) as $projectNode) { 
    // repeat this for every needed node 
    $titleNode = $xpath->query('titel', $projectNode)->item(0); 
    if ($titleNode instanceof DOMElement) { 
     echo $titleNode->nodeValue; 
    } 

    // or us a loop for all child nodes 
    foreach ($projectNode->childNodes as $childNode) { 
     echo $childNode->nodeValue; 
    } 
} 
+0

我明白了......非常感謝你耀西 – David

1

除了已經給了你也可以在解決方案中使用:

foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) { 
    echo 
     $projectNode->getElementsByTagName('titel')->item(0)->nodeValue, 
     $projectNode->getElementsByTagName('siteurl')->item(0)->nodeValue, 
     $projectNode->getElementsByTagName('screenshot')->item(0)->nodeValue, 
     $projectNode->getElementsByTagName('omschrijving')->item(0)->nodeValue; 
} 

,或者直接使用XPath

foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) { 
    echo 
     $xpath->evaluate('string(titel)', $projectNode), 
     $xpath->evaluate('string(siteurl)', $projectNode), 
     $xpath->evaluate('string(screenshot)', $projectNode), 
     $xpath->evaluate('string(omschrijving)', $projectNode); 
} 
取一個DOMText節點值

或將節點導入SimpleXml

foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) { 
    $detail = simplexml_import_dom($projectNode); 
    echo 
     $detail->titel, 
     $detail->siteurl, 
     $detail->screenshot, 
     $detail->omschrijving; 
} 

甚至直接串接所有值中的XPath:

$xpath = new DOMXPath($dom); 
echo $xpath->evaluate(
    sprintf(
     'concat(
      /projecten/project[@id = %1$d]/titel, 
      /projecten/project[@id = %1$d]/siteurl, 
      /projecten/project[@id = %1$d]/screenshot, 
      /projecten/project[@id = %1$d]/omschrijving 
     ', $id 
    ) 
);