Tutorials A to Z » Blog Archives

Tag Archives: XML Tutorial

XML Tutorial

XML Namespace

Published by:

XML Namespace


Namespace avoid conflicts with element.  For example: In the example below the XML has 2 section one for book author details and other one is having the details of library customers. Both elements have similar tags that might be bight confusing. So to make it more simpler to understand we use namespaces.

<?xml version = "1.0" encoding = "UTF-8" ?>
<library>	
    <author>
      <name>Honey Maxi</name>
      <age>45</age>
      <gender>Male</gender>
    </author>
    <author>
      <name>Jack Mauga</name>
      <age>39</age>
      <gender>Male</gender>
    </author>	
    <customer>
      <name>Zaphod Beeblebrox</name>
      <age>19</age>
      <gender>Male</gender>
    </customer>	
    <customer>
      <name>June Hox</name>
      <age>15</age>
      <gender>Female</gender>
    </customer>		
</library>
Defining a Namespace


Syntax: xmlns:prefix=”URI

xmlns – Namespace declaration always starts with xmlns which stands for XML namespace.
prefix – Then there is a prefix. The prefix can be any name that adds meaning to the element name.
URI – URI is defined to uniquely identify namespace. An XML cannot have 2 same URI, URI should always be unique.

There are multiple places we can define a namespace:

  • Inside a element
    <author xmlns:author="https://www.tutorialsatoz.com/author">
  • Inside a root element
    <library xmlns:customer="https://www.tutorialsatoz.com/customer" xmlns:author="https://www.tutorialsatoz.com/author">
  • Inside XML definition
    <?xml version = "1.0" encoding = "UTF-8" 
      xmlns:customer="https://www.tutorialsatoz.com/customer" 
      xmlns:author="https://www.tutorialsatoz.com/author"
    ?>
Referring Defined Namespace


Once we have defined our namespace we need to refer our elements using the prefix defined.

<?xml version = "1.0" encoding = "UTF-8" 
  xmlns:customer="https://www.tutorialsatoz.com/customer" 
  xmlns:author="https://www.tutorialsatoz.com/author"
?>
<library>	
    <author>
      <author:name>Honey Maxi</author:name>
      <author:age>45</author:age>
      <author:gender>Male</author:gender>
    </author>
    <author>
      <author:name>Jack Mauga</author:name>
      <author:age>39</author:age>
      <author:gender>Male</author:gender>
    </author>	
    <customer>
      <customer:name>Zaphod Beeblebrox</customer:name>
      <customer:age>19</customer:age>
      <customer:gender>Male</customer:gender>
    </customer>	
    <customer>
      <customer:name>June Hox</customer:name>
      <customer:age>15</customer:age>
      <customer:gender>Female</customer:gender>
    </customer>		
</library>
XML Tutorial

Creating a Simple XML

Published by:

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>
 
XML Tutorial

Understanding XML Structure

Published by:

XML Structure
Structure


XML has XML Definition, below it Root element and inside it its child elements and attribute.


To write a correct XML we need flow few guidelines:

XML Syntax:

<XML Definition>
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>
1. XML Definition:


This is an optional field, but used in most of the XML. This field comes at the very start of your XML and should not even have any space or character before it. This defines the metadata for the XML been used below. It contains XML version and XML file encoding.

XML file Encoding – File encoding depicts various characters a particular file may have. Every encoding have different sets of characters build into it. For example: If you live in US for example, you could go pretty far with ASCII. But in many counties we need characters like ä, å, ü etc. (If so was ASCII only or you try to read this text as ASCII encoded text, you’d see some weird characters in the places of ä, å and ü.) Think also the China, Japan, Thailand and other “exotic” countries, you might need more verity of characters thus need to use a different encoding type.
There are various different types of file encoding, example: UTF-8 (most commonly used), UTF-16, UTF-32, ISO-8859 etc.

<?xml version="1.0" encoding="UTF-8"?>
2. Root Element


This is a mandatory field, and is the parent of all the elements. All the other elements to be defined in XML should and must come inside one root element. There can only be one root element in the XML. Here “student” is the root element and also the parent element to its child element “firstname” and “lastname

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <firstname>Varun</firstname>
    <lastname>Goel</lastname>
</student>
3. Child Elements


All the other elements defined comes inside root element. Which can have other elements or attributes in it.

XML has 2 basic components


  • Elements
  • Attribute
Elements


Elements
Elements are building blocks of XML. Element are defined using tags. There are closing and opening tag in every element in XML.

Opening Tag:

<firstname>

Closing Tag:

</firstname>

Both opening and closing tags when combined together creates element

<firstname></firstname>

Elements can have opening tag; and closing tag inside it. Such elements will not hold any text data inside it. Also know as Empty Elements or empty tag.

<firstname />
Elements may contain attributes, text data or other elements inside it know as child elements.Examples:

  • Elements with Text data
    <firstname>Varun</firstname>
  • Element with attribute
    <firstname gender="female">Varun</firstname>
  • Element with Child Elements: All the elements inside parent element are referred as child elements. Here “student” is an parent element to child element “firstname“.
    <student>
        <firstname gender="male">Varun</firstname>
    </student>
Attributes


Attributes are part of elements and are defined only in opening tag of an element. Attributes define addition information to the element, more of a metadata. There can be no or multiple attributes to an element.
Syntax: name=”vaule”

<firstname gender="female">Varun</firstname>

Here we have defined attribute gender=”male”.

 

XML Tutorial

XML – Why and Its Uses

Published by:

XML stands for Extensible Markup Language.
Why use XML


Suppose, communication is to be established between applications by sending messages. Application A wants to send first names and last name of students to application B and B on receiving the message should acknowledge it sending message “Accepted”. There are many different message formats in which application A can send message to application B. They won’t be able to properly communicate until a proper message structure is defined between application A and B.

Thus, XML defines a message structure for the data that needs to be communicated between different applications.

Other Uses of XML


  • XML can be used to store data.
  • Better for rendering data in a structure format, i.e. records and lists and trees
  • Its is used by many programming tools/software.
  • Can be used to render HTML
Why XML is so widely used


  • Validation – XML allows validation of its data and structure using XSD or Schema.
  • Searchable – Content inside XML can be easily searched using XPATH or XQuery
  • Transformation – XSLT can be used to transform data in XML into desired format.
  • Parsing – XML has Parsing standards: DOM, SAX, StAX
Example of XML


<student>
  <firstname>Varun</firstname>
  <lastname>Goel</lastname>
</student>