labiere33 Posted May 23, 2013 Share Posted May 23, 2013 I've been trying to figure out how to parse an xml file using PHP and to output some HTML. I followed an example from joemarini.com Display a List of Categorized Links with PHP, XML, and SAX The example xml being parsed is only showing tags with attributes, but not with text values within the tag pairs. So I see how to apply the php example to xml that looks like this: <tagname> <tag1 attr="foo"> <tag2 attr1="one" attr2="two"></tag2> </tag1> </tagname> but not to xml that looks like this: <tagname> <tag1 attr="foo"> <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2> </tag1> </tagname> Basically I want to modify the php example so that it allows me to not only store and do something with the attribute values as it parses, but also the text values between the tag pairs. (Is this what is being called the CDATA everywhere I look?) I have found examples that parse the values between tags and they look very different from the code example I have here that parses attribute values. Any help is appreciated. links.php links.xml Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/ Share on other sites More sharing options...
Barand Posted May 23, 2013 Share Posted May 23, 2013 What have you tried? Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431736 Share on other sites More sharing options...
Eiseth Posted May 23, 2013 Share Posted May 23, 2013 (edited) Are you trying to output the whole xml? try DOMDocument() // if you're getting the xml from outside source use file_get_contents('http://www.example.com'); $xml = <<<EOT <?xml version="1.0" ?> <tagname> <tag1 attr="foo"> <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2> </tag1> </tagname> EOT; $dom = new DOMDocument(); $dom->loadXML($xml); header('Content-type: text/xml'); echo $dom->saveXML(); Are you trying to parse specific node? Try simplexml_load_file $xml = <<<EOT <?xml version="1.0" ?> <tagname> <tag1 attr="foo"> <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2> </tag1> </tagname> EOT; $sx = simplexml_load_string($xml); echo $sx->tag1->tag2; // output SOME TEXT VALUE ADDED HERE If this isn't what you want, then try playing with DOMDocument, DOMXPath, DOMElement, SimpleXML instead of parsing it using the tutorial you posted Edited May 23, 2013 by Eiseth Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431746 Share on other sites More sharing options...
labiere33 Posted May 23, 2013 Author Share Posted May 23, 2013 I have a web page calling a PHP script that is trying to read an xml file from an external source and do something with the values, like display them in an HTML table or maybe insert them into a mySQL database. But I want to display both the tag attribute values and values between tags. So something like: <?xml version="1.0" ?> <item status="active"> <desc language="en">WIDGET</desc> </item> could be parsed into an HTML table like this: <table> <tr> <td>active</td> <td>en</td> <td>WIDGET<td> </tr> </table> So yes, I want to parse the entire xml file, creating a new table row in my output table for each "Item" tag I encounter. Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431751 Share on other sites More sharing options...
Solution Eiseth Posted May 23, 2013 Solution Share Posted May 23, 2013 Well you can use simplexml_load_string() or simplexml_load_file() $xml = <<<EOT <?xml version="1.0" ?> <item status="active"> <desc language="en">WIDGET</desc> </item> EOT; // Use file if your xml comes from a file // this converts your xml to object, so $xml == your root element <item> $xml = simplexml_load_string($xml); $item_attributes = $xml->attributes(); echo $item_attributes['status']; // active $desc_attributes = $xml->desc->attributes(); echo $desc_attributes['language']; // en echo $xml->desc; // WIDGET Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431758 Share on other sites More sharing options...
Barand Posted May 23, 2013 Share Posted May 23, 2013 or $xml = simplexml_load_file('my.xml'); $data = $xml->xpath('//item'); foreach ($data as $item) { echo $item['status'] .' | '. $item->desc['language'] .' | '. $item->desc .'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431788 Share on other sites More sharing options...
labiere33 Posted May 24, 2013 Author Share Posted May 24, 2013 (edited) Sorry I only seem to be able to mark one of those last two replies as "best". Just to help anyone else out looking for similar information I thought I would add, The example I quoted above seems to be a little dated. I actually ran across it from Lynda.com in their "XML Basics" video tutorial. Understandably, no one really explained how to modify that example. SimpleXML that became available in PHP 5 seems to be an easier way to achieve the goal. I have latched on to the Basic Tutorial at php net and I should be able to help myself further along now. Thanks. Edited May 24, 2013 by labiere33 Quote Link to comment https://forums.phpfreaks.com/topic/278303-difficulty-pasing-xml-with-php/#findComment-1431976 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.