Schema

  public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  String path = this.getServletContext().getRealPath("/")
    + "xml\\shiporder.xsd";
  File file = new File(path);
  SchemaFactory factory = SchemaFactory
    .newInstance("http://www.w3.org/2001/XMLSchema");
  Schema schema = null;
  try {
   schema = factory.newSchema(file);
  } catch (SAXException e) {
   e.printStackTrace();
  }
  String input = this.getServletContext().getRealPath("/")
    + "xml\\shiporder.xml";
  Source source = new StreamSource(input);
  Validator validator = schema.newValidator();
  try {
   validator.validate(source);
   out.println(input + "\tis\tvalid");
  } catch (Exception e) {
   out.println(input + "\tis\tnot\tvalid");
   e.printStackTrace();
  }

  注意:path路径不能有空格否则造成以下异常

 

       java.net.MalformedURLException: unknown protocol: d
        at java.net.URL.<init>(URL.java:574)
        at java.net.URL.<init>(URL.java:464)
        at java.net.URL.<init>(URL.java:413)

 

你可能感兴趣的:(java,.net,xml)