XPath in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joran420
Forum Newbie
Posts: 8
Joined: Wed Apr 04, 2007 8:06 pm

XPath in PHP

Post by joran420 »

I got the XPath.class.php class file and am using it and i can select single data easy but how can I select an array of matches?

Xpath->getData("/xml/*/headline")
gives me an error that there is more than one match(which there is ...but how can i make it return an array of nodes that match or something simillar?)

not sure how clear that was...its for a CMS news System
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

Where did you ge the XPath.class.php? What version of PHP are you using? If you're using PHP5 or greater you can use SimpleXML which has pretty good support for XPath and can certainly return multiple elements from a query.
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

Joran has the right idea, it's probably worth writing it yourself in SimpleXML (it's not hard, trust me) and you'll need to use a function called childNodes, which returns an array.

http://devzone.zend.com/node/view/id/688
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

If you have PHP5, use SimpleXML where possible, or the libxml2 extension.

You're looking for "//nodename" to select all nodes of that name from the current node.
joran420
Forum Newbie
Posts: 8
Joined: Wed Apr 04, 2007 8:06 pm

Post by joran420 »

thanks for the heads up guys :) I got it working but then decided to scrap XML for mySQL ....hehe :P
Post Reply