Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

52 users online



Simple XML parsing for editing

Simple XML parsing for editing

Currently viewing this thread: 1 (0 members and 1 guests)


gfd

gfd

Status: Offline!

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! Smile

Jeremie

Jeremie

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

gfd

gfd

Status: Offline!

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:

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

Quote:

Warning: Invalid argument supplied for foreach() in ******.php on line 9

Phil

Phil

with Mr. Jones
Status: Offline!

Your filename isnt a password, you dont have to hide it Smile

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:

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

gfd

gfd

Status: Offline!

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 Tongue

gfd

gfd

Status: Offline!

The code you gave me has a parse error. I think this is what you meant:

PHP:

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

Phil

Phil

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

Jeb

Jeb

Status: Offline!

$mvalues probably has no values.

Thus, the loop will never execute.

Change that function to this to cater for arrays with no values:

PHP:

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

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0109 seconds.