RabPHP Posted November 7, 2006 Share Posted November 7, 2006 Greetings,I need to know if it is possible to query an XML file for something specific.For example...I have an XML data that looks like...<news><story><headline>Story1</headline><description>description of Story1 </description></story><story><headline>Story2</headline><description>Description of Story2</description></story></news>What I would like to do is essentially "SELECT description from (file) where headline='story2';Is there anyway to do something like that with PHP and XML?Rab Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/ Share on other sites More sharing options...
trq Posted November 7, 2006 Share Posted November 7, 2006 Take a look at the [url=http://php.net/manual/en/ref.simplexml.php]simpleXML[/url] extension. Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121178 Share on other sites More sharing options...
LazyJones Posted November 7, 2006 Share Posted November 7, 2006 You can parse XML data with PHP:1. If you are using PHP5, I recommend [url=http://www.php.net/simplexml]http://www.php.net/simplexml[/url]2. If older, check [url=http://www.php.net/xml]http://www.php.net/xml[/url] functions. It's a bit trickier but works. Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121180 Share on other sites More sharing options...
RabPHP Posted November 7, 2006 Author Share Posted November 7, 2006 I've been looking over the SimpleXML function, however I can't seem to find any examples of what I am looking for.I have found out how to pull out everything in a specific node, but not how to pull out information in a node where another node on the same level equals something. Anyone out there have an example?RabRab Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121188 Share on other sites More sharing options...
trq Posted November 7, 2006 Share Posted November 7, 2006 Ive never used it myself but maybe something like....[code]<?php $xmlstring = file_get_contents("yourfile.xml"); $xml = new SimpleXMLElement($xmlstring); foreach ($xml->story as $story) { if (strtolower($story->headline) == "story2") { echo $story->description; } }?> [/code] Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121195 Share on other sites More sharing options...
RabPHP Posted November 7, 2006 Author Share Posted November 7, 2006 That worked! I've been trying to do this myself for almost a week.Only 1 small problem. The XML file has a New Line after the entries in the description. For example...<description>description of Story1This is a cool movieThis gets 5 stars</description>With the code below it's displaying...description of Story1 This is a cool movie This gets 5 starsHow can I get it to take the New Line markers and display it on a new line?Rab Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121208 Share on other sites More sharing options...
trq Posted November 7, 2006 Share Posted November 7, 2006 Assuming your trying to display it in html, try running it through [url=http://php.net/nl2br[/url].[code=php:0]echo nl2br($story->description);[/code] Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121257 Share on other sites More sharing options...
RabPHP Posted November 7, 2006 Author Share Posted November 7, 2006 Thorpe, your a genius. Thanks for the help, I'll study these two new functions, I've never seen nl2br before.Rab Link to comment https://forums.phpfreaks.com/topic/26493-query-xml-for-specific-data/#findComment-121266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.