troubleshooting Question

How to sort an XML file by "title" using PHP

Avatar of webmanager
webmanagerFlag for Canada asked on
PHPXML
10 Comments1 Solution1213 ViewsLast Modified:
Hi there,

I have an XML file with about 1000 entries in it.

The XML is formatted similar to this:




<?xml version="1.0" encoding="UTF-8"?>
<urlset xmls="http://www.sitemaps.org/schemas/sitemap/0.9">

<url>
<title>Programs</title>
<loc>programs.php</loc>
<changefreq>always</changefreq>
<priority>0.9</priority>
</url>

<url>
<title>Training</title>
<loc>training.php</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>

<url>
<title>College</title>
<loc>college.php</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>

<url>
<title>Academic</title>
<loc>academic.php</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>

<url>
<title>University</title>
<loc>University.php</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>

<url>
<title>Education</title>
<loc>education.php</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>

<url>
<title>Apply now</title>
<loc>apply.php</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>

<url>
<title>Policies</title>
<loc>policies.php</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>

</urlset>



My current PHP code looks like this:

<?php

$xml_file = new SimpleXMLElement('sitemap.xml', null, true);

echo '<ul>';

foreach ($xml_file->url as $itemChild) {
      echo '<li><a href="' . $itemChild->loc . '">' . $itemChild->title . "</a></li>\n";
}

echo '</ul>';
?>


I want to sort the XML by the title attribute, and at each new letter in the alphabet, have a new list started.  Basically, I want to create an automated A-Z index listing.

I'm new to XML, so I'm not sure what to do here.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros