Implementing XML with DTD schemas

0
10137

In this session today, we will learn to implement xml with the DTD schema and validate it using xml validator.

  • To implement xml with DTD schema follow the steps given below :

  1. Create a new folder on the desktop for example xml :
  2. v0

  3. Inside this folder create a new file called as index.xml.
  4. Right click on index.xml and open it with Notepad++ ,so the window will look like as shown below:-
  5. v1

  6. So to create an xml with DTD schema write the following code in Notepad++ .

    <?xml version="1.0"?>
    <movies>
       <movie>
       		<title>The Godfather</title>
    		<year>1972</year>
    		<genre>Drama</genre>
    		<director>Francis Ford Coppola</director>
       </movie>
       <movie>
       		<title>Superbad</title>
    		<year>2007</year>
    		<genre>Comedy</genre>
    		<director>Greg Mottola</director>
       </movie>
       <movie>
       		<title>The Departed</title>
    		<year>2006</year>
    		<genre>Drama</genre>
    		<director>Martin Scorsese</director>
       </movie>
       <movie>
       		<title>Saving Private Ryan</title>
    		<year>1998</year>
    		<genre>Action</genre>
    		<director>Steven Spielberg</director>
       </movie>
       <movie>
       		<title>The Expendables</title>
    		<year>2010</year>
    		<genre>Action</genre>
    		<director>Sylvester Stallone</director>
       </movie>
    </movies>
    
  7. After writting the code you will have the following look of the window :
  8. v2
    v3

  9. So now when you open the file in Internet explorer you will have the following output :
  10. v4
    v5

  11. Create an another file say for example movies.dtd in your xml folder.
  12. v5.1

  13. So now write the following code in Notepad++.

    <!ELEMENT movies (movie)>
    <!ELEMENT movie (title,year,genre,director)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT year (#PCDATA)>
    <!ELEMENT genre (#PCDATA)>
    <!ELEMENT director (#PCDATA)>
    
  14. So after writting the code you will have the following Notepad++ window look out:
  15. v8

  16. Now we have to validate the XML and DTD file, for that we will used the xml validator. (Search on Google, xml validator).
  17. You will get the different xml validator ,with the respective validator you have to validate your xml or DTD document.
  18. v9

  19. So as shown in the abve figure we have to use the w3 validator to run our xml document structure.
  20. v10

  21. Copy and paste our index.xml file code into syntax-checker, hence after doing it you will have following view of your window :
  22. v11

  23. When we click on the validate button it will display the dialog box with a message that no error found,if your xml document is correct but if there is an error in your xml it will display the error description. Otherwise it will simply display No error Found message.
  24. To add the movies.dtd file to the syntax checker First we have to Copy ourindex.xml file code to the syntax-checkeralso mention the movies.dtd file code in it. So the complete code is given below.

    <?xml version="1.0"?>
    <!DOCTYPE movies[
    <!ELEMENT movies (movie)>
    <!ELEMENT movie (title,year,genre,director)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT year (#PCDATA)>
    <!ELEMENT genre (#PCDATA)>
    <!ELEMENT director (#PCDATA)>
    ]>
    <movies>
       <movie>
       		<title>The Godfather</title>
    		<year>1972</year>
    		<genre>Drama</genre>
    		<director>Francis Ford Coppola</director>
       </movie>
       <movie>
       		<title>Superbad</title>
    		<year>2007</year>
    		<genre>Comedy</genre>
    		<director>Greg Mottola</director>
       </movie>
       <movie>
       		<title>The Departed</title>
    		<year>2006</year>
    		<genre>Drama</genre>
    		<director>Martin Scorsese</director>
       </movie>
       <movie>
       		<title>Saving Private Ryan</title>
    		<year>1998</year>
    		<genre>Action</genre>
    		<director>Steven Spielberg</director>
       </movie>
       <movie>
       		<title>The Expendables</title>
    		<year>2010</year>
    		<genre>Action</genre>
    		<director>Sylvester Stallone</director>
       </movie>
    </movies> 
    
  25. Then syntax-checker validates the movies.dtd code with xml (i .e our index.xml ) .This is called XML with DTD schemas.
  26. When we click the validate button at that time it will display the dialog box. If the XML with DTD schemas is correct then it will display No error Found message.
  27. Hence the window will look like as shown below :-
  28. v12

  29. You can also check by giving the filename path,so the window looks like this:-
  30. v13

  31. Thus we have implemented xml with DTD schema and have validated it using xml validator.

LEAVE A REPLY

Please enter your comment!
Please enter your name here