XML How to Use

Along with its use in many aspects of web development, XML is also used to separate data from presentation.

Separating Data from Presentation:

The information about how to be displayed is not carried by XML. With a full separation between data and presentation, the XML data can be used in many different presentation scenarios.

XML is Often a Complement to HTML:

The HTML is generally used to format and display the same data, while, XML is often used in HTML applications to store or transport data.

Separating Data from HTML:

To not edit the HTML file, when the data changes while displaying data in HTML, we can use XML to store the data in separate XML files. We can read an XML file and update the data content of an HTML page, with just a few lines of JavaScript code.

Example: Books.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Unknown</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
    <author>Author 4</author>
    <year>2004</year>
    <price>350.00</price>
  </book>
 
</bookstore>

Output: XML Tree:

Transaction Data:

To describe day-to-day data transactions, in different industries, thousands of XML formats exist.

  • Stocks and Shares
  • Financial transactions
  • Medical data
  • Mathematical data
  • Scientific measurements
  • News information
  • Weather services

XML News:

For exchanging news and other information, XMLNews is used as a specification which makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<nitf>
  <head>
    <title>CoronaVirus</title>
  </head>
  <body>
    <headline>
      <hl1>Millions Dead in World</hl1>
    </headline>
    <byline>
      <bytag>By Editor, Example Press</bytag>
    </byline>
    <dateline>
      <location>India</location>
      <date>Tuesday June 2 2020 8:28 IST</date>
    </dateline>
  </body>
</nitf>

Output: XML Tree:

XML Weather Service:

In the below example, we are demonstrating an XML national weather service from NOAA (National Oceanic and Atmospheric Administration):

Example:

<?xml version="1.0" encoding="UTF-8"?>
<current_observation>
 
<credit>NOAA's National Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>
 
<image>
  <url>http://weather.gov/images/xml_logo.gif</url>
  <title>NOAA's National Weather Service</title>
  <link>http://weather.gov</link>
</image>
 
<location>New York/John F. Kennedy Intl Airport, NY</location>
<station_id>KJFK</station_id>
<latitude>40.66</latitude>
<longitude>-73.78</longitude>
<observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500 EST
</observation_time_rfc822>
 
<weather>A Few Clouds</weather>
<temp_f>11</temp_f>
<temp_c>-12</temp_c>
<relative_humidity>36</relative_humidity>
<wind_dir>West</wind_dir>
<wind_degrees>280</wind_degrees>
<wind_mph>18.4</wind_mph>
<wind_gust_mph>29</wind_gust_mph>
<pressure_mb>1023.6</pressure_mb>
<pressure_in>30.23</pressure_in>
<dewpoint_f>-11</dewpoint_f>
<dewpoint_c>-24</dewpoint_c>
<windchill_f>-7</windchill_f>
<windchill_c>-22</windchill_c>
<visibility_mi>10.00</visibility_mi>
 
<icon_url_base>http://weather.gov/weather/images/fcicons/</icon_url_base>
<icon_url_name>nfew.jpg</icon_url_name>
<disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_url>
<copyright_url>http://weather.gov/disclaimer.html</copyright_url>
 
</current_observation>

Output: XML Tree:

Please Share