Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

location of xsd file

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd">
.....
</myRoot>
When I parse the XML string to DOM, I set the parser to validate the XML in
following way:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really have no idea whether this could work but try DocumentBuilder#setEntityResolver(new MySchemaFileResolver())
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(java.io.InputStream 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 "http://java.sun.com/xml/jaxp/properties/schemaSource" on DocumentBuilderFactory to the schemaLocation before getting the DocumentBuilder instance. Hope that helps.
[ November 14, 2003: Message edited by: S K K Kumar ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic