Hi, 
   
  I am trying to use an external schema to validate an xml file by using 
Xerces-J 2.9.1, but somehow it seems the validator couldn't find my schema 
file.  The error message I got is error: Parse error occurred - cvc-elt.1: 
Cannot find the declaration of element 'meta_file'. The following is my code, I 
couldn't figure out what went wrong. Any help is greatly appreciated. Attached 
are the test.xml and test.xsd files.
   
  import java.io.File;
  import java.io.IOException;
  import javax.xml.XMLConstants;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.FactoryConfigurationError;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.transform.Source;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.validation.Schema;
  import javax.xml.validation.SchemaFactory;
  import javax.xml.validation.Validator;
  import org.w3c.dom.Document;
  import org.xml.sax.SAXException;
  
  public class PolicyConverter {
  /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
  protected static final String NAMESPACES_FEATURE_ID = 
"http://xml.org/sax/features/namespaces";;
  /** Validation feature id (http://xml.org/sax/features/validation). */
  protected static final String VALIDATION_FEATURE_ID = 
"http://xml.org/sax/features/validation";;
  /** Schema validation feature id 
(http://apache.org/xml/features/validation/schema). */
  protected static final String SCHEMA_VALIDATION_FEATURE_ID = 
"http://apache.org/xml/features/validation/schema";;
  /** Schema locations feature id 
(http://apache.org/xml/features/honour-all-schemaLocations). */
  protected static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = 
"http://apache.org/xml/features/honour-all-schemaLocations";;
  /** Schema external locations 
(http://apache.org/xml/features/honour-all-schemaLocations). */
  protected static final String SCHEMA_EXTERNAL_LOCATIONS_ID = 
"http://apache.org/xml/properties/schema/external-schemaLocation";;
  /**
  * @param args
  */
  public static void main(String[] args) {
  
  int i = 0;
  
  String xmlFile = "/home/wcai/xerces-2_9_1/data/test.xml"; 
  String xsdFile = "/home/wcai/xerces-2_9_1/data/test.xsd"; 
  String exLocation = "http://www.ibm.com/iam test.xsd";
  
  Document document = null;
  try {
  DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  factory.setNamespaceAware(true);
  
  // factory.setFeature(NAMESPACES_FEATURE_ID, true);
  // factory.setFeature(VALIDATION_FEATURE_ID, true);
  // factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID , true);
  // factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, true);
  document = builder.parse(xmlFile);
  // create a SchemaFactory capable of understanding WXS schemas
  SchemaFactory schemaf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  // load a WXS schema, represented by a Schema instance
  Source schemaFile = new StreamSource(new File(xsdFile));
  Schema myschema = schemaf.newSchema(schemaFile);
  // create a Validator instance, which can be used to validate an instance 
document
  Validator validator = myschema.newValidator();
  validator.setProperty(SCHEMA_EXTERNAL_LOCATIONS_ID,exLocation);
  validator.validate(new DOMSource(document));
  }
  catch (FactoryConfigurationError e) {
  // unable to get a document builder factory
  System.err.println("unable to get a document build factory:"+ e.getMessage());
  e.printStackTrace(System.err);
  } 
  catch (ParserConfigurationException e) {
  // parser was unable to be configured
  System.err.println("unable to get a configure the parser:"+ e.getMessage());
  e.printStackTrace(System.err);
  }
  catch (SAXException e) {
  // parsing error
  System.err.println("error: Parse error occurred - "+e.getMessage());
  e.printStackTrace(System.err);
  
  } 
  catch (IOException e) {
  // io error
  System.err.println("error: Parse error occurred - "+e.getMessage());
  e.printStackTrace(System.err);
  
  }
  // TODO Auto-generated method stub
  }
  }
   
  Thanks
   
  wenling

Attachment: test.xsd
Description: 669643437-test.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<meta_file xmlns="http://www.ibm.com/iam"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";  xsi:schemaLocation="http://www.ibm.com/iam test.xsd">

<system> test </system>

</meta_file>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to