[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

simple xml to array

Posted on 2012-08-31
11
Medium Priority
?
41 Views
Last Modified: 2016-06-26
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>
0
Comment
Question by:Herci
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38354320
Try the attached class.
xmlthing.class.php
1
 

Author Comment

by:Herci
ID: 38354835
Thanks a lot for that. I will try this and get back to you.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38356485
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Herci
ID: 38358796
@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
 

Author Comment

by:Herci
ID: 38358807
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38358841
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
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38359325
It would also be helpful to see the full code, instead of just the single function. Context is important.
0
 

Author Comment

by:Herci
ID: 38610836
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41675177
To quote the author:
gr8gonzo's class file works fine
Here is the link:
https:#a38354320
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 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