File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes location of xsd file 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 "location of xsd file" Watch "location of xsd file" New topic

location of xsd file

S. Palanigounder
Ranch Hand

Joined: Apr 03, 2003
Posts: 145
I'm reading a XML message string from a queue of Websphere MQ and the XML string contains something like:
<myRoot TransactionId="1" xmlns:xsi="" xsi:noNamespaceSchemaLocation="mySchema.xsd">
When I parse the XML string to DOM, I set the parser to validate the XML in
following way:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new DefaultHandler());
Document dom = builder.parse(new InputSource(new StringReader(xmlString)));
I got following error when the program runs:
org.xml.sax.SAXException: Parsing Error
Line: 2
URI: null
Message: Element type "myRoot" must be declared.
I think it is because that parser could not find mySchema.xsd file. Where should I put mySchema.xsd file? Thanks.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
I really have no idea whether this could work but try DocumentBuilder#setEntityResolver(new MySchemaFileResolver())

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
S Kumar

Joined: Nov 14, 2003
Posts: 9
The schemaLocation has to be an absolute path. Since in your example you have just mentioned the file name for schemaLocation, try using the method "public Document parse( is,java.lang.String systemId)" on the DocumentBuilder where systemId is the base for resolving relative path. Also, in case you are not aware, you could try setting the attribute "" on DocumentBuilderFactory to the schemaLocation before getting the DocumentBuilder instance. Hope that helps.
[ November 14, 2003: Message edited by: S K K Kumar ]
Terry Broman

Joined: Jun 18, 2003
Posts: 15
I am unable to get this to work if the schema is inside a jar file. Is there a way to point to a schema inside a jar file and have it work?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
You could try Class#getResource(String) to get a URL to the schema file inside your .jar, and then use the path given by URL#getFile()
I agree. Here's the link:
subject: location of xsd file
It's not a secret anymore!