• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Creating a Well-Formed XML Document ...

This article will teach you how to create your first well-formed XML document. I will go through the various rules for creating a well-formed document, beginning with the XML declaration. By the end of the article you will have a firm grasp on how to create well-formed documents in XML.

In our previous article we looked at the basic architecture of an XML document. We learned to create root, parent, and child structures, and put this into practice by creating a taxonomy of fields necessary for a music store.

Part of this article will rehash what we learned in our last article, adding more depth and understanding. First, however, we will look at the XML declaration, as promised above.

XML Declarations 

In XML documents, the first thing that you are required to input is the declaration. With this statement, you declare that the XML document is compliant. This is the first line that you will see in your XML document. Here is how it should be written:

<?xml version="1.0"?>

Or, if you are using XML version 1.1, you would write:

<?xml version="1.1"?>

The processor will garner certain information from the above declaration. First, It will learn that the document is an XML document and second, it will know upon which version of XML the document is built.

You can also add the standalone attribute and a value to the declaration. This informs the processor that your document does not rely on any other document to work (if the value is set to yes, mind you). If you set your attribute to yes in this manner, you can still call on style sheets, DTDs, schemas, etc. You are simply telling the processor that it will have to look no further than this document for actual content.

To add the standalone attribute, you write the declaration like this:

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

You may also want to indicate which encoding you will be using. You can do this by specifying your encoding attribute. There are different encoding attributes from which to choose, but for now we will use UTF 8, or the standard 8-bit ASCII character-set. Here is how you would write the declaration including the encoding attribute:

<?xml version="1.0" standalone="yes" encoding="UTF-8"?>

The XML declaration is a part of what is known as the XML prolog, and can include more data than we have seen here. We will cover these other attributes in a later article however. For now, be satisfied with these basics.
We used some content analysis in our previous article to uncover how to create XML elements and taxonomies. As a refresher, XML elements are made up of other elements and text or content. You can use attributes to further define and give more information about your elements or the content that they contain. In this example, we will create a new element called <album> and give it an attribute called <genre> to provide a better idea of what the content is all about:

<album genre="Rock and Roll"/>

This tells us that the album is genre is rock and roll. It is equivalent to using this code: 

<album>

<genre> Rock and Roll </genre>

</album>

Which method you should use is largely a matter of opinion, but here are some guidelines for you to ask yourself:

   1. Is part of your goal to give definition to a certain part of your element? An example would be the need to give information about a physical aspect of something, such as height, weight, width and so forth.
   2.Would you like to give more information about unique instances of an element? For instance, if you sold iPods, and some were white and others were red.
   3. Do you want to include more information with an element when it is being used? 

If you answer yes to these questions, then you may wish to use the attribute method. As an example, perhaps you are doing a report on all of the albums sold. If you use <genre> as an attribute of <album>, the genre information will always be available on your report, as opposed to having to specify that yes, you would like to retrieve the genre as well.
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. 

 Original Source:
http://www.codewalkers.com/c/a/XML-Tutorials/Creating-a-Well-Formed-XML-Document/

AddThis Social Bookmark Button

Posted at 09:45:39 am | Permalink | Posted in Tutorials  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange