Skip to content

xml_examples

These examples illustrate common uses of XML, including structuring data, parsing, and interacting with XML using modern JavaScript.

We’ll also touch upon how XML is handled in contemporary web development.

Examples of well-formed XML documents:

  • A simple note structure (note.xml)
  • An XML CD catalog (cd_catalog.xml)
  • An XML plant catalog (plant_catalog.xml)
  • An XML food menu (food_menu.xml)

Viewing these files typically requires a text editor or a browser, which often displays the XML tree structure.

While raw XML is human-readable, it’s often transformed for presentation.

  • Styling with CSS: Basic presentation rules can be applied using CSS. (Example: Linking cd_catalog.xml with a CSS file). While possible, this is less common for complex display logic.
  • Transformation with XSLT: XSLT is a powerful language for transforming XML into other formats like HTML. (Example: Displaying food_menu.xml styled with XSLT). Still used in specific scenarios, especially document transformation.
  • Client-Side Rendering with JavaScript: Modern web applications often parse XML (or more commonly JSON) and dynamically generate HTML using JavaScript frameworks (like React, Vue, Angular) or plain JavaScript DOM manipulation.

Modern Approaches Explained

JavaScript provides built-in tools to parse XML data fetched from a server or contained within a string.

  • Parsing an XML file using Fetch API and DOMParser (Modern approach)
  • Parsing an XML string using DOMParser

Examples demonstrate how to access data within the XML structure (the DOM).

Parsing Examples Explained

Displaying XML data dynamically within an HTML page is a common task.

  • Fetching XML (e.g., cd_catalog.xml) and rendering it into an HTML table using JavaScript.
  • Loading XML data and displaying specific pieces of information within HTML elements (e.g., <div>, <span>).

Integration Examples Explained

Demonstrating basic application logic using XML data.

  • Navigating through XML nodes (e.g., showing next/previous CD from cd_catalog.xml).
  • Building a simple client-side catalog browser using XML data.

Application Examples Explained

Web servers often provide data in XML format via APIs.

  • How backend frameworks (like Node.js, Python/Flask/Django, Java/Spring, PHP, ASP.NET Core) can serve XML data.
  • Example: Fetching XML output generated from a database.
  • Note: While XML is used, JSON is now more common for web APIs due to its simpler structure and native JavaScript support.

Server Examples Explained

Using JavaScript to interact with the XML DOM:

  • Reading element values and attributes.
  • Modifying element values.
  • Adding new elements or attributes.
  • Removing elements or attributes.

Advanced DOM Examples Explained