现在有七个相互关联的schema文件,我想用这些文件来验证一xml
xml吧
全部回复
仅看楼主
level 1
import java.io.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
public class xmlValidation {
/**
* @功能描述 TODO
* @创建人 - 017
* @创建时间 2013-5-23 下午5:58:38
* @param args
* @throws SAXException
* @throws IOException
*/
public static void main(String[] args) throws SAXException, IOException {
// 1. Specify you want a factory for RELAX NG
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
// 2. Load the specific schema you want.
// Here I load it from a java.io.File, but we could also use a
// java.net.URL or a javax.xml.transform.Source
File CDAFile = new File("F:\\test\\schema\\sdschemas\\CDA.xsd");
File PCHISFile = new File("F:\\test\\schema\\sdschemas\\PCHIS.xsd");
File POCDFile = new File("F:\\test\\schema\\sdschemas\\POCD_MT000040.xsd");
File datatypesFile = new File("F:\\test\\schema\\CoreSchemas\\datatypes.xsd");
File datatypesBaseFile = new File("F:\\test\\schema\\CoreSchemas\\datatypes-base.xsd");
File NarrativeBlockFile = new File("F:\\test\\schema\\CoreSchemas\\NarrativeBlock.xsd");
File vocFile = new File("F:\\test\\schema\\CoreSchemas\\voc.xsd");
Source[] source = {
new StreamSource(CDAFile),
new StreamSource(PCHISFile),
new StreamSource(POCDFile),
new StreamSource(datatypesFile),
new StreamSource(datatypesBaseFile),
new StreamSource(NarrativeBlockFile),
new StreamSource(vocFile)
};
// 3. Compile the schema.
Schema schema = factory.newSchema(source);
// 4. Get a validator from the schema.
Validator validator = schema.newValidator();
// 5. Parse the document you want to check.
String input = "F:\\test\\201305\\0721ee956c104d32a72beb052bd529d6.xml";
Source source2 = new StreamSource(input);
// 6. Check the document
try {
validator.validate(source2);
System.out.println(input + " is valid.");
} catch (SAXException ex) {
System.out.println(input + " is not valid because ");
System.out.println(ex.getMessage());
}
}
}
总是报错
Exception in thread "main" org.xml.sax.SAXParseException: rcase-Recurse.2: There is not a complete functional mapping between the particles.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.reportSchemaError(XSConstraints.java:276)
    at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.fullSchemaChecking(XSConstraints.java:405)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:530)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:489)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:224)
    at net.aidingwei.util.xmlValidation.main(xmlValidation.java:47)
2013年05月24日 03点05分 1
1