This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Wiered Exceptions while Validating  XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Wiered Exceptions while Validating  XML" Watch "Wiered Exceptions while Validating  XML" New topic
Author

Wiered Exceptions while Validating XML

Fred Rogers
Greenhorn

Joined: Apr 20, 2007
Posts: 7
Hi guys,

I am try to validate Sample.xml file against external DTD file (dtdSource.dtd)

I have implemented:


1 SAXParserFactory f = SAXParserFactory.newInstance();
2 f.setNamespaceAware(true);
3 f.setValidating(true); // Default is false
4 SAXParser p = f.newSAXParser();
5 p.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
6 p.setProperty(JAXP_SCHEMA_SOURCE,"C:\\devel\\Taxonomy\\ScionRelate\\XmlToXtmJavaApp\\xtm1.dtd");
7 DefaultHandler h = new DefaultHandler();
8 p.parse(sample,h);


When I do it:
I get

org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.


If I comment 5 and 6, it validate ok even though xml is not correct.

I am using:
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";

Please help. Any suggestions or hints.

Thanks


SCJP 1.5
Fred Rogers
Greenhorn

Joined: Apr 20, 2007
Posts: 7
Small Typo:

Line 6: p.setProperty(JAXP_SCHEMA_SOURCE,"dtdSource.dtd");

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Well, DTDs and schemas are not the same thing. So surely line 5 isn't correct?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
The markup in the document preceding the root element must be well-formed.


I suspect that error comes from trying to read the .DTD file (not an XML format document) as a schema (which is expected to be valid XML).

Bill


Java Resources at www.wbrogden.com
Fred Rogers
Greenhorn

Joined: Apr 20, 2007
Posts: 7
So how do you validate against external .DTD file ? I checked sun's tutorial and it gave me the code like I posted.

Any help ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

I didn't spend too much time googling that but this is the first article I found which tells you that:

http://www.xml.com/pub/a/2005/07/06/jaxp.html
Fred Rogers
Greenhorn

Joined: Apr 20, 2007
Posts: 7
Thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wiered Exceptions while Validating XML
 
Similar Threads
Schema Validation
Validation with JAXP and XML Schema
Problem validating schema
Validation with JAXP and XML Schema
SAXParser not Validating