Docstoc

XML

Document Sample
XML Powered By Docstoc
					What is XML (Extensible Markup Language)?
XML is basically a better comma delimited file.

Example:
Your client asks you to write a new reporting system for an application that was
written five years ago. Nobody who originally worked on the application is still at
the company. You have to figure out how the app works, parse the data that it
produces and arrange the data into a report.

The application gives you data that looks like this:

Jones, Washer, 401.56, New York
Janson, Blender, 56.90, Austin
Newton, Mixer, 90.00, Waco

This is a comma delimited document. Each column is separated by a comma and
each record is on a different row.
What is XML?
Jones, Washer, 401.56, New York
Janson, Blender, 56.90, Austin
Newton, Mixer, 90.00, Waco

Problems with delimited data:
•What happens if the data itself includes a comma?
    You have to put quotes around the data
•Different documents use different delimiters – so you to write a parser for
    each delimiter
•Different OS have different ideas about what makes the end of a line
    carriage return vs. line feed
•What is this data?
    Name?, Item?, What is the number?
    Without documentation, we have no idea
•Can’t represent related data
    customers and orders in the same document
    hard to represent a hierarchy
•Can’t add new fields without breaking the parser
What is XML?
Same data in XML
<Orders>
  <Order>
    <Customer>Jones</Customer>
    <Item>Washer</Item>
    <Amount>401.56</Amount>
    <City>New York</City>
  </Order>
  <Order>
    <Customer>Janson</Customer>
    <Item>Blender</Item>
    <Amount>56.90</Amount>
    <City>Austin</City>
  </Order>
  <Order>
    <Customer>Newton</Customer>
    <Item>Mixer</Item>
    <Amount>90.00</Amount>
    <City>Waco</City>
  </Order>
</Orders>
Advantages of XML?

•XML documents are readable and self describing
    tags indicate what each piece of data is
•XML is interoperable – not tied to any operating system or technology
    create documents using a text editor
    Parsers exist for virtually every operating system
•XML documents are hierarchical
    easy to add related data
•You don’t have to write the parser
    many already exist
•Changes to the document do not break the parser
    you can add elements without breaking backward compatibility

XML lets you concentrate on writing the application instead of worrying about
the details of the parser.
XML Document Structure and Syntax

•Declaration
     <?xml version=“1.0”?>
First line of every document. Always the same. Not required.
Tells you two things
     •It’s and xml document
     •It conforms to the XML 1.0 W3C recommendation

•Elements
    elements are what contain the data
    sometimes called nodes
    documents must have at least one top level element

Most basic XML document
<?xml version=“1.0”?>
<ORDERS>
</ORDERS>
XML is very picky

Most basic XML document
<?xml version=“1.0”?>
<ORDERS>
</ORDERS>

•Every element must have an open and a closing tag
•Declaration is the only element that does not need a closing tag
•XML is case sensitive
    orders is different from Orders or ORDERS
Elements that contain Data

<?xml version=“1.0”?>
<ORDERS>
  <ORDER>
    <DATETIME>1/4/200</DATETIME>
    <ID>33456</ID>
    <CUSTOMER>Steve Moore</CUSTOMER>
    <TOTALAMOUNT>4563.44</TOTALAMOUNT>
  </ORDER>
</ORDERS>

•Description in English:
It contains a top level ORDERS element and a single ORDER element, or
node. The ORDER node is a child of the ORDERS element. The ORDER
element itself contains four child nodes of its own: DATETIME, ID,
CUSTOMER, and TOTALAMOUNT.
Elements that contain Data

A document containing two orders

<?xml version=“1.0”?>
<ORDERS>
  <ORDER>
    <DATETIME>1/4/2000</DATETIME>
    <ID>33456</ID>
    <CUSTOMER>Steve Moore</CUSTOMER>
    <TOTALAMOUNT>4563.44</TOTALAMOUNT>
  </ORDER>
<ORDER>
    <DATETIME>1/5/2000</DATETIME>
    <ID>56756</ID>
    <CUSTOMER>Jane Wolf</CUSTOMER>
    <TOTALAMOUNT>3456.89</TOTALAMOUNT>
  </ORDER>
</ORDERS>
Attributes

Attributes are another way to enclose a piece of data in an XML document.
An Attribute is part of an element.
It usually modifies or is related to the information in the element.

<?xml version=“1.0”?>
<ORDERS>
  <ORDER id = “33456” custid=“406”>
    <DATETIME>1/4/200</DATETIME>
    <TOTALAMOUNT>4563.44</TOTALAMOUNT>
  </ORDER>
</ORDERS>

Attribute values are enclosed in quotes.
You can use single or double quotes wherever needed.
CDATA
The < character and the & character and throw off the XML parser. The
following could cause a problem

<?xml version=“1.0”?>
<ORDERS>
  <ORDER id = “33456”>
          <NAME>Jones & Williams CPA</Name>
          <DATETIME>1/4/200</DATETIME>
          <TOTALAMOUNT>4563.44</TOTALAMOUNT>
  </ORDER>
</ORDERS>
You can use the a CDATA element to handle this. CDATA tells the parser not to
interpret or parse the characters in the CDATA element

<?xml version=“1.0”?>
<ORDERS>
  <ORDER id = “33456”>
        <NAME><![CDATA[Jones & Williams CPA]]></Name>
        <DATETIME>1/4/200</DATETIME>
        <TOTALAMOUNT>4563.44</TOTALAMOUNT>
  </ORDER>
</ORDERS>
Abbreviated Close Tags
For elements that do not contain data you can use abbreviated syntax.

<?xml version=“1.0”?>
<ORDERS>
  <ORDER id = “33456”>
        <NAME>Jones & Williams CPA</Name>
        <DATETIME>1/4/200</DATETIME>
        <TOTALAMOUNT/>
  </ORDER>
</ORDERS>

instead of

<?xml version=“1.0”?>
<ORDERS>
  <ORDER id = “33456”>
        <NAME><![CDATA[Jones & Williams CPA]]></Name>
        <DATETIME>1/4/200</DATETIME>
        <TOTALAMOUNT></TOTALAMOUNT>
  </ORDER>
</ORDERS>
XML Tools

Microsoft XML Notepad – simple tool for creating XML
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnxml/html/xmlpaddownload.asp
XML Tools

XML Spy – full featured tool

XML editor built into Visual Studio.Net
XML Tools

XML Spy – full featured tool

XML editor built into Visual Studio.Net




  using the dom
  ado xml method
  example app w treeview?

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:1/16/2013
language:English
pages:15