Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
MyXmlValidator.java - Unmarshal XML File with Schema Validation
How to add XML schema validation during the unmarshalling process with JAXB API?
✍: FYIcenter.com
If you want to perform unmarshal-time XML Schema validation on the input XML file,
you need to do the following:
1. Create a SchemaFactory instance and a Schema instance with the XML schema file, User.xsd:
SchemaFactory f = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema s = f.newSchema(new File("User.xsd"));
2. Create an Unmarshaller instance and assign the Schema instance:
JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo"); Unmarshaller m = c.createUnmarshaller(); m.setSchema(s);
3. Call the m.unmarshal() method to unmarshal the input XML file. It will perform the XML Schema validation and return more detailed errors for invalid elements or attributes.
Here is the entire example program,
// Copyright (c) FYIcenter.com import com.fyicenter.demo.User; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import java.io.*; public class MyXmlValidator { public static void main(String[] args) throws Exception { String x = "User.xml"; if (args.length < 1) { System.out.println("USAGE: java MyXmlValidator xml"); System.exit(-1); } x = args[0]; System.out.println("Unmarshal XML file to user object..."); JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo"); Unmarshaller m = c.createUnmarshaller(); SchemaFactory f = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema s = f.newSchema(new File("User.xsd")); m.setSchema(s); User u = (User) m.unmarshal(new FileInputStream(x)); System.out.println("My user object:"); System.out.println(" Name: "+u.getName()); System.out.println(" BirthDate: "+u.getBirthDate()); System.out.println(" Sex: "+u.getSex()); System.out.println(" ID: "+u.getID()); } }
Compile and run MyXmlValidator.java as shown below.
fyicenter> java -cp .:../jaxb-ri/mod/jaxb-api.jar:\ ../jaxb-ri/mod/jaxb-runtime.jar:\ ../jaxb-ri/mod/istack-commons-runtime.jar:\ ../jaxb-ri/mod/javax.activation-api.jar \ MyXmlValidator.java UserError2.xml Unmarshal XML file to user object... Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; cvc-datatype-valid.1.2.1: 'two' is not a valid value for 'integer'.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:340) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:578) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:264) ... fyicenter> \local\jdk-1.8.0\bin\javac MyXmlValidator.java fyicenter> \local\jdk-1.8.0\bin\java MyXmlValidator UserError2.xml Unmarshal XML file to user object... Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; cvc-datatype-valid.1.2.1: 'two' is not a valid value for 'integer'.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249) ...
⇒ FAQ for jaxb-*.jar - Java Architecture for XML Binding
⇐ Unmarshaller with No Default Data Validation
2017-06-30, 1883🔥, 0💬
Popular Posts:
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for m...
ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify ex...
A stream buffer is a stream-based representation of an XML infoset in Java. Stream buffers are desig...
What Is javamail1_1_3.zip? javamail1_1_3.zip is the binary package of JavaMail API 1.1.3 in ZIP form...
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...