Home / XML / How to create an XML document?

How to create an XML document?

In any markup language, the first element to appear is called the “root element”, which defines what kind of xml document that file will be. In an HTML file the tag is the root element. An HTML file will always have the HTML element as the root element, while in an XML file it can be anything.

In an XML file there can only be one root element. The root element must encapsulate all other elements, meaning these other elements must show up after the opening root tag and before the closeing root tag. Given below is an example of an XML document with the root element “email”.

With XML we can define the structure of our document.

For example, we want to create an email, then the structure of the same will as follows:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<email>
<sender>
       <sender_name>XYZ Kumar</sender_name>
       <sender_email_id>xyz.kumar@gmail.com.com</sender_email_id>
</sender>
<recipient>
       <recipient_name>ABC</recipient_name>
      <recipient_email_id>visu.k@gmail.com.com</recipient_email_id>
</recipient>
<subject>Hello! how r U</subject>
<message>
      <message_header>Hello dear</message_header>
      <message_body_text>Hello this is Ravish, how are you dear</message_body_text>
       <message_footer>bye!, take care</message_footer>
</message>
</email>

The XML document above, define the structure of an email document.

Another rule for XML files is only one root element per file is allowed, this means no XML file can have two “root” elements. Our previous example followed this rule, but our example below does not because it has two root elements. What are the two root elements?

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<email>
<sender>
       <sender_name> </sender_name>
      <sender_email_id> </sender_email_id>
</sender>
<recipient>
      <recipient_name> </recipient_name>
      <recipient_email_id> </recipient_email_id>
</recipient>
<subject> </subject>
<message>
      <message_header> </message_header>
<message_body_text> </message_body_text>
<message_footer> </message_footer>
</message>
</email>
<mail>
<text>
</text>
</mail>

If you said “email” and “mail” were the two root elements then you got it right! Phone book is the first element to appear in this file, so it is automatically a root element. After the phonebook element is closed, no other elements should follow because then there would be another root element. “mail” is the element that did not follow the one root element rule and transformed this XML file into a lawless, rule-breaking file! and displays error while viewing. Always remember, no XML file can have more than one “root” element.

What we to getting started?

To create an XML document all we need is a plain text editor like Notepad etc. there are some expensive XML editing tools available but as long as we are not using it for complex development where rapid development is needed, we don’t need them. Notepad is a power full editor for XMLediting. You can also try Notepad++, which is an extremly powerful plain text editor and supports more than 20 languages.

Open the Notepad and type the following code and save it with “.xml” extension.