XML (eXtensible Markup Language) - Complete Notes
1. Introduction to XML
Definition:
XML stands for eXtensible Markup Language. It is a markup language used to store and transport
data, designed to be both human-readable and machine-readable.
Purpose:
XML is mainly used to structure, store, and transfer data across systems, especially over the
internet.
Example:
<student>
<name>John Doe</name>
<age>21</age>
<course>Computer Science</course>
</student>
2. Features of XML
- Platform-independent
- Supports Unicode (multi-language)
- Human-readable format
- Self-descriptive structure
- Facilitates data sharing
- Supports nested hierarchical structure
3. XML Syntax Rules
- XML tags are case-sensitive
- Must have a root element
- Tags must be properly closed
- Attributes must be quoted
- XML documents must be well-formed
Example of a Well-Formed XML:
<book>
<title>XML Basics</title>
<author>Jane Smith</author>
<price>299.00</price>
</book>
4. XML vs HTML
XML vs HTML:
Feature | XML | HTML
------------ | ---------------------- | -----------------
Purpose | Store and transport | Display data
Tags | User-defined | Predefined
End Tags | Mandatory | Optional
Structure | Strict | Flexible
Data | Carries data | Displays data
5. XML Elements
Elements contain start tag, content, and end tag.
Syntax:
<elementName>Content</elementName>
Nested Elements:
<student>
<name>Ravi</name>
<roll>23</roll>
</student>
6. XML Attributes
Provide additional information about elements.
Example:
<book category="technology">
<title>XML Guide</title>
</book>
7. XML Prolog
Appears at the top of the XML document. Declares version and encoding.
<?xml version="1.0" encoding="UTF-8"?>
8. XML Schema / DTD
Purpose: Define the structure and rules of an XML document
a. DTD (Document Type Definition):
<!DOCTYPE note [
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
b. XML Schema (XSD):
<xs:element name="age" type="xs:int"/>
9. XML Namespaces
Used to avoid name conflicts when combining XML documents.
<abc:book xmlns:abc="http://www.bookstore.com">
<abc:title>Learning XML</abc:title>
</abc:book>
10. Parsing XML
a. DOM (Document Object Model) Parser:
- Loads entire XML into memory as tree
- Slower but easier to manipulate
b. SAX (Simple API for XML):
- Event-driven parser
- Faster, uses less memory
11. Applications of XML
- Web services (SOAP, REST)
- Data configuration (e.g., Android layouts)
- RSS Feeds
- Document storage (Microsoft Office, OpenOffice)
- Database export/import
12. Tools for Working with XML
Editors: Notepad++, VS Code, Oxygen XML Editor
Parsers: DOM, SAX, StAX
Languages Support: Java, Python, C#, PHP
13. Example Use Case in Android
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello XML" />
14. XML in Android App Development
In Android development, XML is used to define layouts, UI elements, and resources.
15. Common XML Files in Android
a. AndroidManifest.xml - Contains application metadata
b. activity_main.xml - Defines UI layout for an activity
c. strings.xml - Contains string resources
d. colors.xml - Defines color resources
e. styles.xml - Manages themes and styles
16. Commonly Used XML Tags in Android Layouts
1. <LinearLayout>
- Arranges children in a single row or column.
- Attributes:
- android:orientation="vertical/horizontal"
- android:layout_width, android:layout_height
- android:gravity, android:padding
2. <RelativeLayout>
- Allows positioning relative to parent or siblings.
- Attributes:
- android:layout_above, below, toLeftOf, etc.
3. <ConstraintLayout>
- Flexible layout with constraints.
- Attributes:
- app:layout_constraintTop_toTopOf, etc.
4. <FrameLayout>
- Stacks children; only the top child is visible.
5. <TextView>
- Displays text to the user.
- Attributes:
- android:text, android:textSize, android:textColor
6. <EditText>
- Input field for text.
- Attributes:
- android:hint, android:inputType
7. <Button>
- Clickable button.
- Attributes:
- android:text, android:onClick
8. <ImageView>
- Displays image.
- Attributes:
- android:src, android:scaleType
9. <RecyclerView>
- Displays list of items efficiently.
10. <ScrollView>
- Makes content scrollable vertically.
17. Layout Parameters
- android:layout_width / android:layout_height
- wrap_content: size to fit content
- match_parent: fill available space
- android:layout_margin / android:padding
- Specifies outer/inner space
- android:gravity / layout_gravity
- Positions content or view
18. Input Types for EditText
- text, textEmailAddress, textPassword, number, phone
Example:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="text" />
19. State and Visibility
- android:visibility = visible | invisible | gone
- android:enabled = true/false
- android:clickable = true/false
20. Android Resource Types
- drawable/: images and shapes
- layout/: UI layouts
- values/: strings, colors, styles
- mipmap/: launcher icons
- raw/: audio/video files
21. Styles and Themes in XML
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#6200EE</item>
<item name="colorAccent">#03DAC5</item>
</style>
22. Sample Layout Code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your name:"
android:textSize="18sp" />
<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name" />
<Button
android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>