Jump to content

XML Usage

From Wikiversity


Accessing XML data with PHP5: XPath

[edit | edit source]
<?xml version="1.0" encoding="iso-8859-1" ?>
<articles>
<item>
<title>PHP Weekly: Issue # 172</title>
<link>http://www.zend.com/zend/week/week172.php</link>
</item>
<item>
<title>Tutorial: Develop rock-solid code in PHP: Part three</title>
<link>http://www.zend.com/zend/tut/tut-hatwar3.php</link>
</item>
</articles>
<?php
 $dom = new DomDocument();
 $dom->load("articles.xml");
 $xp = new domxpath($dom);
 $titles = $xp->query("/articles/item/title");
 foreach ($titles as $node) {
   print $node->textContent . " ";
 }
 $path = new domxpath($dom);
 $link=$path->query("/articles/item[position() = 2]/link");
 foreach ($link as $node) {
   print $node->textContent . "";
 }
 $path1 = new domxpath($dom);
 $link1=$path1->query("/articles/item/link");
 foreach ($link1 as $node) {
   print $node->textContent . "";
 }
?>