0

迅速で汚いニュースシステムを作ろうとしています。

基本的なXMLファイルを用意します。

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

次のコードですべての記事を表示できます。

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

IDに基づいて1つの記事だけを表示する方法がわかりません。

どんな助けでも素晴らしいでしょう。

ありがとうステファン

4

1 に答える 1

1
$id = 1;
$queryResult = $xpathvar->query(sprintf('//articles/article[@id="%s"]', $id));
于 2010-10-20T03:59:20.350 に答える