Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Software Engineering Tutorial
  • Software Development Life Cycle
  • Waterfall Model
  • Software Requirements
  • Software Measurement and Metrics
  • Software Design Process
  • System configuration management
  • Software Maintenance
  • Software Development Tutorial
  • Software Testing Tutorial
  • Product Management Tutorial
  • Project Management Tutorial
  • Agile Methodology
  • Selenium Basics
Open In App
Next Article:
XML Tutorial
Next article icon

XML | Syntax

Last Updated : 22 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

XML




<?xml version="1.0" encoding="UTF-8"?>
<message>
   <to>Students</to>
   <from>Teacher</from>
   <subject>Regarding assignment submission</subject>
   <text>All students will have to submit assignment by tomorrow.</text>
</message>
 
 

Syntax rules for XML declaration or XML Prolog: 

XML




<?xml version="1.0" encoding="UTF-8"?>
 
 

Below is the explanation of each point.

  • This line is called XML Prolog or XML declaration.
  • This line is optional i.e, it can be either used or not in an XML document. However, it should be the very first line if used.
  • The version=”1.0″ is the version of the XML currently used. There are various versions of XML available.
  • The encoding=”UTF-8″ specifies the character encoding used while writing an XML document, for example, êèé is for French and so on. Its default value is “UTF-8”. For more about character encoding click here.
  • This declaration is case sensitive for example “xml” should be in lower case in .

Syntax rules for Root Element:

  • Every XML files should have one or more Root elements to avoid error. For example below code is wrong because it’s not containing Root element. 

XML




<to>Students</to>
<from>Teacher</from>
<subject>Regarding assignment submission</subject>
<text>All students will have to submit assignment by tomorrow.</text>
 
 
  • In the first example the Root element is <message> and all the remaining elements <to>, <from> etc is the child element and reside within the root element.
  • It is case sensitive.
  • The XML element should have a closing element for example <text category = “message”>Hi</text> is correct but <text category = “message”>Hi is not correct because it does not contain the closing element and it will throw an error and vice-versa.
  • The elements in XML should be nested properly otherwise it will throw an error. For example <to><from>Geeks</from></to> is nested correctly but <to><from>Geeks</to></from> is wrong because if <from> is opened inside the <to> element then this should also end inside of the </to> element.
  • It is also case sensitive i.e, the starting and closing element should be in the same case. For example <to>….</to> is correct but <to>…..</To> is not correct and it will throw an error.
  • The XML attribute is having two part one is Name and other is its value. It resides inside of the opening of an XML element. For example: <text category = “message”>All students will have to submit the assignment by tomorrow.</text> Here category is the attribute name and message is its value and the attribute value should either be in a single quotation or in double quotation otherwise it will throw an error. The Attribute Name is written without any quotation.
  • The XML attribute is also case sensitive.
  • An XML element can have multiple attributes but can not have the same attribute names in the same element. For example: <text category =”message” purpose = “greet”>GeeksforGeeks</text> Above attributes is correct because of having multiple attributes with the different attribute name. <text category =”message” category = “greet”>GeeksforGeeks</text> Above attribute is wrong because of having the same attribute name in a single element.

XML Comments: Correct syntax for writing XML comments are: <!– It is comment section –>
Incorrect comments: <!– It is comment — section –> i.e. Two dashes in between the comment is not allowed.



Next Article
XML Tutorial

K

Kanchan_Ray
Improve
Article Tags :
  • Software Engineering
  • HTML and XML

Similar Reads

  • 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 cas
    2 min read
  • XML | Elements
    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 emp
    2 min read
  • XML Tutorial
    XML, or Extensible Markup Language, is a way to structure and organize information in a text format that is easy for computers to read. It uses tags, similar to those in HTML, to define different types of data inside a document. XML allows data to be stored and shared in a consistent and structured
    4 min read
  • XML | Basics
    Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. It is a textual data format with strong s
    3 min read
  • HTML vs XML
    HTML (Hyper Text Markup Language) is used to create web pages and web applications. It is a markup language. By HTML we can create our static page. It is used for displaying the data not to transport the data. HTML is a combination of Hypertext and Markup language. Hypertext defines the link between
    3 min read
  • What are XML Entities ?
    XML is a way to organize information on the internet. It's like a recipe that tells computers how to understand and use the information. Entities in XML are like ingredients in a recipe. They help you define and reuse parts of your information, making it easier to manage and share. What is XML?XML s
    8 min read
  • XML - CDATA Sections
    CDATA sections are a mechanism in XML for handling character data that might otherwise be misinterpreted by the XML parser. CDATA stands for Character Data. These sections include blocks of text within an XML document that the parser should treat literally, without interpreting any characters as XML
    2 min read
  • XHTML Introduction
    XHTML or EXtensible HyperText Markup Language is a mix of HTML and XML, very similar to HTML but stricter. It's like a rulebook for creating web pages that browsers easily understand. Unlike HTML, you have to be careful and follow the rules exactly. Most browsers support it. Just think of it as a mo
    5 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
    3 min read
  • XML full form
    Extensible Markup Language (XML) is a type of markup language that establishes a set of guidelines for encoding texts in a way that is both machine- and human-readable. For storing and transferring data on the web and in many other applications, XML is widely used. Getting Started with XMLPresented
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences