The Wayback Machine - https://web.archive.org/web/20240814201053/https://www.geeksforgeeks.org/xml-elements/
Open In App

XML | Elements

Last Updated : 24 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The XML elements are the basic building block of the XML document. It is used as a container to store text elements, attributes, media objects etc. Every XML documents contain at least one element whose scopes are delimited by start and end tags or in case of empty elements it is delimited by an empty tag. 

Syntax:

<element-name attributes> Contents...</element-name>
  • element-name: It is the name of element.
  • attributes: The attributes are used to define the XML element property and these attributes are separated by white space. It associates the name with a value, which is a string of characters.

Example:

name="Geeks"
Here, Geeks represents the value of attribute

Rules to define XML elements: There are some rules to create XML elements which are given below:

  • An element can contain alphanumeric values or characters. But only three special characters are required in the names these are hyphen, underscore and period.
  • Names are case sensitive. It means lower case letters have different meaning and upper case characters have different meaning. For example address, Address, aDDress are different names.
  • Both start and end tags for elements need to be same.
  • An element, which is a container, can contain text or elements

Empty Elements: An element in XML document which does not contains the content is known as Empty Element. The basic syntax of empty element in XML as follows:

<elements-name attributename/>

Example 1: Following is the example of an XML document describing the address of a college student using XML elements. 

html




<?xml version = “1.0”?>
<contactinfo>
    <address category = “college”>
        <name>G4G</name>
        <College>Geeksforgeeks</College>
        <mobile>2345456767</mobile>
    </address>
</contactinfo>


Output:

      G4G
      Geeksforgeeks
      2345456767

Example 2: 

html




<?xml version = "1.0"?>
<student>
    <_personal_details = "Personal Details">
        <name>xyz</name>
        <father_name>abc</father_name>
    </personal_details>
    <edu_details = "Educational Details">
        <hsc_perc>80%</hsc_perc>
        <ssc_perc>98%</ssc_perc>
    </edu_details>
</student>


Output:

      xyz 
      abc
      80%
      98%


Similar Reads

Difference Between text/xml and application/xml for Webservice Response
application/xmlThis is the recommended media type for XML data according to the IETF specifications [RFC 7303]. It signifies that the XML content might not be human-readable and could potentially contain processing instructions or other non-human-consumable elements. Syntax:Content-Type: application/xmlThis example demonstrates a web service respon
2 min read
Web technologies | HTML and XML | Question 1
Which of the following is an advantage of putting presentation information in a separate CSS file rather than in HTML itself? (A) The content becomes easy to manage (B) Becomes easy to make site for different devices like mobile by making separate CSS files (C) CSS Files are generally cached and therefore decrease server load and network traffic. (
1 min read
Displaying XML Using CSS
XML stands for Extensible Markup Language. It is a dynamic markup language. It is used to transform data from one form to another form. An XML file can be displayed using two ways. These are as follows :- Cascading Style Sheet Extensible Stylesheet Language Transformation Displaying XML file using CSS : CSS can be used to display the contents of th
4 min read
Java Architecture for XML Binding ( JAXB ) | Set-1
Java Architecture for XML Binding (JAXB) defines an API for reading and writing Java objects to and from XML documents. JAXB gives Java developers an efficient and standard way of mapping between XML and Java code. JAXB makes it easier for developers to extend their applications with XML and Web Services technologies.At First JAXB was developed as
4 min read
Displaying XML Using XSLT
XSLT stands for Extensible Stylesheet Language Transformation. XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspecti
3 min read
XML | Syntax
Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. C/C++ Code &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=
3 min read
XML | Tags
XML tags are the important features of XML document. It is similar to HTML but XML is more flexible then HTML. It allows to create new tags (user defined tags). The first element of XML document is called root element. The simple XML document contain opening tag and closing tag. The XML tags are case sensitive i.e. &lt;root&gt; and &lt;Root&gt; bot
2 min read
XML | Attributes
Prerequisite: XML | BasicsThe XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.Syntax: &lt;element_name attribute1 attribute2 ... &gt; Contents... &lt;/element_name&gt; In the above syntax element_name is the name of an
3 min read
Convert multidimensional array to XML file in PHP
Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file. Example: First, create a PHP Multidimensional Array for converting that array into the XML file
2 min read
PHP | XMLReader XML() Function
The XMLReader::XML() function is an inbuilt function in PHP which is used to set the data containing the XML to parse. XML() function serves the same purpose as open function but the only difference is former accepts XML as string while later accepts it as a separate .xml file. Syntax: bool XMLReader::XML( string $source, string $encoding, int $opt
2 min read
three90RightbarBannerImg