
July 28th, 2003
11:38 PM
Simple XML parsing for editing
I have an xml file that is loaded by a Flash animation -- but thats not the point.
I want to load the xml (actually, they are 4 XMLs) display everything in many form fields and when submitting the form updating every xml. Sort of a simple admin backend.
Each xml looks something like this:
[PHP]<?xml version="1.0"?>
<news>
<item title="Title 1" text="Long text 1" url="1.htm" />
<item title="Title 2" text="Long text 2" url="2.htm" />
</news>[/PHP]
Every xml has only two items like that.
I would like to get them in an array like
[b]$xml1[0][title];[/b]
to get "Title 1"
[b]$xml1[1][text];[/b]
to get "Long text 2"
(etc.)
The rest of the system is not a problem, i just need to put all the info inside the xmls in arrays.
Thanks in advance guys! 

July 29th, 2003
12:04 AM
Neversidian
Status: Offline!
check the php functions to parse xml
you have to create your own parser, its very easy
i coded a xml parser for a little project and I did it in less than 1 hour
___________________
Jeremie - Used to be the Director of Community Development

July 29th, 2003
01:15 AM

I've trying to use php's manual for this (http://www.php.net/xml)
However, i've been trying to change an example (no.2):
What i have so far is:
<?php
class AminoAcid {
var $title;
var $text;
var $url;
function AminoAcid ($aa) {
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function readDatabase($filename) {
// read the xml database of aminoacids
$data = implode("",file($filename));
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$data,$values,$tags);
xml_parser_free($parser);
// loop through the structures
foreach ($tags as $key=>$val) {
if ($key == "item") {
$molranges = $val;
// each contiguous pair of array entries are the
// lower and upper range for each molecule definition
for ($i=0; $i < count($molranges); $i+=2) {
$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
continue;
}
}
return $tdb;
}
function parseMol($mvalues) {
for ($i=0; $i < count($mvalues); $i++)
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
return new AminoAcid($mol);
}
$eng1 = readDatabase("../eng/news.xml");
echo "** Database:\n";
print_r($eng1); //this is just to test if the array gets formed ok, then i will remove it
?>
But... php gives me the following error:
Warning: Invalid argument supplied for foreach() in ******.php on line 9

July 29th, 2003
01:28 AM
with Mr. Jones
Status: Offline!
Your filename isnt a password, you dont have to hide it 
Your error is on line 44 and the one above
Immediate reason:
return new AminoAcid($mol);
$mol isnt an array
Likely cause:
You screwed up for loop, no }
Likely solution
<?php
function parseMol($mvalues) {
for ($i=0; $i < count($mvalues); $i++)
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
?>
Since your code was tabbed you should of been able to spot that
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials

July 29th, 2003
01:35 AM
Thanks, i'll try that.
The code was basically copied from php.net, i only changed some names from the xml file for mine, i didn't write the actual code.
And i didn't intend to hide the filename, but the path to it 

July 29th, 2003
01:48 AM
The code you gave me has a parse error. I think this is what you meant:
<?php
function parseMol($mvalues) {
for ($i=0; $i < count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
?>
But it still gives me the "Warning: Invalid argument supplied for foreach() in /www/sanjuanvip.com/htdocs/loscap/data/news.php on line 9"

July 29th, 2003
05:05 PM
with Mr. Jones
Status: Offline!
Its still because in this line return new AminoAcid($mol); $mol isn't a array. Before you return do a print_r($mol)
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials

July 30th, 2003
05:46 AM
$mvalues probably has no values.
Thus, the loop will never execute.
Change that function to this to cater for arrays with no values:
<?php
function parseMol($mvalues) {
$mol = array();
for ($i=0; $i < count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
?>
Should clear up the foreach() problem, don't know about any others.
___________________
Adam Goossens -- PHP is my mother tounge.
Linux: ( kernel.org | winehq ) -- f33l the p0w3r.
Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.