Jaxp1.0.1 - Validating XML Against a DTD

wxdqz

New Member
HiI have an XML file and a DTD file. By using JAXP1.0.1's classes, I need tpparse this XML file and process it. But I would like to validate it againsta specified DTD. I have the following program------------import java.io.File;import org.w3c.dom.Document;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;public class val{public static void main(String[] args){Document doc;try{DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();docBuilderFactory.setValidating(true);DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();System.out.println(docBuilder.isValidating());doc = docBuilder.parse (new File (args[0]));System.out.println ("The doctype element of the doc is " +(doc.getDoctype()).getName());}catch (SAXParseException err){System.out.println ("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId ());System.out.println(" " + err.getMessage ());}catch (SAXException e){Exception x = e.getException ();((x == null) ? e : x).printStackTrace ();}catch (Throwable t){t.printStackTrace ();}}}------------------------And I pass the following XML File to it:-------------------<?xml version="1.0" ?><!DOCTYPE Recipe[ <!ELEMENT Recipe (Name, Description?, Ingredients?, Instructions?)><!ELEMENTName (#PCDATA)><!ELEMENT Description (#PCDATA)><!ELEMENT Ingredients (Ingredient)*><!ELEMENTIngredient (Qty, Item)><!ATTLIST Ingredient vegetarian CDATA "true"><!ELEMENTQty (#PCDATA)><!ATTLIST Qty unit CDATA #IMPLIED><!ELEMENT Item (#PCDATA)><!ATTLISTItem optional CDATA "0"><!ELEMENT Instructions (Step)+><!ELEMENT Step (#PCDATA)>]><Recipe>cxvcv<Name>Lime Jello Marshmallow Cottage Cheese Surprise</Name><Description>My grandma's favorite (may she rest in peace.)</Description><Ingredients><Ingredient><Qty unit="box">1</Qty><Item>lime gelatin</Item></Ingredient><Ingredient><Qty unit="g">500</Qty><Item>multicolored tiny marshmallows</Item></Ingredient><Ingredient><Qty unit="ml">500</Qty><Item>Cottage cheese</Item></Ingredient><Ingredient><Qty unit="dash" /><Item optional="1">tabascosauce</Item></Ingredient></Ingredients><Instructions><Step>Prepare lime gelatin according to package instructions...</Step></Instructions></Recipe>---------------------------This is a well-formed but invalid XML, but JAXP fails to capture it.(Notethe usage of text near Recipe element)I cant use a different parser, only Jaxp1.0.1 is allowed. Given this scenario,can someone help me out?Or give a program that validates the XML but uses only the JAXP1.0.1Mark an email also to <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->,Guruprasad
 
Back
Top