Jump to content


Photo

I need to modify a script (xml parser) to parse a new XML format.


  • Please log in to reply
2 replies to this topic

#1 adrianTNT

adrianTNT
  • Members
  • PipPipPip
  • Advanced Member
  • 146 posts

Posted 18 October 2006 - 03:05 PM

Hello.

I have this php script that displays the contents of an xml file like this:

<?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>

The script to print the above xml file is like this:

<?php

class 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 />";
}
*/
?>

Question is: how should the parser look like in order to parse a new XML file formated like this:

<?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>

Thank you.
- Adrian.

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 03:59 PM

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.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 adrianTNT

adrianTNT
  • Members
  • PipPipPip
  • Advanced Member
  • 146 posts

Posted 18 October 2006 - 04:17 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users