Solved

php xml code explantion

Posted on 2013-11-14
6
342 Views
Last Modified: 2013-11-16
Please explain why this code works and the object part



 <?php // RAY_temp_rgb192.php error_reporting(E_ALL); echo '<pre>'; // SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28287746.html#a39633174 $url = 'http://www.experts-exchange.com/viewCodeSnippet.jsp?refID=39631349&rtid=20&icsi=3'; // THE XML FILE AT THE GIVEN $url IS UNUSABLE, SO IT'S INSERTED HERE $xml = <<<EOD <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://website.com/</loc> <priority>1.0</priority> </url> <url> <loc>http://www.website.com/portfolio/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/about/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/contact/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/portfolio/branding/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/web-wordpress/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/social-media/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/print/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/branding/cabrillo-youth-summer-institute.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/santacruz-popup-artparty.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/red-house.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/what-to-do-in.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/room-service.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/new-evolution.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/film-production-studio.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/rogueIBO.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/booster-bath.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/pilar-macchione.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/katie-mcmahon.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/genesis.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/tequila-jacks.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/em-integrated.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/em.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/orignauxmoose.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/corey-gegner.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/trey-hock.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/nathan-jones.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/morgan-victoria.php</loc> </url> </urlset> EOD; // MAKE AN OBJECT $obj = simplexml_load_string($xml); // USE AN ITERATOR TO RECOVER THE PARTS OF THE URL ARRAY $urlist = array(); foreach ($obj->url as $u) { $l = (string)$u->loc; $p = (string)$u->priority; $d = "$l,$p"; $d = rtrim($d, ','); $urlist[] = $d; } // SHOSW THE WORK PRODUCT print_r($urlist); 

Open in new window

0
Comment
Question by:rgb192
  • 3
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39647992
The code does not work any more.  It worked when I posted it, but it appears to have been modified.  Do you have a copy of the original, with appropriate line breaks and formatting?  I do not keep copies of "RAY_temp_" scripts.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39649094
As other comment said, this is really hard to read with the formatting you have posted.

But I gather your question is, how does the inline xml work?
This is using something called a heredoc
See
http://www.tuxradar.com/practicalphp/2/6/3

In your example, the code is using "EOD" (this can be anything, but usually people use EOD as short for "end of document")
So the string:
$xml = <<<EOD

is initialized to all the subsequent text until reaching a single line starting with "EOD"

It then attempts to load that xml document (from the string $xml) using this function:

$obj = simplexml_load_string($xml)

which appears to return an object.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39649153
Note on HEREDOC... The end-of-document delimiter must start in column 1.  That is one of the reasons for keeping the original formatting.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rgb192
ID: 39651078
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28287746.html#a39633174

$url = 'http://www.experts-exchange.com/viewCodeSnippet.jsp?refID=39631349&rtid=20&icsi=3';

// THE XML FILE AT THE GIVEN $url IS UNUSABLE, SO IT'S INSERTED HERE
$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://website.com/</loc> <priority>1.0</priority> </url> <url> <loc>http://www.website.com/portfolio/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/about/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/contact/</loc> <priority>0.9</priority> </url> <url> <loc>http://www.website.com/portfolio/branding/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/web-wordpress/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/social-media/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/print/</loc> <priority>0.8</priority> </url> <url> <loc>http://www.website.com/portfolio/branding/cabrillo-youth-summer-institute.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/santacruz-popup-artparty.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/red-house.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/what-to-do-in.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/room-service.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/new-evolution.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/film-production-studio.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/rogueIBO.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/booster-bath.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/pilar-macchione.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/katie-mcmahon.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/genesis.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/tequila-jacks.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/em-integrated.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/em.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/orignauxmoose.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/corey-gegner.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/trey-hock.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/nathan-jones.php</loc> </url> <url> <loc>http://www.website.com/portfolio/branding/morgan-victoria.php</loc> </url> </urlset>
EOD;

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

// USE AN ITERATOR TO RECOVER THE PARTS OF THE URL ARRAY
$urlist = array();
foreach ($obj->url as $u)
{
    $l = (string)$u->loc;
    $p = (string)$u->priority;
    $d = "$l,$p";
    $d = rtrim($d, ',');
    $urlist[] = $d;
}

// SHOSW THE WORK PRODUCT
print_r($urlist);

Open in new window


Sorry, mobile.ee to copy paste PREVIOUSLY
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39651302
The script uses the SimpleXML class to create an object from the XML string.  In PHP both arrays and objects are iteratible, so you can use foreach() to process objects iteratively, drawing out data elements one at a time in sequence.  That is what we are doing with the $l and $p variables.  In some cases the $p variable does not exists, so after concatenating the $l,$p, we use rtrim() to remove the trailing comma, if any.

