dcsimg
Validating XML with SAX and DTD
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Andreas_Nauerz
Posted On:   Monday, February 10, 2003 12:34 PM

Hi, I am currently parsing an XML file with SAX in a non-validating manner. But now I want to switch on validation capabilities. I have therefore added DTD information in front of my XML file. The XML file now looks like this: <?xml version="1.0"?> <!ELEMENT BUDDYLIST (PROCESSTEMPLATE*)> <!ELEMENT PROCESSTEMPLATE (ACTIVITY+)> <!ELEMENT ACTIVITY (GROUP*)> <!ELEMENT GROUP (ROLE*,MEMBER*)> <!ELEMENT ROLE EMPTY> <!ELEMENT MEMBER EMPTY> <!ATTLIST PROCESSTEMPLATE NAME CDATA #REQUIRED>    More>>

Hi,



I am currently parsing an XML file with SAX in a non-validating manner.

But now I want to switch on validation capabilities.



I have therefore added DTD information in front of my XML file.

The XML file now looks like this:



<?xml version="1.0"?>







<!ELEMENT BUDDYLIST (PROCESSTEMPLATE*)>

<!ELEMENT PROCESSTEMPLATE (ACTIVITY+)>

<!ELEMENT ACTIVITY (GROUP*)>

<!ELEMENT GROUP (ROLE*,MEMBER*)>

<!ELEMENT ROLE EMPTY>

<!ELEMENT MEMBER EMPTY>



<!ATTLIST PROCESSTEMPLATE NAME CDATA #REQUIRED>

<!ATTLIST ACTIVITY NAME CDATA #REQUIRED>

<!ATTLIST GROUP NAME CDATA #REQUIRED>

<!ATTLIST ROLE NAME CDATA #REQUIRED>

<!ATTLIST MEMBER TYPE CDATA #REQUIRED>

<!ATTLIST MEMBER NAME CDATA #REQUIRED>



]>















































Okay, I am not quite sure if my DTD is absolutly correct, but let's neglect that for the moment.

I am instantiating my SAX parser via:



// Set up SAX parser

SAXParserFactory spfactory = SAXParserFactory.newInstance();

spfactory.setValidating(false);

SAXParser saxParser = spfactory.newSAXParser();





xmlReader = saxParser.getXMLReader();

xmlReader.setContentHandler(this);

xmlReader.setErrorHandler(this);





// Connect with file to parse

InputSource source = new InputSource(XML_FILE);





So, what do I exactly have to change, i.e. which steps are necessary to switch validation on ?

Can somebody provide a detailed description, please ?



Thanks.



Andreas

   <<Less

Re: Validating XML with SAX and DTD

Posted By:   James_Bayer  
Posted On:   Monday, February 10, 2003 12:54 PM

in your code, replace


spfactory.setValidating(false);



with



spfactory.setValidating(true);



and give that a shot.
About | Sitemap | Contact