Intro to XML


Data markup languages

  • XML - Extensible Markup Language
  • DTD's - Document Type Definition & XML elemtents
  • XSD's
  • XSL
  • JSON
  • AJAX

XML

Gives a logical structure that is legible both by humans and computers

HTML docs always start with <html> tag while XML always starts with <?xml version="1.0"?>

HTML does not require tags to be closed while XML does require tags to be closed

HTML tags are not case sensitive while XML tags are case sensitive

HTML does not require quotes around attributes while XML requires some kind of quote around attributes

HTML suppports pre-defined tags while XML allows the developer to define their own tags


XML is used for...

Configuration

  • Server
  • Web services
  • Database mappings (Hibernate, OJB, iBatis...)
  • Framework configs (Struts, Spring, Ant, Nant, Nunit, Junit,...)

Communication

  • Web services
  • Ajax
  • VXML

Well formed XML

Building blocks - elements, attributes, and values

Rules:

  • Declare an XML version - first line - <?xml version="1.0"?>
  • A root element is rewuired
  • Closing tags are required
  • Elements must be properly nested
  • Quotes are required
  • Entity references must be declared

For Example:


<? xml version="1.0" ?>
<musicAlbums>
  <album numSold="triple-platinum">
      <name>Paul's Boutique</name>
      <artist>Beastie Boys</artist>
      <year>1987</year>
      <!-- members is an optional element inside albums -->
      <members>Mike D, MCA, Ad-Rock</members>
  </album>
  <album numSold="multi-platinum">
      <name>Enter the 36 Chambers</name>
      <artist>Wu-Tang Clan</artist>
      <year>1993</year>
  </album>
</musicAlbums>

Naming rules:

  • Case matters - <test></Test> is invalid
  • Names must begin with a letter, underscore, or colon - <1purchase/> is invalid
  • Names may contain letters, digits, underscores, hyphens, periods, and colons
  • Colons are generally only used for defining namespaces
  • Names that begin with the letters x, m, and l (in any comination of upper or lower case) are reserved by W3C. - <xmlbox/> should be invalid

Coding XML

  • Commenting code - <!-- blah blah --/>
  • Empty elements - <firstName value="Charles"/>
  • Formatting is indented
  • Use escaping entities for &, ', " etc. - w3schools.com
  • Display elements as text - <![CDATA[<!nestedXmlFile>...<!/nestedXmlFile>]]>

Good Practice

Keep things generic

  • DON'T:
    <jack> <jill>
  • DO:
    <person name="jack"/>
    <person name="jill"/>

Keep in mind:

  • How does the computer think?
  • How much coding do you want to do to retrieve the data from the XML?