Validating XML Document using Internal DTD

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.

Advertisements

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:

Standard Navigation - NetBeans IDE
Standard Navigation – NetBeans IDE
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.
Choose XML Document
Choose XML Document
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.

Advertisements
<?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.

Validate XML Document
Validate XML Document

If the document is well-formed, then we will get following output.

Validation Successful
Validation Successful

If the validation is not successful you need to make the necessary changes in order to make the XML document well-formed.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.