Troubleshooting
Problem
This technote provides a sample IBM® Rational® Robot GUI script which uses the "LoadXml" method to validate an XML file based on a DTD file.
Resolving The Problem
Use the "LoadXML" method in a GUI script to load a XML file for validation. The following GUI script validates an XML file (library.xml) against a DTD file (library.dtd).
XML sample - library.xml (Note: <!doctype> tag is not included in this example)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Library>
<Book rating="2" noofpages="300">
<BookNo> 1 </BookNo>
<Author> XYZ </Author>
<Publisher> XYZ </Publisher>
<Price> 300 </Price>
</Book>
<Book rating="2" noofpages="400">
<BookNo> 2 </BookNo>
<Author> ABC </Author>
<Publisher> ABC </Publisher>
<Price> 400 </Price>
</Book >
<Book rating="7" noofpages="400">
<BookNo>3</BookNo>
<Author> PQR </Author>
<Publisher> PQR </Publisher>
<Price> 500 </Price>
</Book>
</Library>
DTD sample - library.dtd
<!ELEMENT Library (Book)+>
<!ELEMENT Book (BookNo,Author,Publisher,Price)>
<!ATTLIST Book rating (1|2|3|4) "1" noofpages CDATA #REQUIRED>
<!ELEMENT BookNo (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
According to the DTD file above, the value of the book rating can be 1, 2, 3 or 4. However, the book number "3" in library.xml contains the book rating of '7', which is an invalid XML reference.
See below for GUI Script sample, this validates library.xml against library.dtd.
Sub Main
Dim Result As Integer
Dim objXml as object
Dim objTempXml as Object
Dim strXml as String
Dim strXmlFile as String
Dim strDtdFile as String
'XML file to be validated
strXmlFile = "C:\library.xml"
'DOCTYPE tag for the XML file
strDtdFile = "<!DOCTYPE Library SYSTEM ""C:\library.dtd"">"
'Create objects of XMLDOM
set objXml = CreateObject("Microsoft.XMLDOM")
set objTempXml = CreateObject("Microsoft.XMLDOM")
'Load the XML file to be validated
objXml.Load(strXmlFile)
'form a string containing the whole XML document
'.ChildNodes.item(0) is to hold the XML tag <?xml version="1.0" encoding="UTF-8" standalone="no"?>
strXml = "" & objXml.ChildNodes.item(0).xml & Chr(13)
'include the DOCTYPE tag
strXml = strXml & strDtdFile & Chr(13)
'include rest of the contents of the XML document
strXml = strXml & objXml.ChildNodes.item(1).xml
'load the XML using the string
if objTempXml.LoadXml(strXml) then
'if successfully loaded
msgbox "Xml file successfully loaded. Follows DTD specification"
else
'if error occurs while loading, display the message
msgbox "Error occurred while loading Xml (invalid XML)."
msgbox "Error: " & objTempXml.ParseError.reason & " at line " & objTempXml.ParseError.line
end if
End Sub
Message box 1
- Error occurred while loading XML (invalid XML).
- Error: Attribute 'rating' has an invalid value according to the DTD/Schema. at line 16
Historical Number
157089014
Was this topic helpful?
Document Information
More support for:
Rational Robot
Software version:
2001a, 2002, 2003.06.00, 2003.06.12, 2003.06.13, 2003.06.14
Operating system(s):
Windows
Document number:
329001
Modified date:
16 June 2018
UID
swg21132400