XML is the abbreviation for EXtensible Markup Language. It's used to store and transport data. It's both human-readable and meachine -readable format.
There is no pre-defined tags (like
in HTML) in XML. Here, the developer must define the tags and document structure.
XML documents are extensible meaning adding/removing tags to the XML document will not impact the application that process the document.
XML Tree Structure: XML docs are formed as ELEMENT TREES. They have root element and child elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
XML doc components:
1. XML Prolog (Optional) . It contains two parts.
3. child elements
4. xml data
XML Namespaces:
If you use same element name twice with different child elements, there will be conflict with element name. XML Namespace is used to avoid the conflict. use a prefix and define xmlns attribute with unique URL(any dummy URL) value.
Example 1:
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.venkat.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Example 2:
The namespace can also be declared at root element level:
<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>....
..............
Example 3(Default namesapce):
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="http://venkat.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
XSD:
It's the equivalent of DTD. It's the .xsd file that is defined for xml file. In xml file, xsd is referenced as shown below:
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com/venkat"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com/venkat note.xsd"> <to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Explanation:
xmlns - default namespace
schemaLocation - (defalut namespace) (path of .xsd file)
Valid vs Well-formed documents:
If the XML document follows the syntax, it's called well-formed document.
If the XMl document is validated against DTD or XML Schema(xsd), then it;s called both 'well-formed' and 'valid' document.
XPath: It is a language for finding information in an XML doc.
XML Viewer: You can use any browser to view XML file.
How do you create xml from xsd using JAXB:
Sometimes, you will be getting only .xsd file and test data. You need to generate the xml file from the xsd file and test data.
1. Generate java classes from the xsd file using xjc utility
2. write a java classto set data to the generated java classes and to marshall the java objects to xml file using JAXB classes.
There is no pre-defined tags (like
in HTML) in XML. Here, the developer must define the tags and document structure.
XML documents are extensible meaning adding/removing tags to the XML document will not impact the application that process the document.
XML Tree Structure: XML docs are formed as ELEMENT TREES. They have root element and child elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
XML doc components:
1. XML Prolog (Optional) . It contains two parts.
1. XML Declaration(Example: <?xml version="1.0" encoding="UTF-8"?>)
2.DTD Declaration
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. root element (Mandatory) (Document)3. child elements
4. xml data
XML Namespaces:
If you use same element name twice with different child elements, there will be conflict with element name. XML Namespace is used to avoid the conflict. use a prefix and define xmlns attribute with unique URL(any dummy URL) value.
Example 1:
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.venkat.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Example 2:
The namespace can also be declared at root element level:
<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>....
..............
Example 3(Default namesapce):
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="http://venkat.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
XSD:
It's the equivalent of DTD. It's the .xsd file that is defined for xml file. In xml file, xsd is referenced as shown below:
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com/venkat"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com/venkat note.xsd"> <to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Explanation:
xmlns - default namespace
schemaLocation - (defalut namespace) (path of .xsd file)
Valid vs Well-formed documents:
If the XML document follows the syntax, it's called well-formed document.
If the XMl document is validated against DTD or XML Schema(xsd), then it;s called both 'well-formed' and 'valid' document.
XPath: It is a language for finding information in an XML doc.
XML Viewer: You can use any browser to view XML file.
How do you create xml from xsd using JAXB:
Sometimes, you will be getting only .xsd file and test data. You need to generate the xml file from the xsd file and test data.
1. Generate java classes from the xsd file using xjc utility
2. write a java classto set data to the generated java classes and to marshall the java objects to xml file using JAXB classes.