Solved

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

Posted on 2013-12-11
10
481 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:Neil Thompson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39713906
Sounds pretty theoretical, but the basic concept is "use the SimpleXML class."
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39713922
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
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 39713945
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
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39713956
No, it sounds so theoretical as to be an unusable concept.  What would the data source be that had unknowable node names?
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39713979
This is agnostic about node names.  I don't have an example for recursive traversal.   You might want to look into the SPL.  There may be something in the iterators.

<?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 $thing)
{
    foreach ($thing->attributes() as $akey => $attr)
    {
        echo PHP_EOL . "$akey -> $attr";
    }
    foreach ($thing as $key => $data)
    {
        echo PHP_EOL . "$key -> $data";
    }
    echo PHP_EOL;
}

Open in new window

0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 39714008
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!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39714183
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
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 39715143
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 :)
0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 39715159
Superb thank you, neil
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39715494
... the reasons I have no hair :)
You must be going to my barber!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JSON decode 5 45
geting data from the array list 6 37
Joomla Editing 3 21
How to convert PHP array into string ? 16 23
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question