Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

how to use jdk 1.5 & dtd to validate xml file

Thread view: 
John_Woo - 23 Jan 2006 15:21 GMT
Hi,

I'm wondering, JDK5 brights with xml parser, validator, etc, but how
can I use its package (or other like web-service packages) to validate
a xml file by a dtd file, before let SAX proceed?

I know an example in SUN's web
(http://java.sun.com/developer/technicalArticles/xml/validationxpath/)

, but it's using xml schema instead of dtd to validate.

Any help?

--
Thanks lots in advance
John
Toronto
zero - 23 Jan 2006 18:18 GMT
"John_Woo" <john_woo@canada.com> wrote in news:1138029717.491824.252800
@g44g2000cwa.googlegroups.com:

> Hi,
>
[quoted text clipped - 8 lines]
>
> Any help?

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("myfile.xml", new MyDefaultHandler());

where MyDefaultHandler extends DefaultHandler.  This code will
automatically validate the xml file if a DTD declaration is present.

packages used are not in javax.xml but org.xml.sax.
John_Woo - 23 Jan 2006 21:26 GMT
Thanks for the info, but when I tried

t.dtd
<!ELEMENT c (trans*) >
<!ATTLIST c  >
<!ELEMENT trans (EMPTY)>
<!ATTLIST trans type CDATA #FIXED "7" >

t.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE c SYSTEM "t.dtd">
<c>
   <trans type="9" />
</c>

it's obviously that value 9 didn't match value 7 defined in dtd,
but running the code below

javax.xml.parsers.SAXParserFactory factory =
javax.xml.parsers.SAXParserFactory.newInstance();

factory.setValidating(true);
javax.xml.parsers.SAXParser saxParser = factory.newSAXParser();
saxParser.parse("t.xml", new My...());

there was no exception found.

Can u test it?

John
zero - 24 Jan 2006 00:24 GMT
"John_Woo" <john_woo@canada.com> wrote in news:1138051619.256468.8820
@f14g2000cwb.googlegroups.com:

> Thanks for the info, but when I tried
>
[quoted text clipped - 26 lines]
>
> John

Validation errors are nonfatal errors, and are ignored by the
ErrorHandler implementation in DefaultHandler.  To throw an exception
when a validation error occurs, you need to override the error
(SAXParseException) and/or warning(SAXParseException) methods.

public void error(SAXParseException e)
  throws SAXParseException
{
  throw e;
}

public void warning(SAXParseException e)
  throws SAXParseException
{
  throw e;
}

See the JAXP tutorial at
http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/
John_Woo - 24 Jan 2006 03:40 GMT
got it, Thanks lots.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.