开发者

Need to show child data on parent id

开发者 https://www.devze.com 2023-03-20 23:26 出处:网络
i\'m struggling with Xpath, i have an xml list and i need to get the child data based on the parent id ...

i'm struggling with Xpath, i have an xml list and i need to get the child data based on the parent id ...

My xml file :

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

the code i use to get for example the project 1 data (does not work):

$xmlDoc = new DOMDo开发者_StackOverflow社区cument();
$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;     
}

But this does not show anything, if someone can point me out ... thanks


In addition to the solution already given you can also use:

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;
}

or fetch the DOMText node values directly with 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);
}

or import the node to 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;
}

or even concatenate all the values directly in the 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
    )
);


Accessing the child nodes as you do:

echo $detail->title;

Is not valid, if you use DOM* functions. This would probably work if you were using SimpleXML.

For DOM* try this:

$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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号