views:

19

answers:

1

Trying to make a quick and dirty news system.

Have a basic XML file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
  <article id="1">
    <title>Article title 001</title>
    <short>Short text</short>
 <long>Long text</long>
  </article>
  <article id="2">
    <title>Article title 002</title>
    <short>Short text</short>
 <long>Long text</long>
  </article>
</articles>

I can display all the articles with the following code:

<?php

 $xmldoc = new DOMDocument();
 $xmldoc->load('test.xml');

 $xpathvar = new Domxpath($xmldoc);

 $queryResult = $xpathvar->query('//articles/article'); // works fine grabs all articles
 foreach($queryResult as $result){
   echo $result->textContent;
 }
?>

I just can't work out how to show just one article based on ID.

Any help would be great.

Thanks Stefan

+1  A: 
$id = 1;
$queryResult = $xpathvar->query(sprintf('//articles/article[@id="%s"]', $id));
Phil Brown
Thanks that worked a treat!
StefWill