In this post we will create an XML document and validate the document using DTD. The Document Type Definition( DTD) tell us about the building blocks of a XML document. Hence, it is used for validating the XML documents.
Objective:
To create a XML document for BOOKSTORE and write the DTD information before the XML content in the same document.
Then we validate the document using a software tool or an online XML Validator. For this exercise we are using Net Beans, so you may also like to get Net Beans IDE from Oracle or NetBeans.org.
When you create a new Project in Net Beans, Click the new document option, highlighted in below figure using the circle.
Program:
XML Document [ BOOKSTORE.XML ]
Write the following XML information and save the file as BOOKSTORE.xml file.
<?xml version="1.0" encoding="UTF-8"?> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE BOOKSTORE [ <!ELEMENT BOOKSTORE (BOOK*) > <!ELEMENT BOOK (BOOKID,BOOKTITLE,AUTHOR,PUBLISHED)> <!ELEMENT BOOKID (#PCDATA)> <!ELEMENT BOOKTITLE (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT PUBLISHED (#PCDATA)> ]> <BOOKSTORE> <BOOK> <BOOKID> 01</BOOKID> <BOOKTITLE>ART OF LIVING</BOOKTITLE> <AUTHOR>SHANKAR</AUTHOR> <PUBLISHED>2005 </PUBLISHED> </BOOK> <BOOK> <BOOKID> 02</BOOKID> <BOOKTITLE>DISCRETE MATH</BOOKTITLE> <AUTHOR>KENNETH ROSEN</AUTHOR> <PUBLISHED>1998 </PUBLISHED> </BOOK> <BOOK> <BOOKID> 03</BOOKID> <BOOKTITLE>HARRY POTTER</BOOKTITLE> <AUTHOR>J.K.ROWLING</AUTHOR> <PUBLISHED>2001 </PUBLISHED> </BOOK> </BOOKSTORE>
The document starts with DTD information enclosed within < [ …… ] > and then the rest of the document is XML tags.
Note: XML is case sensitive and tag names must be consistent throughout the document.
Validating the XML
To validate the XML Document using NetBeans, Right-Click the XML file and Click Validate XML.
If the document is well-formed, then we will get following output.
If the validation is not successful you need to make the necessary changes in order to make the XML document well-formed.