Every time you use it to check the XML file, you only need to change the file name in xmldoc. Load ("XML file name"); and then put the file in the browser for execution.
Based on the dialog box that appears in the browser, determine whether the XML you write meets the DTD constraints.
Note:
1. Correct XML file: No syntax error
Valid XML file: The syntax is correct and meets the DTD constraints.
2. You can only use IE to try it out. The try using Firefox is invalid.
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> Yi ye Binzhou </title>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
// Create an IE built-in parser
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
// Enable XML Verification
Xmldoc. validateonparse = "true ";
// Load the XML file to be checked
Xmldoc. Load ("arg_entity.xml ");
// Correct
If (xmldoc. parseerror. Reason. Length = 0 ){
Window. Alert ("valid XML file (correct syntax + DTD constraint ");
// Error
} Else {
Window. Alert (xmldoc. parseerror. Reason );
}
</SCRIPT>
</Body>
</Html>
Self-written XML file: (class. XML)
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype person system "class. DTD">
<! -- Describe a student's information with DTD -->
<Person>
<Student>
<Name> Yi ye Binzhou </Name>
<Age> 23 | </age>
<Sex> male </sex>
</Student>
<Student>
<Name> no regrets </Name>
<Age> 20 </age>
<Sex> male </sex>
</Student>
</Person>
Defined DTD constraint file: (class. DTD)
<? XML version = "1.0" encoding = "UTF-8"?>
<! Element person (student +)>
<! Element student (name, age, sex)>
<! Element name (# pcdata)>
<! Element age (# pcdata)>
<! Element sex (# pcdata)>
Effect:
XML self-defined checker syntax + constraints (1)