views:

88

answers:

2

I am trying to chop XML data into usable strings to reuse them later on in my script.

I am receiving the data via a Curl request and his goes great. now chopping the data kills me..

this a part of the XML I am receiving (the whole data part is about 90 lines)

<professions>
    <skill key="IT Specialist" maxage="40" group="IT" worked="5"/>
    <skill key="Assistant" maxage="35" group="Office" worked="5"/>
</professions>

Now of course I could make a function filtering data etc, but I just feel there is a different way to get the job done. The XML is loaded in a $var.

Any help getting this done better than reading the string etc etc would be helpfull.

+1  A: 

xml_parse_into_struct() is nice.

Havenard
Looking into this right now, will keep you in the loop with the result.
Fons
Getting the Half the data in the array now, great!Values are recorded, index not.Will try to find a way around that. Thanks a lot!RegardsFons
Fons
Works like a charm! very fast as well.Thanks a lotFons
Fons
A: 

Sorry if I don't understand your problem correctly, but what's the problem with simply throwing the XML into SimpleXML which is by far the easiest to use PHP XML API and it also supports XPath for selective data extraction:

$data = simplexml_load_string($var);
echo $data->skill[0]['key']; // will print "IT Specialist"
foreach ($data->skill as $skill) {
    printf('%s in group %s with max. age %d and %d year(s) of experience', 
        $skill['key'], $skill['group'], $skill['maxage'], $skill['worked']) . "\n";
}
/*
 * this will print:
 * IT Specialist in group IT with max. age 40 and 5 year(s) of experience
 * Assistant in group Office with max. age 35 and 5 year(s) of experience
 */
$keys = $data->xpath('/professions/skill/@key');
foreach ($keys as $key) {
    echo $key . "\n";
}
/*
 * this will print:
 * IT Specialist
 * Assistant
 */
// and you can do a lot more...

I cannot think of any reason why one would bother with the weird data structure xml_parse_into_struct() creates (besides when writing some own XML parser-like functionality), when there are tools that handle the given problem much easier. But perhaps I misunderstood your question...

Stefan Gehrig