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.
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.
A New File dialog box will open, in which on the left block , has different categories and right-hand block, there is a list of documents for the selected category.
Choose XML category and then Select XML Document for the XML category.
We are ready to write our first XML document and it is also possible to validate the XML document using NetBeans IDE.
Note: If you do not see XML category then you should check for Updates for NetBeans IDE.
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)>
<BOOKTITLE>ART OF LIVING</BOOKTITLE>
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.
|Successful Validation of XML document|
If the validation is not successful you need to make the necessary changes in order to make the XML document well-formed.