Tag: XML

jQuery + PHP to parse XML

jQuery is very well suited for cross browser JavaScript development, but I bet you didn’t know jQuery is also fairly well suited to parsing XML. I often use jQuery to send XMLHttpRequests and then parse the XML returned by a PHP script on the server and then manipulate the return in some way. I won’t really cover exactly what to do with it because this is so dependent on the purpose of your data.

Parsing the XML isn’t overly difficult with jQuery and will allow you to truly use the power of AJAX and eliminate a lot of the complexity with JavaScript. Of course, getElementByTagName and getAttribute aren’t exactly 100% cross browser compatible, and jQuery is pretty much 100% of the time.

Things to Remember

  • PHP must set the content-type to be specified as text/xml or application/xml as seen in the following code example.
  • If you are using .ajax() you must ensure that the dataType is “xml” or you won’t be able to parse the data returned.


<?php
header ("Content-Type: text/xml");
?>

<your_xml_content>
<site ID="1" >
<Title>BRCline Consulting</Title>
<url>www.brcline.com</url>
</site>
</your_xml_content>

<script type="text/javascript" >
$(document).ready(function(){
$.ajax({
type: "GET",
url: "yourURLHere.php",
dataType: "xml",
success: function(xml) {

}
});
});
</script>

Further Reading
jQuery often has a ton of plugins that can increase the flexibility and decrease the amount of time you spend on writing your own “utility” code. One such plugin available to reduce the amount of time spent on writing utility code for parsing XML/RSS feeds is jFeed.

MODx Revolution – Dynamically Generating Google SiteMap XML

Google SiteMap and the other web tools provides a way for the owner or webmaster of a site to easily submit all of the pages to Google and keep track of which ones are crawled and indexed. Wikipedia provides a much more detailed description which goes beyond the scope of this post.

I didn’t want to have to manually try and figure out which links are children of other links for my own site (which again only has 5 pages anyway!) or other things like that, so I was looking for an automatic way to generate the google sitemap and then be able to provide it to google.

MODx Revolution has a plugin called GoogleSiteMap which uses a snippet called GoogleSiteMap I created a page, set the template to empty, and called the snippet directly on the page and then took the xml it created and pasted it into an xml file to submit. The XML it generates even correctly does the last modified date.

Hope this helps!

Creating a Simple RSS Feed

RSS is an acronym for Really Simple Syndication and is an XML specification published by the W3C. RSS became a standardized specification around June 2000. This is meant to be a very simple primer on developing an RSS feed. Please note that this is not meant to replace a detailed book or the specification created by W3C. When I first created an RSS feed for a client the specification was the only way to learn RSS.

There are required elements to meet the specification as well as some optional. Some of the elements that are option really should be included logically.

Each RSS feed begins with a tag describing what XML specification the document meets. For example:

<?xml version=”1.0″?>

After the XML Version, we need to know which version of the RSS specification this document will meet. The rss document will also have a closing </rss> tag at the end.

So far, we have:
<?xml version=”1.0″?>
<rss version=”2.0″>
</rss>

The next tag we have is for a channel. A channel is a reverse-chronological list of links to stories that includes a title, and some sort of description.

The channel has a couple of required elements that are mostly self explanatory. The first one is a title (the name of the channel, usually just the website name), a link (to the website) and a description of the service.

So, we now have:
<?xml version=”1.0″?>
<rss version=”2.0″>
<channel>
<title>Brian R. Cline’s News Feed</title>
<link>http://www.brcline.com</link>

<description>Programming Blog for a Programmer Analyst in Niagara Falls, Ontario, Canada.</description>
</channel>
</rss>

There’s many optional elements to include within the channel, but I don’t feel that most are needed to produce a great and strictly valid RSS feed.

For example, there’s image which lets you specify a GIF,JPEG, or PNG to associate with the channel but not all RSS aggregators can show it.

The bulk of the RSS feed is created by using “items” which represent the story and usually include a title, link, and description. All elements of an item are actually optional, but you should include at least a title and description.

An item is fairly simple, here is an example:

<item>
<title>New Blog Post</title>
<link>http://www.brcline.com/blog/page?=13</link>

<description>Brian R. Cline posted about how to create an RSS feed.</description>
</item>

RSS channel’s usually include more than one item and are updated fairly regularly. I’ve included a couple of different items in the final example.

<?xml version=”1.0″?>
<rss version=”2.0″>
<channel>
<title>Brian R. Cline’s News Feed</title>
<link>http://www.brcline.com</link>

<description>Programming Blog for a Programmer Analyst in Niagara Falls, Ontario, Canada.</description>

<item>

<title>New Blog Post</title>

<link>http://www.brcline.com/blog/page?=13</link>

<description>Brian R. Cline posted about how to create an RSS feed.</description>

</item>

<item>

<title>New Blog Post – Welcome</title>

<link>http://www.brcline.com/blog/page?=1</link>

<description>Brian R. Cline posted a new blog welcoming users to the blog.</description>

</item>

<item>

<title>Blog Created</title>

<link>http://www.brcline.com/blog/ </link>

<description>Brian R. Cline adds wordpress to his website.</description>

</item>
</channel>
</rss>