The Difference Between XML and HTML

XML is not a replacement for HTML.

XML and HTML were designed with different goals:

  • XML was designed to describe data, with focus on what data is
  • HTML was designed to display data, with focus on how data looks

HTML is about displaying information, while XML is about carrying information.

XML Components

  • Element

An XML element is everything from (including) the element’s start tag to (including) the element’s end tag.

An element can contain:

  • other elements
  • text
  • attributes
  • or a mix of all of the above…

<bookstore>

 <book category=“CHILDREN”>

   <title>Harry Potter</title>

   <author>J K. Rowling</author>

   <year>2005</year>

   <price>29.99</price>

 </book>

 <book category=“WEB”>

   <title>Learning XML</title>

   <author>Erik T. Ray</author>

   <year>2003</year>

   <price>39.95</price>

 </book>

</bookstore>

In the example above, <bookstore> and <book> have element contents, because they contain other elements. <book> also has an attribute (category=”CHILDREN”). <title>, <author>, <year>, and <price> have text content because they contain text.

  • Attributes

XML elements can have attributes, just like HTML.

Attributes provide additional information about an element.

In HTML, attributes provide additional information about elements:

<img src=”computer.gif”>

<a href=”demo.asp”>

Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:

<file type=”gif”>computer.gif</file>

Attribute values must always be quoted. Either single or double quotes can be used. For a person’s gender, the person element can be written like this:

<person gender=”female”>

or like this:

<person gender=’female’>

If the attribute value itself contains double quotes you can use single quotes, like in this example:

<gangster name=’George “Shotgun” Ziegler’>

or you can use character entities:

<gangster name=”George &quot;Shotgun&quot; Ziegler”>

  • XML Elements vs. Attributes

Take a look at these examples:

<person gender=”female”>

<firstname>Anna</firstname>

<lastname>Smith</lastname>

</person>

 

<person>

<gender>female</gender>

<firstname>Anna</firstname>

<lastname>Smith</lastname>

</person>

In the first example gender is an attribute. In the last, gender is an element. Both examples provide the same information.

There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead

XML Syntax Rules

All XML Elements Must Have a Closing Tag

XML Tags are Case Sensitive

XML Elements Must be Properly Nested

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *