Validating XML with XSD in Java using JDOM and Xerces
This guide demonstrates how to validate XML files against an XSD schema using Java with JDOM and Xerces libraries. It covers the setup of the development environment using Eclipse, Java, and the required JAR files. The example provided includes code that initializes the SAXBuilder, reads the XML and XSD files, and validates their compliance. Success and error handling outputs are included for clarity. Additionally, discussions on alternative solutions and tools within the XML ecosystem are highlighted, offering a comprehensive view for developers working with XML validation.
Validating XML with XSD in Java using JDOM and Xerces
E N D
Presentation Transcript
Schema validationwith java xml xsd 8.10.08
Agenda • Umgebung • Programm • Output 08.10.08
Umgebung • Eclipse 3.3.1.1 • Java 1.5 • Erweiterung http://jdom.org/dist/binary • java archives: jdom.jar und xerces.jar 08.10.08
publicclass Xmlschema { publicstaticvoid main( String[] args ) {try { File xsdFile = new File(“c:\countries.xsd"); File xmlFile = new File(“c:\countries.xml"); SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true); builder.setFeature("http://apache.org/xml/features/validation/schema", true); builder.setProperty(http://apache.org/xml/properties/schema + "/external-noNamespaceSchemaLocation", xsdFile.toURL().toString() ); org.jdom.Document doc = builder.build( xmlFile ); System.out.println( "Successfully parsed and validated" ); catch ( Exception cause ) {System.err.println( cause.toString() );}}} Program 08.10.08
“Output” 08.10.08
“Output” 08.10.08
File: defineexample1.xml xsi:schemaLocation="http://www.cdisc.org/ns/odm/v1.2 define1-0-0.xsd" schema location 08.10.08
schemata define1-0-0.xsd <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/03/xml.xsd"/> <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd"/> <xs:include schemaLocation="http://www.cdisc.org/schema/def/v1.0/define-extension.xsd"/> define-extension.xsd <xs:import namespace="http://www.cdisc.org/ns/def/v1.0" schemaLocation="http://www.cdisc.org/schema/def/v1.0/define-ns.xsd"/> <xs:redefine schemaLocation="http://www.cdisc.org/schema/odm/v1.2.1/ODM1-2-1.xsd"> 08.10.08
Fragen • Gibt es andere Lösungen (sas) ? 08.10.08