XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Elements

XML Elements

shape Description

Elements is the most important part of the XML Document. XML Elements are logical units of information in an XML document and the information is enclosed in angular brackets < >. Syntax: [xml] <element-name attribute1 attribute2> ....content </element-name> [/xml] where, element-name is the name of the XML element. XML Elements names are case-sensitive i.e. when element starts with upper case letter, it should end with upper-case only. Likewise for lower case also. attribute1 and attribute2 are element attributes which are partitioned by white spaces. An attribute provides single property of an element. It uses a name and value combination that are separated by equal "=" sign. [xml]<a href="http://www.splessons.com/">SPLessons</a>[/xml] In the above example, href is the name of attribute and http://www.splessons.com/ is the value of attribute. Example - 1: [xml] <category>Programming</category> [/xml] Example - 2: [xml] <?xml version="1.0"?> <contact-info> <name>Mike</name> <company>SPLessons</company> <phone>+(011) 011223366</phone> </contact-info> [/xml] Elements can contain text, sub-elements, media objects and attributes. Every XML document can have multiple elements inside it and the scope of them will be decided by the begin and end tags or by empty element.

Root Element

shape Description

XML documents must have a root element inside which all the sub-elements are defined. [xml] <root> <child> <subchild>.....</subchild> </child> </root> [/xml] In the below example, the root element is <note> [xml] <?xml version="1.0" encoding="UTF-8"?> <note> <to>Michael</to> <from>Randy</from> <heading>Invitation</heading> <body>Party Tonight!!</body> </note> [/xml]

Close element

shape Description

All XML elements should have a closing tag( /> ) otherwise, the error is shown. [xml] <!------illegal declaration and not considered-----> <p>This is a paragraph <!-------Legal declaration and considered-----> <p>This is a paragraph</p> [/xml]

Empty element

shape Description

Elements with no content can be expressed like below: [xml]<category></category>[/xml] In shorthand, elements with no content can be defined as below. [xml]</category>[/xml]

XML Elements Naming Rules

shape Description

  • XML Element names must start with a letter either upper or lowercase, A-Z or an underscore(_).
  • It may contain any combination of letters, numbers, dashes(-) or underscores after the first character.
  • It can be of any length.
  • XML element names are case-sensitive.
  • The elements cannot have spaces.

Extensible XML Elements

shape Description

Elements in XML are extensible and can take in more data if required. Suppose, consider the below example which has three elements containing student information. [xml] <student> <id>1</id> <name>John</name> <class>10th standard</class> </student> [/xml] If the school management wants to add some more data to the existing one after few days, that can be done as XML elements are extensible. Additionally for the above details, father-name and mother-name are added. [xml] <student> <id>1</id> <name>John</name> <class>10th standard</class> <father-name>Mike</father-name> <mother-name>Rita</mother-name> </student> [/xml]

Summary

shape Key Points

  • XML Elements are logical units of information in an XML document.
  • XML element names are case-sensitive.
  • Elements in XML are extensible and can take in more data if required.