tags:

views:

41

answers:

1

Hi. How can I validate an XML document against a DTD file which only my application knows about. So that the XML document which should be validated does not contain the DOCTYPE declaration which specifies the .dtd file. I need this in Java. Here is an example: The xml file to be validated:

<?xml version = "1.0" ?>
<Employee>
  <Emp_Id> E-001 </Emp_Id>
  <Emp_Name> Vinod </Emp_Name>
  <Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

The .dtd file from my application:

<!ELEMENT Employee (Emp_Id, Emp_Name, Emp_E-mail)>
<!ELEMENT Emp_Id (#PCDATA)>
<!ELEMENT Emp_Name (#PCDATA)>
<!ELEMENT Emp_E-mail (#PCDATA)>
+1  A: 

Read the Java Documentation - API for validation of XML documents

SUMMARY:This package provides an API for validation of XML documents. Validation is the process of verifying that an XML document is an instance of a specified XML schema. An XML schema defines the content model (also called a grammar or vocabulary) that its instance documents will represent.

Example,

Document xmlDocument = builder.parse(new FileInputStream("xmlDoc.xml"));
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "xmlDoc.dtd");
transformer.transform(source, result);
adatapost