Programming with PHP is only about the data, and that is all that PHP does -- transform data from one form to another.  We can use print_r() to visualize the data.  In the case of the $obj variable, it is an object that contains an array of smaller objects.  It looks like this:

SimpleXMLElement Object
(
    [url] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [loc] => http://website.com/
                    [priority] => 1.0
                )

            [1] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/
                    [priority] => 0.9
                )

            [2] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/about/
                    [priority] => 0.9
                )

            [3] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/contact/
                    [priority] => 0.9
                )

            [4] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/
                    [priority] => 0.8
                )

            [5] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/web-wordpress/
                    [priority] => 0.8
                )

            [6] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/social-media/
                    [priority] => 0.8
                )

            [7] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/print/
                    [priority] => 0.8
                )

            [8] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/cabrillo-youth-summer-institute.php
                )

            [9] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/santacruz-popup-artparty.php
                )

            [10] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/red-house.php
                )

            [11] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/what-to-do-in.php
                )

            [12] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/room-service.php
                )

            [13] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/new-evolution.php
                )

            [14] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/film-production-studio.php
                )

            [15] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/rogueIBO.php
                )

            [16] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/booster-bath.php
                )

            [17] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/pilar-macchione.php
                )

            [18] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/katie-mcmahon.php
                )

            [19] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/genesis.php
                )

            [20] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/tequila-jacks.php
                )

            [21] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/em-integrated.php
                )

            [22] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/em.php
                )

            [23] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/orignauxmoose.php
                )

            [24] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/corey-gegner.php
                )

            [25] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/trey-hock.php
                )

            [26] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/nathan-jones.php
                )

            [27] => SimpleXMLElement Object
                (
                    [loc] => http://www.website.com/portfolio/branding/morgan-victoria.php
                )

        )

)

Open in new window

In the case of the $urlist array, it looks like this:

Array
(
    [0] => http://website.com/,1.0
    [1] => http://www.website.com/portfolio/,0.9
    [2] => http://www.website.com/about/,0.9
    [3] => http://www.website.com/contact/,0.9
    [4] => http://www.website.com/portfolio/branding/,0.8
    [5] => http://www.website.com/portfolio/web-wordpress/,0.8
    [6] => http://www.website.com/portfolio/social-media/,0.8
    [7] => http://www.website.com/portfolio/print/,0.8
    [8] => http://www.website.com/portfolio/branding/cabrillo-youth-summer-institute.php
    [9] => http://www.website.com/portfolio/branding/santacruz-popup-artparty.php
    [10] => http://www.website.com/portfolio/branding/red-house.php
    [11] => http://www.website.com/portfolio/branding/what-to-do-in.php
    [12] => http://www.website.com/portfolio/branding/room-service.php
    [13] => http://www.website.com/portfolio/branding/new-evolution.php
    [14] => http://www.website.com/portfolio/branding/film-production-studio.php
    [15] => http://www.website.com/portfolio/branding/rogueIBO.php
    [16] => http://www.website.com/portfolio/branding/booster-bath.php
    [17] => http://www.website.com/portfolio/branding/pilar-macchione.php
    [18] => http://www.website.com/portfolio/branding/katie-mcmahon.php
    [19] => http://www.website.com/portfolio/branding/genesis.php
    [20] => http://www.website.com/portfolio/branding/tequila-jacks.php
    [21] => http://www.website.com/portfolio/branding/em-integrated.php
    [22] => http://www.website.com/portfolio/branding/em.php
    [23] => http://www.website.com/portfolio/branding/orignauxmoose.php
    [24] => http://www.website.com/portfolio/branding/corey-gegner.php
    [25] => http://www.website.com/portfolio/branding/trey-hock.php
    [26] => http://www.website.com/portfolio/branding/nathan-jones.php
    [27] => http://www.website.com/portfolio/branding/morgan-victoria.php
)

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 39653170
The script uses the SimpleXML class to create an object from the XML string.  In PHP both arrays and objects are iteratible, so you can use foreach() to process objects iteratively, drawing out data elements one at a time in sequence.  That is what we are doing with the $l and $p variables.  In some cases the $p variable does not exists, so after concatenating the $l,$p, we use rtrim() to remove the trailing comma, if any.


Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Paypal 502 Error 3 66
php email 2 26
PHP7 and Sql Server Windows 2008 R2 13 85
array_values - reorder after unset? 5 9
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 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 …

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now