my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes XML Schema parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Schema parsing" Watch "XML Schema parsing" New topic
Author

XML Schema parsing

ruhail akhtar
Greenhorn

Joined: Apr 20, 2004
Posts: 6
Hello,
I am struggling to find out how can I can I make use of the XML schema, when I parse my XML document with SAX parser. I have to need to find out types and constrainsts of the elements which are defined in XML schema of the XML document.
I am kinda thinking I have to write XML schema parser as well. Any thoughts or suggestions will be appreciated.
Regards,
Akhtar
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The schema document can be used to validate the XML document while parsing. It's basically invisible to you except for the parse() method throwing a SAXParseException if the XML instance doesn't conform to the specified XML Schema.
Take a look at HowToValidateXmlAgainstAnySchema in our wiki.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
clio katz
Ranch Hand

Joined: Apr 30, 2004
Posts: 101
yeah. parsing and validating are two different things.
if you just want to get straight to the core of what's *in* a (perhaps valid) dtd, and passively perform some lightweight validation along the way - don't write your own parser, try something like wutka's or bourret's dtdparser first.
Likewise there are articles about perl/php versions of this (if your box has php) at webreference or zend (expat).
hth
Clarence Schabe
Greenhorn

Joined: May 12, 2004
Posts: 5
Hi,

I am using the sample code provided by the link in the above response:

HowToValidateXmlAgainstAnySchema

but I am getting the error:

Feature not recognized: http: //xml.org/sax/features/validation

which was obviously in response to the line of code:

parser.setFeature("http: //xml.org/sax/features/validation", true);

Can someone help?

Also, does anyone know how to reference a schema that is just on the local development machine - i.e. the line of code:

parser.setProperty("http: //apache.org/xml/properties/schema/external-schemaLocation",
"http://domain.com/mynamespace mySchema.xsd");

How do I point to my schema if it is only on my local development machine at say c:\myFolder\myXsd.xsd?

Many Thanks!
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Feature not recognized: http: //xml.org/sax/features/validation

which was obviously in response to the line of code:

parser.setFeature("http: //xml.org/sax/features/validation", true);

Apparently your parser doesn't recognize that particular feature. Solution? Switch into using another parser, Xerces, for example.

Also, does anyone know how to reference a schema that is just on the local development machine - i.e. the line of code:

parser.setProperty("http: //apache.org/xml/properties/schema/external-schemaLocation",
"http://domain.com/mynamespace mySchema.xsd");

How do I point to my schema if it is only on my local development machine at say c:\myFolder\myXsd.xsd?

Try replacing "mySchema.xsd" with your absolute file path. Notice that the domain.com stuff in that example is the namespace, not the physical location of the schema document.
 
GeeCON Prague 2014
 
subject: XML Schema parsing