Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simplexml grab loc and priority

Posted on 2013-10-22
4
Medium Priority
?
267 Views
Last Modified: 2013-10-29
<?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>

Open in new window



using simplexml

want to echo loc and priority on each line
0
Comment
Question by:rgb192
[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
  • 2
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1200 total points
ID: 39591266
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28273694.html

$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>
</urlset>
EOD;

$obj = SimpleXML_Load_String($xml);

foreach ($obj->url as $x)
{
    echo "<br>$x->loc";
    if (isset($x->priority)) echo " $x->priority";
}

Open in new window

Best regards, ~Ray
0
 
LVL 10

Assisted Solution

by:ienaxxx
ienaxxx earned 800 total points
ID: 39591287
Ray beated me on the speed....

<?php
$XML="<?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>";
if($xmlObj = new SimpleXMLElement($XML)){
	$firstLvl=$xmlObj;
foreach ($firstLvl->children() as $secLvl){
	echo "Loc: ".$secLvl->loc." priority: ".$secLvl->priority."<br />";
	// var_dump($secLvl);
}
}
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39591317
Footnote... It looks like you may have copied the XML from a display made with Internet Explorer or similar.  You need to be careful about that because of all of the hyphens that get inserted into the display listing. Those characters make the XML document invalid and it requires an extra step to remove them before you can process the document.
0
 

Author Closing Comment

by:rgb192
ID: 39610834
Both work great.  Thanks

I like that there is a children option
$firstLvl->children()
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 count occurrences of each item in an array.

670 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