XML Structure |
Structure
XML has XML Definition, below it Root element and inside it its child elements and attribute.
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. <?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
Elements 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:
|
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. <firstname gender="female">Varun</firstname> Here we have defined attribute gender=”male”.
|
Understanding XML Structure
