Displaying XML

Welcome to The Coding College, where we simplify coding and programming for learners of all levels! Today, we’ll explore how to display XML data effectively. Whether you’re building a website, a web application, or analyzing data, presenting XML in a readable format is key.

What Does “Displaying XML” Mean?

XML (Extensible Markup Language) is designed to store and transport data. However, displaying XML in a user-friendly way often requires formatting and styling to make it more visually appealing or interactive.

Ways to Display XML

1. Displaying Raw XML in a Browser

Web browsers can display raw XML documents directly, but they are not styled and might appear plain.

Example Raw XML:

<library>  
    <book>  
        <title>XML Basics</title>  
        <author>John Doe</author>  
    </book>  
    <book>  
        <title>Advanced XML</title>  
        <author>Jane Smith</author>  
    </book>  
</library>  

How to Display XML in a Browser

  1. Save the XML data in a file (e.g., books.xml).
  2. Open the file in a web browser.

Result:

  • The browser will display the XML tree structure as plain text.

2. Formatting XML with XSLT

You can use XSLT (Extensible Stylesheet Language Transformations) to transform and style XML for better presentation.

Example:

XML File (books.xml):

<library>  
    <book>  
        <title>XML Basics</title>  
        <author>John Doe</author>  
    </book>  
    <book>  
        <title>Advanced XML</title>  
        <author>Jane Smith</author>  
    </book>  
</library>  

XSLT File (books.xsl):

<?xml version="1.0" encoding="UTF-8"?>  
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  

    <!-- Template for transforming the XML document -->  
    <xsl:template match="/">  
        <html>  
        <head>  
            <title>Library Books</title>  
        </head>  
        <body>  
            <h1>Library Books</h1>  
            <table border="1">  
                <tr>  
                    <th>Title</th>  
                    <th>Author</th>  
                </tr>  
                <xsl:for-each select="library/book">  
                    <tr>  
                        <td><xsl:value-of select="title" /></td>  
                        <td><xsl:value-of select="author" /></td>  
                    </tr>  
                </xsl:for-each>  
            </table>  
        </body>  
        </html>  
    </xsl:template>  

</xsl:stylesheet>  

Linking XML with XSLT

Add a reference to the XSLT file in your XML document:

<?xml version="1.0" encoding="UTF-8"?>  
<?xml-stylesheet type="text/xsl" href="books.xsl"?>  
<library>  
    <book>  
        <title>XML Basics</title>  
        <author>John Doe</author>  
    </book>  
    <book>  
        <title>Advanced XML</title>  
        <author>Jane Smith</author>  
    </book>  
</library>  

Result:
When opened in a browser, the XML document will display as a styled HTML table.

3. Displaying XML with CSS

You can also style XML with CSS for basic formatting.

XML File (books.xml):

<?xml-stylesheet type="text/css" href="books.css"?>  
<library>  
    <book>  
        <title>XML Basics</title>  
        <author>John Doe</author>  
    </book>  
    <book>  
        <title>Advanced XML</title>  
        <author>Jane Smith</author>  
    </book>  
</library>  

CSS File (books.css):

library {  
    display: block;  
    font-family: Arial, sans-serif;  
}  

book {  
    display: block;  
    margin-bottom: 10px;  
    padding: 10px;  
    border: 1px solid #ddd;  
}  

title {  
    font-size: 18px;  
    font-weight: bold;  
    color: #333;  
}  

author {  
    font-size: 14px;  
    color: #666;  
}  

Result:
The XML will display with styled blocks for each book.

4. Displaying XML Dynamically with JavaScript

Using JavaScript, you can dynamically parse and display XML in an interactive way.

HTML and JavaScript Example:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <title>Display XML</title>  
</head>  
<body>  
    <h1>Library Books</h1>  
    <div id="bookList"></div>  

    <script>  
        const xmlData = `  
            <library>  
                <book>  
                    <title>XML Basics</title>  
                    <author>John Doe</author>  
                </book>  
                <book>  
                    <title>Advanced XML</title>  
                    <author>Jane Smith</author>  
                </book>  
            </library>  
        `;  

        const parser = new DOMParser();  
        const xmlDoc = parser.parseFromString(xmlData, "application/xml");  
        const books = xmlDoc.getElementsByTagName("book");  

        let output = "<ul>";  
        for (let book of books) {  
            const title = book.getElementsByTagName("title")[0].textContent;  
            const author = book.getElementsByTagName("author")[0].textContent;  
            output += `<li><strong>${title}</strong> by ${author}</li>`;  
        }  
        output += "</ul>";  

        document.getElementById("bookList").innerHTML = output;  
    </script>  
</body>  
</html>  

Result:
The browser will display a list of books dynamically generated from the XML data.

Tools for Displaying XML

Here are some tools that help display XML data:

  1. Online XML Viewers: Tools like Code Beautify allow you to upload and view formatted XML.
  2. Text Editors with XML Support: Editors like Visual Studio Code and Sublime Text provide syntax highlighting for XML.
  3. Browser Developer Tools: Most browsers let you inspect and debug XML documents in their Developer Tools.

Best Practices for Displaying XML

  1. Use Stylesheets for Readability:
    • Use XSLT or CSS to make XML user-friendly.
  2. Validate XML Before Displaying:
    • Ensure your XML is well-formed and adheres to its schema or DTD.
  3. Format XML Properly:
    • Use indentation and line breaks to improve readability.
  4. Combine with HTML and JavaScript:
    • For interactive displays, use JavaScript to dynamically render XML.

Learn More at The Coding College

Displaying XML data is an essential skill for developers working with structured data. At The Coding College, we provide in-depth tutorials and guides to help you master XML and other programming concepts.

Conclusion

XML is a versatile data format, but making it presentable requires some extra work. Whether you use XSLT, CSS, or JavaScript, the right approach depends on your project requirements.

Leave a Comment