How to use Elements in XML

In this article I have described the element of XML application with the help of XML naming rule.
  • 2247

 XML Elements

  • The building block of XML is the element and Each element has a name and a content .
  • XML must be properly nested
     

    <Element Name>Content</Element Name>
    <Element Name></Element Name>
    <Empty Element Name/>



  • An XML document is a tree of elements. There is no limit to the depth of the tree, and elements can repeat.

XML Element names must follow certain rules

  • Any name can be used, no words are reserved.
  • Names can contain letters, numbers, and other characters.
  • Each element has a name and a content.
  • Names cannot start with a number or punctuation character.
  • XML elements can be extended to carry more information.
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • By convention, HTML elements in XML are always in uppercase.
  • Names cannot contain spaces.
  • Names cannot start with the string "xml", which is reserved for the XML specification itself.

The content of an element is delimited by special markups known as start tag and end tag. The tagging mechanism is similar to HTML, which is logical because both HTML and XML inherited their tagging from SGML.

The following are examples of valid element names in XML:

<copyright-information>
<p>
<base64>
<abc.client>
<firstname>

The following are examples of invalid element names. You could not use these names in XML:

<123>
<first name>
<you&me>

Unlike HTML, names are case sensitive in XML. So, the following names are all different:

<address>
<ADDRESS>
<Address>

By convention, XML elements are frequently written in lowercase. When a name consists of several words, the words are usually separated by a hyphen, as in address-book.

Empty Element

  • Elements that have no content are known as empty elements. Usually, they are enclosed in the document for the value of their attributes.
  • There is a shorthand notation for empty elements: The start and end tags merge and the slash from the end tag is added at the end of the opening tag.

    For XML, the following two elements are identical:

    <email href="manish.mca1987@gmail.com"/>
    <email href="manish.mca1987@gmail.com"></email

Nesting of Elements

  • As I have already described in rule above , the element content is not limited to text; elements can contain other elements that in turn can contain text or elements and so on.

    EXAMPLE

<bookstore>
  
<book category="sport">
    <title>Cricket Master</title>
    <author>Denish Richchi</author>
    <year>2003</year>
    <price>699</price>
  </book>
  <book category="music">
    <title>Learning Music</title>
    <author>javed akhater</author>
    <year>2003</year>
    <price>999</price>
  </book
>
</bookstore>

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2019 DotNetHeaven. All rights reserved.