Jump to content

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


Recommended Posts

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]
<?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 />";
}
*/
?>
[/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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.