XML Tutorial

Creating a Simple XML

Creating a Simple XML
When use elements and attributes?


Usage of attributes or elements is usually decided by the data you are trying to model.

For instance, if a certain entity is PART of the data, then it is advisable to make it an element. For example the name of the employee is an essential part of the employee data.

Now if you want to convey METADATA about data (something that provides additional information about the data) but is not really part of the data, then it is better to make it an attribute. For instance, lets say each employee has a GUID needed for back end processing, then making it an attribute is better.(GUID is not something that conveys really useful information to someone looking at the xml, but might be necessary for other purposes).

Data repeats (1 to many), it’s probably an element

Data never repeats, and only makes sense when correlated to something else, it’s an attribute.

There is no rule as such that says something should be an attribute or a element.

Its not necessary to AVOID attributes at all costs..Sometimes they are easier to model, than elements. It really depends on the data you are trying to represent.

Creating an XML with an example:


Here we will be creating an XML for book Library. The following data we need to have in the XML –

  • Book name
  • Book Author
  • Issued To
  • Issued Date
  • Returned Date
Sample 1:


<?xml version = "1.0" encoding = "UTF-8" ?>
<booklist>
  <book name='Guide to the Galaxy 2' author='Honey Maxi'>
        <issued name='Zaphod Beeblebrox'>
      <issueDate>01/01/2019</issueDate>
      <returnDate>14/01/2019</returnDate>
    </issued>	
    </book>
    <book name='On the Run' author='Jack Mauga'>
    <issued name='June Hox'>
      <issueDate>03/01/2019</issueDate>
      <returnDate>24/01/2019</returnDate>
    </issued>
    </book>
</booklist>
Sample 2:


<?xml version = "1.0" encoding = "UTF-8" ?>
<booklist>	
    <book name='Guide to the Galaxy 2'>
    <author>Honey Maxi</author>
    <author>Kira Maxi</author>
    <issued>
      <name>Zaphod Beeblebrox</name>
      <issueDate>01/01/2019</issueDate>
      <returnDate>14/01/2019</returnDate>
    </issued>
    </book>	
    <book name='On the Run'>
    <author>Jack Mauga</author>
    <issued>
      <name>June Hox</name>
      <issueDate>03/01/2019</issueDate>
      <returnDate>24/01/2019</returnDate>
    </issued>
    </book>	
</booklist>
Sample 3:


<?xml version = "1.0" encoding = "UTF-8" ?>
<booklist>	
  <book name='Guide to the Galaxy 2'>
    <bookDetails>
      <author>Honey Maxi</author>
    </bookDetails>		
        <issueDetails>
      <issue>
        <to>Zaphod Beeblebrox</to>
        <issuedOn>01/01/2019</issuedOn>
        <returnedOn>14/01/2019</returnedOn>
      </issue>
    </issueDetails>	
    </book>
  <book name='On the Run'>
    <bookDetails>
      <author>Jack Mauga</author>
    </bookDetails>		
        <issueDetails>
      <issue>
        <to>June Hox</to>
        <issuedOn>03/01/2019</issuedOn>
        <returnedOn>24/01/2019</returnedOn>
      </issue>
    </issueDetails>	
    </book>
</booklist>
 
Varun Goel

About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.

Leave a Reply

Your email address will not be published.