Stal Posted February 13, 2007 Share Posted February 13, 2007 Hi, I am looking for a solution to this problem I am faced with. I have a website which i can query and it replies with a small amount of xml data with regards to a particular product, eg spec, weight, stock etc. I am trying to create a small script that will query that page and extract the values of the xml response, eg the xml data may look like this: <?xml version="1.0" ?> - <product> <result>OK</result> <description>SOME PRODUCT</description> <extendeddescription>TECHNICAL SPEC</extendeddescription> <weight>9.35</weight> <stock>434</stock> </product> I need to capture this response and extract the values of description, stock, etc as variables i can use in PHP. Then i can call this page for each product code required, eg www.mydomain.com/script.php?product_code=12345 Can anyone point me in the right direction as i am struggling to find any info as to how to extract the values i need. ??? ??? ??? Thanks Quote Link to comment Share on other sites More sharing options...
Stal Posted February 21, 2007 Author Share Posted February 21, 2007 ok, how about another possibility. Is there a PHP function i can use to extract everything between 2 values, eg: $description = strip everything bewteen "<description>" and "</description>" from $xml_result; $weight = strip everything bewteen "<weight>" and "</weight>" from $xml_result; etc.... ??? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 Use domxml and xpath_eval. Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted February 21, 2007 Share Posted February 21, 2007 I would use the SimpleXML object. Its dead simple to use like this: Check out the php manual for detailed info. DomXML is soooooooooo php4 <? $xml = <<<XML <?xml version="1.0" ?> <products> <product> <result>OK</result> <description>Product 1</description> <extendeddescription>TECHNICAL SPEC</extendeddescription> <weight>9.35</weight> <stock>434</stock> </product> <product> <result>OK</result> <description>Product 2</description> <extendeddescription>TECHNICAL SPEC</extendeddescription> <weight>5.35</weight> <stock>132</stock> </product> </products> XML; $xmlobj = simplexml_load_string($xml); foreach ($xmlobj as $product) { print ( $product -> result."<br />". $product -> description."<br />". $product -> extendeddescription."<br />". $product -> weight."<br />". $product -> stock."<br /><br />" ); } ?> Quote Link to comment Share on other sites More sharing options...
Stal Posted February 21, 2007 Author Share Posted February 21, 2007 thanks for your help. Unfortunately it looks like i will be unable to use that function as i am using PHP version 4.4.4 which I guess is why im getting: Fatal error: Call to undefined function: simplexml_load_string() Quote Link to comment Share on other sites More sharing options...
Barand Posted February 21, 2007 Share Posted February 21, 2007 Before php5 came along I used a XMLParser class (by Marcus Pont) that I found on PHPClasses site. The data array produced by the class was remarkably similar to the ones produced by php5 function. Im attaching the code if you're interested. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Stal Posted February 21, 2007 Author Share Posted February 21, 2007 yes, I would be interested in taking a look at that, many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.