1

I need to validate an XML against a local XSD and I do not have a internet connection on the target machine (on which this process runs). The code look like below :

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

File schemaLocation = new File(xsd);

Schema schema = factory.newSchema(schemaLocation);

Validator validator = schema.newValidator();

Source source = new StreamSource(new BufferedInputStream(new FileInputStream(new File(xml))));

validator.validate(source);

I always get a java.net.ConnectException when validate() is called.

Can you please let me know what is not being done correctly ?

Many Thanks. Abhishek

Abhishek
  • 11
  • 2
  • It would be helpful to see the XML and Schema. You likely have a reference to an external location for your schema fragments, DTD, etc. You will need to find them, download local copies, and change the reference to local system paths. – Mads Hansen Aug 02 '11 at 12:22

1 Answers1

1

Agreed with Mads' comment - there are likely many references here that will attempt outgoing connections to the Internet, and you will need to download local copies for them. However, I'd advise against changing references within the XML or schema files, etc. - but instead, provide an EntityResolver to return the contents of your local copies instead of connecting out to the Internet. (I previously wrote a little bit about this at http://blogger.ziesemer.com/2009/01/xml-and-xslt-tips-and-tricks-for-java.html#InputValidation.)

However, in your case, since you're using a Validator instead of Validator.setResourceResolver(...) - and pass-in a LSResourceResolver, before calling validate.

ziesemer
  • 27,712
  • 8
  • 86
  • 94