Link to home
Start Free TrialLog in
Avatar of Neil Thompson
Neil ThompsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Parse XML using PHP printing both the values of attributes of each node & path info

Hi

I've been asked to Parse XML using PHP printing both the values of attributes of each node & path info so wondered if any of you gurus could assist.

My colleague doesnt know what fields he finally wants mapped until he sees all the results so I thought it would be a good idea to pass the XML file and then he could pick and choose.

To then aid me in providing what he wants it would be good to get the path beside each output, so I'm looking for something like this: NOTE: this is not the XML I'll be using, he doesnt have that yet so the solution needs to be very generic and look after whatever is chucked at it.


The XML can and will go to many child levels, values and atributes etc.


<songs>
    <song dateplayed="2011-07-24 19:40:26">
        <title>I left my heart on Europa</title>
        <artist>Ship of Nomads</artist>
    </song>
    <song dateplayed="2011-07-24 19:27:42">
        <title>Oh Ganymede</title>
        <artist>Beefachanga</artist>
    </song>
    <song dateplayed="2011-07-24 19:23:50">
        <title>Kallichore</title>
        <artist>Jewitt K. Sheppard</artist>
    </song>
</songs>

Open in new window


So with this basically iterate the whole thing and produce something like:

2011-07-24 19:40:26 | song[0]['dateplayed']
I left my heart on Europa | song[0]->title
Ship of Nomads | song[0]->artist
2011-07-24 19:27:42 | song[1]['dateplayed']
Oh Ganymede | song[1]->title
Beefachanga | song[1]->artist
2011-07-24 19:23:50 | song[2]['dateplayed']
Kallichore | song[2]->title
Jewitt K. Sheppard | song[2]->artist

Regards
Neil
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Sounds pretty theoretical, but the basic concept is "use the SimpleXML class."
See if this makes sense.   I don't readily know how to determine the array key number, and I've never written any code that had a dependency on that.  But this shows how to get the attributes and data elements from the objects.
http://www.laprbass.com/RAY_temp_neilt.php

<?php // RAY_temp_neilt.php
error_reporting(E_ALL);
echo '<pre>';


// DEMONSTRATE HOW TO PARSE XML
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28316178.html


// TEST DATA FROM THE POST AT EE
$xml = <<<EOD
<songs>
    <song dateplayed="2011-07-24 19:40:26">
        <title>I left my heart on Europa</title>
        <artist>Ship of Nomads</artist>
    </song>
    <song dateplayed="2011-07-24 19:27:42">
        <title>Oh Ganymede</title>
        <artist>Beefachanga</artist>
    </song>
    <song dateplayed="2011-07-24 19:23:50">
        <title>Kallichore</title>
        <artist>Jewitt K. Sheppard</artist>
    </song>
</songs>
EOD;


// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS THE INFORMATION
foreach ($obj as $song)
{
    $d = $song['dateplayed'];
    $t = $song->title;
    $a = $song->artist;
    echo PHP_EOL . "ON $d WE PLAYED SONG $t BY ARTIST $a";
}

Open in new window

HTH, ~Ray
Avatar of Neil Thompson

ASKER

Thanks Ray,

The unfortunate thing is in the real life XML we do not yet know the node names or attributes so I'm looking for something that can scan the entire tree, giving back not only the values but also its own array path to that attribute or node value.

does that make sense?

Neil
No, it sounds so theoretical as to be an unusable concept.  What would the data source be that had unknowable node names?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Ray ill take a look. Sorry to be so vague on this it's just we don't have the XML files as yet as they are being sorted by a third party. When we get them I need to have them all on a website in a day but only the bits someone here wants, not it all

Aaagh!
need to have them all on a website in a day
And I want to be able to run a marathon in under two hours!  Some goals are unreasonable, and I think good judgement about realistic timelines is one of the important parts of business management.  It is unreasonable to say, in effect, "You cannot have any test data and you must use whatever we give you to achieve a result in a day."  No amount of preparation will predictably lead to success.  What if the XML document is malformed?  These are the things you need to find out beforehand, so you can mock up the XML and write the programming.

Best of luck with it, and I hope you're getting paid a lot of money!! ~Ray
Thanks Ray, I'll take a look what I can do with SimpleXML and some sort of iteration.

perils of UK local authorities unfortunately, everything is always last minute, zero budget required yesterday and your fault when it's not done.

One of the reasons I have no hair :)
Superb thank you, neil
... the reasons I have no hair :)
You must be going to my barber!  Thanks for the points and thanks for using EE, ~Ray