Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wiered Exceptions while Validating XML

 
Fred Rogers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fred Rogers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small Typo:

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

Thanks
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fred Rogers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic