There are several methods that can be used to display XML on HTML pages. This article touches lightly on some of the more commonly used options available. Learn a brief history of HTML and XML, as well as how to display XML tags on a HTML page.
slide 1 of 4
XML – Extensible Mark-Up Language
Before learning how to display XML tags on HTML pages, it's best to understand what XML is, and how it relates to HTML. When HTML was created, it was designed to transfer data over the Internet easily. Therefore the text-based structure of HTML was highly appreciated. However, over time, HTML began to fall short in terms of displaying data, since the tags were finite and did not cover a broad spectrum of requirements.
However, because XML uses a similar notation for tags as HTML, displaying XML data on HTML pages becomes tricky. This is the gap that XML (Extensible Mark-up Language) was made to fill. In XML, the tags are user-designed. The data is then enclosed in these custom tags and sent across, as easily as HTML. The underlying programs and applications were free to interpret the XML text in whichever manner the developers desired.
slide 2 of 4
var xmlfile = new ActiveXObject("Microsoft.XMLDOM");
In the above code, the variable ‘xmlfile’ is defined as an XML ActiveX object. The file name parameter, testfile.xml, is passed into the code and stored in the variable ‘xmlsource’ by calling the function loadxml. The file is then loaded into the XML object, and inbuilt functions are used to manipulate the data.
slide 3 of 4
Using Escape Characters
The most straightforward, yet tedious, way to display XML tags on an HTML page is to change all the ‘<’ notations to ‘<’ and all the ‘>’ notations to ‘>’.
While the process is painstaking, and there is a great chance of missing some of the signs, this is a sure-fire method of getting the job done. The special characters ‘<’ and ‘>’ will be shown exactly as desired because of the use of HTML escape characters.
However, this option should be considered after every other has been tried, as it is completely manual. If the XML in question needs a change, then the code has to be reworked, therefore it is not a sustainable solution.
slide 4 of 4
Why Display XML on HTML Pages?
Displaying XML code on HTML pages can be necessary for a number of reasons; one of the more popular ones being the need to troubleshoot AJAX code. It is also useful to display XML code in the form of a HTML table, as it then becomes visually easier to understand. Although it's not always easy learning how to display XML code on HTML pages, and learning all of the tags, it is a worthwhile skill to learn, especially if you plan on using XML in the future.