adrianTNT Posted October 18, 2006 Share Posted October 18, 2006 Hello.I have this php script that displays the contents of an xml file like this:[code]<?xml version="1.0" encoding="iso-8859-1"?><icons><ico title="Lino Adium Dock" img="enhancedlabs/lino_adium_dock.png" url="http://www.enhancedlabs.com"/><ico title="The real Christmas" img="enhancedlabs/2005.png" pc="enhancedlabs/the_ico.zip" url="http://www.enhancedlabs.com"/><ico title="Mac Mini" img="enhancedlabs/mac_mini.gif" pc="enhancedlabs/mac_mini_icons.zip" url="http://www.enhancedlabs.com"/></icons>[/code]The script to print the above xml file is like this:[code]<?phpclass XMLparser{ var $main = array(); var $currentparent = "default"; function XMLparser($inputxmlstring){ $lines = explode("\n",$inputxmlstring); $n = 0; foreach ($lines as $line){ if (trim($line) == "") continue; $line = trim($line); $s_regex = "/<([a-z]+)>/i"; $e_regex = "/<\/([a-z]+)>/i"; $s_matches = null; $e_matches = null; $start = preg_match($s_regex, $line, $s_matches); $end = preg_match($e_regex, $line, $e_matches); if ($s_matches == null && $e_matches == null){ //this is a node $attributes = array(); preg_match_all("/([a-z]+)=\"([^\"]*)\"/i", $line, $attributes); $names = $attributes[1]; $values = $attributes[2]; for ($i = 0; $i < count($names); $i++){ $name = $names[$i]; $value = $values[$i]; $this->main[$this->currentparent][$n][$name] = $value; } $n++; }else if($e_matches == null){ //this is a start $parentname = $s_matches[1]; $this->currentparent = $parentname; $n = 0; } } } }$XML = file_get_contents("icons.xml");$instance = new XMLparser($XML);$parseddata = $instance->main;//get all the nodes under the "icons" parent/* foreach ($parseddata['icons'] as $node){ echo "Title: " . $node['title']; echo " Source: " . $node['img']; echo " Credits: " . $node['url'] . "<br />";}*/?>[/code][color=red][b]Question is: how should the parser look like in order to parse a new XML file formated like this:[/b][/color][code]<?xml version="1.0" encoding="iso-8859-1"?><links> <link> <title>Adrian TNT Flash Design</title> <url>http://www.adriantnt.com</url> <description>Flash menus, buttons and components by Adrian. C.</description> </link> <link> <title>FFILES</title> <url>http://www.ffiles.com</url> <description>Sharing resources and creations.</description> </link></links>[/code]Thank you.- Adrian. Quote Link to comment https://forums.phpfreaks.com/topic/24345-i-need-to-modify-a-script-xml-parser-to-parse-a-new-xml-format/ Share on other sites More sharing options...
HuggieBear Posted October 18, 2006 Share Posted October 18, 2006 Apart from the commented out section at the bottom of the page, I can't see any 'tag-specific' code in there, so I'd have thought that it should just work with the new XML too.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24345-i-need-to-modify-a-script-xml-parser-to-parse-a-new-xml-format/#findComment-110758 Share on other sites More sharing options...
adrianTNT Posted October 18, 2006 Author Share Posted October 18, 2006 The commented area at the end are the ones that display the xml in site but the new xml I want to read has an additional level and I do not know what to modify in order to read that new format.//The php script was made by someone else, I am not good with php. Quote Link to comment https://forums.phpfreaks.com/topic/24345-i-need-to-modify-a-script-xml-parser-to-parse-a-new-xml-format/#findComment-110771 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.