simple xml to array

Hi,
I have couple of XML files with different structures. I am looking for a way to insert each xml XML data in to an array.

I currently have this function that loads them in to an array, but the problem is when there are multiple nodes with the same name, only the last one gets inserted because it overrides the previous ones. If you look at the XML below, it contains the <Track> and the <TrackArtist> nodes multiple time but only the last block gets inserted. I need to be able to store all the records in an array.

public function xml2array($xml,$views='')
      {
            $arr = array();
            foreach ($xml as $element)
            {
                  $node = $element->getName();
                  $value = get_object_vars($element);
                  
                        if (!empty($value))
                        {
                              $arr[$node] = $element instanceof SimpleXMLElement ? $this->xml2array($element) : $value;
                        }
                        else
                        {
                              $arr[$node] = trim($element);
                        }
                  
            }
            return $arr;
      }

my XML file looks like this.

<tracks>
    <Track>
        <ids>
            <compositionId>4345<compositionId/>
            <performanceId>154623<performanceId/>
            <trackId>165423</trackId>
        </ids>
        <title>What About Now</title>
        <performers>
            <TrackArtist>
                <id>2001003</id>
                <name>joe blog</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>200100311</id>
                <name>David</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003234</id>
                <name>Joshua</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003221</id>
                <name>Ben</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
    <Track>
        <ids>
            <compositionId>2334<compositionId/>
            <performanceId>4234<performanceId/>
            <trackId>546464</trackId>
        </ids>
        <title>Now or never</title>
        <performers>
            <TrackArtist>
                <id>2323</id>
                <name>Billy joe</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>4564645</id>
                <name>Jim</name>
            </TrackArtist>
            <TrackArtist>
                <id>2342</id>
                <name>Cindy</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003221</id>
                <name>John</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <isPick>false</isPick>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
      <Track>
        <ids>
            <compositionId>3242<compositionId/>
            <performanceId>8423476<performanceId/>
            <trackId>423423</trackId>
        </ids>
        <title>In the Air</title>
        <performers>
            <TrackArtist>
                <id>2323</id>
                <name>jack</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>324</id>
                <name>Jill</name>
            </TrackArtist>
            <TrackArtist>
                <id>34232</id>
                <name>Tom</name>
            </TrackArtist>
            <TrackArtist>
                <id>867868</id>
                <name>Peter</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <isPick>false</isPick>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
</tracks>
HerciAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
Try the attached class.
xmlthing.class.php
1
 
HerciAuthor Commented:
Thanks a lot for that. I will try this and get back to you.
0
 
Ray PaseurCommented:
This does not appear to be valid XML..

Line 10:
<compositionId>4345<compositionId/>

Should it be:
<compositionId>4345</compositionId>

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

// XML FROM THE POST AT EE
$xml = <<<EOXML
<tracks>
    <Track>
        <ids>
            <compositionId>4345<compositionId/>
            <performanceId>154623<performanceId/>
            <trackId>165423</trackId>
        </ids>
        <title>What About Now</title>
        <performers>
            <TrackArtist>
                <id>2001003</id>
                <name>joe blog</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>200100311</id>
                <name>David</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003234</id>
                <name>Joshua</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003221</id>
                <name>Ben</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
    <Track>
        <ids>
            <compositionId>2334<compositionId/>
            <performanceId>4234<performanceId/>
            <trackId>546464</trackId>
        </ids>
        <title>Now or never</title>
        <performers>
            <TrackArtist>
                <id>2323</id>
                <name>Billy joe</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>4564645</id>
                <name>Jim</name>
            </TrackArtist>
            <TrackArtist>
                <id>2342</id>
                <name>Cindy</name>
            </TrackArtist>
            <TrackArtist>
                <id>2001003221</id>
                <name>John</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <isPick>false</isPick>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
      <Track>
        <ids>
            <compositionId>3242<compositionId/>
            <performanceId>8423476<performanceId/>
            <trackId>423423</trackId>
        </ids>
        <title>In the Air</title>
        <performers>
            <TrackArtist>
                <id>2323</id>
                <name>jack</name>
            </TrackArtist>
        </performers>
        <composers>
            <TrackArtist>
                <id>324</id>
                <name>Jill</name>
            </TrackArtist>
            <TrackArtist>
                <id>34232</id>
                <name>Tom</name>
            </TrackArtist>
            <TrackArtist>
                <id>867868</id>
                <name>Peter</name>
            </TrackArtist>
        </composers>
        <duration>251</duration>
        <disc>1</disc>
        <isPick>false</isPick>
        <hasReview>false</hasReview>
        <flags></flags>
    </Track>
</tracks>
EOXML;

$obj = simpleXML_Load_String($xml);
var_dump($obj);

Open in new window

Your thoughts? ~Ray
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
HerciAuthor Commented:
@Ray-Yes you are correct. I think I must have made that syntax mistake when editing the xml data. The actual xml file contains the correct syntax.
0
 
HerciAuthor Commented:
gr8gonzo's class file works fine but I think there are way so many lines of code in there. Can you suggest what I can change/add to my function to get it working? I think what all it needs is to check if the tag is repeated and if it has, then add it to an array. Do I need to keep a reference of the previous tags to check if is repeating again?
0
 
Ray PaseurCommented:
The actual xml file contains the correct syntax.
Then please post the actual XML file, and tell us what tags are important to your work.  I am sure there is a solution, but without useful test data, there is no point in trying to write the code.

The problem with trying to use XML documents as if they were arrays is that they do not readily translate into arrays -- they translate into objects, via the SimpleXML class.  Objects are more complex and dimensional than arrays, and while some limited XML structures may be readily made into arrays, it will almost always be easier to use the objects, rather than adding an additional step of deconstructing the objects back into arrays.  You have already discovered that identical array keys cause the array values to be overwritten because you can have only unique keys.  Perhaps a solution involving an array of sub-arrays or an array of objects will be best.

In any case, I'll wait for you to post the test data or a link to the test data file, then I'll try to show you some ways of extracting the data from the XML string.
0
 
gr8gonzoConsultantCommented:
It would also be helpful to see the full code, instead of just the single function. Context is important.
0
 
HerciAuthor Commented:
Hi Guys, My client has decided to put the project on hold and therefore I won't be able to do any further development on this for the moment. I am looking to start working on it as soon as I get the go ahead from my client, and therefore I would like to keep my question open.
Thanks.
0
 
Ray PaseurCommented:
To quote the author:
gr8gonzo's class file works fine
Here is the link:
https:#a38354320
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.