XML Tutorials
  Home arrow XML Tutorials arrow Page 3 - Creating a Well-Formed XML Document
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML TUTORIALS

Creating a Well-Formed XML Document
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2009-02-18

    Table of Contents:
  • Creating a Well-Formed XML Document
  • Mark Up
  • Getting a Root Element
  • Well-Formed

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Well-Formed XML Document - Getting a Root Element


    (Page 3 of 4 )

     

    After your declaration, you will want to include the opening tag for your root element. The root element is the parent element to all of the other elements in your XML document, and therefore everything is nested within it. A good way to think of this is like the <html> tag in the HTML language. It is the root element in your HTML document, and without it, the document will not work. The <head>, <body> and all other tags begin and end between the opening and closing tags of <html> and </html>. The same goes with your root element. The only difference is that you can make your root element anything you please. 

    In our previous example, I chose to make <albums> the root element. We chose <albums> over the root <album> because a person might well purchase more than one album, and then where would we be? 

    Define Your Elements  

    If you recall from the last article, we had three categories: album, customer, and sales. Within each of these categories, we defined sub-categories, or children. This is what is meant by defining your elements. 

    Below is a reminder of the hierarchy of elements, including one that we have yet to touch upon: 

    1. The parent element - Any time that an element has other elements nested within it, it takes on role of a parent element.

    2. The child element - Any element nested within another element is a child element.

    3. Sibling element - When a parent element has more than one child, these children elements become known as siblings. They share the same level in your hierarchy. 

    This means that in our document from the previous article, <albums> was the root (and thus parent) of all elements. Album, Customer, and Sales are all parents (and siblings to one another), and all of the subcategories are children of our parents, and siblings to one another. 

    Here is a look at our XML document, which is now well-formed: 

    <?xml version="1.0" standalone="Yes"?>

    <albums>

    <album>

    <albuminfo>

    <artist></artist>

    <genre><genre>

    <format></format>

    <price></price>

    <uniqueID></uniqueID>

    <label></label>

    <length><length>

    <release></release>

    </albuminfo>

    <customerinfo>

    <first></first>

    <last></last>

    <address>

    <email></email>

    <phone></phone>

    </customerinfo>

    <salesinfo>

    <uniqueID></uniqueID>

    <price></price>

    <coupon></coupon>

    <shipping></shipping>

    <date></date>

    <type></type>

    </salesinfo>

    <totalPrice></totalPrice>

    </album>

    </albums> 

    You may notice that this document looks a little different from the one in our previous article. This is because I made some changes to ensure that the document is now well-formed. You will notice that one of the changes I made was to make totalPrice a parent and remove it as a child from salesinfo. I did this in case there were multiple sales.  

    More XML Tutorials Articles
    More By James Payne


     

    XML TUTORIALS ARTICLES

    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - Creating XML Taxonomies
    - Getting to Know XML
    - A Friendly Approach to XML
    - Creating RSS 2.0 Feeds
    - Using Modules in Your RSS Feed
    - RSS 2.0
    - Querying XML: Use Cases
    - Joins and Query Use with XML
    - Solving Problems by Querying XML
    - Performing Set Operations When Querying XML
    - Querying XML
    - Handling Data for Ajax with JSON
    - Handling XML Data for Ajax





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek