Part 18 - Learning XML for Beginners
Written by Eric Muss-Barnes, 29 December 2018XML is an abbreviation for "eXtensible Markup Language" and is a very smart alternative to using a database. I have not used XML as frequently as I should have been using it. Back when I began programming, if you wanted to display any kind of dynamic content, you would use a database.
However, that is not a wise way to program. I have learned that was a mistake.
Databases are only useful in two instances:
1.) On very large websites, where a massive quantity of users might need to access data simultaneously.
2.) On websites where you will be editing and changing information through the website itself. For example, a social media website, where people have profile and logins and post and edit information, needs a database.
But...
If your website simply updates some information, such as this website, adding more articles and videos, you can use XML instead. Using XML is far easier and far more efficient.
One great example is the homepage of this site. Let me show you how this site uses XML to list articles and videos on the homepage, using the code below. Note for this example, I am only going to use a few videos, not all of them. I want to keep the code simple enough for you to follow along, so I don't want to confuse you with too much data.
PART 18 - LESSON A - XML DATA LOOP
videos as $videos) { echo "
"; echo ""; echo "
What if you don't want all the records from an XML file? What if you just need to display one?
PART 18 - LESSON B - XML DATA FROM SINGLE NODE
videos as $identify) { // Now we say, "as you loop through all the nodes, if you find an ID which equals '003', assign the value of the corresponding 'videolink' node to a variable called '$YouTubeCode'" if($identify->id == "003") $YouTubeCode = $identify->videolink; } ?>
Similar to the code above, let's pull data from a single node, but instead of hardcoding that node, let's use a querystring.
PART 18 - LESSON C - XML DATA FROM SINGLE NODE WITH QUERYSTRING
videos as $identify) { // Now we say, "as you loop through all the nodes, if you find an ID which equals the querystring ID value, assign the value of the corresponding 'videolink' node to a variable called '$YouTubeCode' and the 'title' node of the video with a variable called '$TitleOfVideo'" if($identify->id == $_GET['myquerystring']) ($YouTubeCode = $identify->videolink) && ($TitleOfVideo = $identify->title); } ?> The first time you hit this page, there will not be a video below. But, one will load when you click one of these links:
First Video
Second Video
Third Video
Fourth Video
The biggest advantage to using XML over a database is simplicity. Creating a new database, linking to it, and writing all the code to parse that data is about twice the amount of code and effort as using the XML file. When you get ready to add more information to the website, you simply edit the XML file and upload the new version via FTP, like you do with all the other files for your website.
And remember, kids, the world owes you nothing... until you create things of value.
Glossary
XML
XML is an abbreviation for "eXtensible Markup Language" and is a markup language with a similar syntax to HTML, but all the tags (called "nodes") are customizeable variables. XML data can be parsed by various scripting languages into actual HTML code and therefore it makes a faster and easier replacement for a full database.Downloads
Other Articles