xml
  1. xml-example

XML Example

XML is a powerful markup language for storing and transporting structured data.

Syntax

<tagname attribute1="value1" attribute2="value2">
    Content goes here.
</tagname>

Example

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J.K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

Output

The above XML code creates a hierarchy of elements where every element has a name and can contain child elements. Here is the output:

<bookstore>
   <book category = "cooking">
      <title lang = "en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   
   <book category = "children">
      <title lang = "en">Harry Potter</title>
      <author>J.K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
   </book>
   
   <book category = "web">
      <title lang = "en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
   </book>
</bookstore>

Explanation

  • Tags: XML elements are marked up using tags, which are enclosed in angle brackets. For example, <book> is a tag that surrounds the book element’s content.
  • Attributes: Elements can also have attributes. For example, the book element has a category attribute.

Use

  • Structured data: XML is used to store and transport structured data.
  • Web services: XML is used in web services to send and receive messages in a standard format.
  • Configuration files: XML is used in configuration files to store settings and preferences.

Important Points

  • XML stands for eXtensible Markup Language.
  • XML is a markup language much like HTML.
  • XML was designed to store and transport data.
  • XML tags are not predefined. You must define your own tags.
  • XML is designed to be self-descriptive.

Summary

In summary, XML is a powerful markup language for storing and transporting structured data. It allows you to define your own custom tags and attributes, and it’s widely used in web services and configuration files. With this example, you should be able to get started with creating and using XML code.

Published on: