Getting Familiar To XML

XML stands for Extensible Markup Language it was developed in 1996 by the World Wide Web Consortium’s (W3C’s).
XML is a markup language designed to store and exchange data between applications over the internet, Yes it’s a markup language just like HTML but it has some differences which we will discuss also XML describes data in a very easy way which both humans and computers can understand, let’s step into a small example to see how XML looks like.

<?xml version=”1.0″?>

As we see in the previous example the XML is pretty easy and understandable, Also it looks like HTML consisting of tags and so on, However the tags aren’t predefined as in HTML (i.e., HTML tags like <img> , <p> , …etc are predefined and i only use them to satisfy my needs but in XML i create my own tags that i’ll need to describe my data as shown above).
XML document just store information but i still need an application to retrieve this data and use it, but the document on it’s own is kinda useless, Unlike HTML which i use to display the data XML i use only to store data.

Why I Should Use XML

  • Software & Hardware Independent (i.e., Can be used anywhere regardless of the platform you use)
  • High-Level of availability (i.e., Any application can use XML as a data source).
  • Easy to understand.
  • Easier to exchange (i.e., As a plain text will be the much easier than anything else when it comes to data exchanging)

XML Syntax

<?xml version=”1.0″?>
<Employee ID=”This Is Attribute”>
<FirstName>This Is Element</FirstName>
<LastName>This Is Element</LastName>
<Salary>This Is Element</Salary>

  • First Line defines the xml version 1.0.
  • Second Line defines the root element of the document which is Employee in this example, followed by ID attribute (Will discuss attributes).
  • Third, Fourth And Fifth Lines defines child elements of the root like when i say an Employee have FirstName, LastName and Salary.
  • Last Line defines the end of Employee element (Each Tag Must Be Closed)
  • Elements: Data stored in XML in a structured way so i use Elements to store my data, Check the previous example we will find that Element Employee have Elements (FirstName, LastName, Salary), now if i’m using that document in my application i’ve a guide showing me where the first name of that employee and where is the salary and i can use them easily.

    Syntax: <ElementName>Value</ElementName>
    Elements can contain (another elements, attributed, data)
    Note that Element Names can’t have spaces also XML is case sensitive (i.e.,FirstName != Firstname), Also avoid using (.) or (-) in xml because and application may treat (.) as u r calling property of a class, and application may think that (-) is a subtraction operator.
    Element can contain another elements to describe it (i.e., Employee have first name and last name)

  • Attributes: Provide additional information about element (NOT PART OF THE DATA), as in previous example i used ID as an attribute not as an Element because it’s not a part of employee’s data not like (FirstName, Salary).Syntax: <ElementName AttributeName=”Value”>
    There is no rule specify when to use Attribute or Element but as i mentioned you should use attribute only if you want to provide some information about the data Not the data itself.
    Attributes have some restrictions not like elements as attributes doesn’t contain Multiple values and not easily expanded (i.e., In previous Example i can expand the Salary element to contain FixedSalary & Bonus elements and each of those will be having a value but if i’m using attribute i can’t do this).
    Attributes must be written within a quotes like in the previous example.

Viewing and Editing XML document is pretty easy because xml is usually a text file with a .xml file-name-extension, So it doesn’t require a special software i can use my notepad to view or edit XML document, but of course there is some tools which make it more convenient to develop XML document (i.e., Visual Studio include XML editor that provide intellisense).

Processing an XML document requires software called XML Parser (XML Processor), A parser makes the document data available to application, The parser check that the document follows the syntax rules specified by the W3C’s XML Recommendation (XML syntax requires a single root element, a start tag and an end tag for each element and properly nested tags [End tag for nested element should appear before the end tag of the enclosing element], Also XML is case-sensitive) If all these rules respected so the XML document called Well-Formatted XML Document, Note that if the XML document was not well-formatted the parser will report and error.
Usually XML parser is built into software like Visual Studio also there is an open-source software called Expat XML Parser.

Validating XML Documents: An XML document can OPTIONALLY reference a Document-Type-Definition (DTD) or a W3C XML Schema that defines the XML document’s proper structure.
When XML referencing DTD or W3C XML Schema some parsers called (Validating Parsers) can use the DTD or the Schema to check that it has the appropriate structure, the XML is valid only if it has the appropriate structure.
(ex: In previous XML example if i’ve referenced DTD that specifies that an Employee element MUST have FirstName, LastName, Salary elements and i omitted the LastName [Haven’t Specified It] that will cause my XML document to be invalid, However it will be Well-Formatted XML Document but it doesn’t follow the structure which the referenced DTD specifies).
Note That : Valid XML Document is by default a Well-Formatted XML Document.

<?xml version=”1.0″?>
<!DOCTYPE Employee SYSTEM “Employee.dtd”>
<Employee ID=”This Is Attribute”>
<FirstName>This Is Element</FirstName>
<LastName>This Is Element</LastName>
<Salary>This Is Element</Salary>

Entity References: There is some characters i can’t use directly in XML because it’s used in language syntax like < > ” ‘ &

&lt; < Less Than
&gt; > Greater Than
&amp; & Ampersand
&apos; Apostrophe
&quot; Quotation Mark

That was a brief article about XML which supposed to help you understand the basics of XML, More will be coming later, Hope you enjoyed reading and you got rich information ^^ If there is any question about the topic please mention it 😉


One thought on “Getting Familiar To XML

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s