1
votes

I'm trying to get data from a XML file through xPath.

This is the XML:

<item>
    <title>
        51-årig stukket i maven: Klapper i over for politiet
    </title>
    <link>
        http://newsbreak.dk/51-aarig-ringede-om-hjaelp-efter-knivstik-nu-tier-han/
    </link>
    <pubDate>Tue, 20 Jan 2015 09:19:46 +0000</pubDate>
    <description>
        <![CDATA[
    <img width="600" height="400" src="http://newsbreak.dk/wp-content/uploads/2015/01/politi44.jpg" class="attachment-big" alt="Foto: Colourbox (genrefoto)" /><br />51-årig mand vil ikke tale med politiet, selvom han selv ringede til ordensmagten for at få hjælp til knivstik
    ]]>
    </description>
    <category>
        <![CDATA[ A-historier ]]>
    </category>
    <category>
        <![CDATA[ Krim ]]>
    </category>
    <guid>
        http://newsbreak.dk/51-aarig-ringede-om-hjaelp-efter-knivstik-nu-tier-han/
    </guid>
</item>

This is the PHP:

$titelQuery = $xpath->query("//item/title/text()");
$itemQuery = $xpath->query("//item");
$linkQuery = $xpath->query("//item/guid/text()");
$imageQuery = $xpath->query("//item/description/text()");

I get the title and link correctly , but when im trying to get the "description" part, im only getting the text after the image tag like so:

<br />51-årig mand vil ikke tale med politiet, selvom han selv ringede til ordensmagten for at få hjælp til knivstik
]]>
1

1 Answers

0
votes

Just target that <description> node, then use ->nodeValue:

$imageQuery = $xpath->query("//item/description");
echo $imageQuery->item(0)->nodeValue;

Sample Output

Using ->evaluate() will work as well:

echo $xpath->evaluate('string(//item/description)');