Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼2887🔥, 0💬
Popular Posts:
Rhino JavaScript Java Library is an open-source implementation of JavaScript written entirely in Jav...
commons-net.jar is the bytecode of Apache Commons Net library, which implements the client side of m...
Java Cryptography Extension 1.6 JAR File Size and Download Location: File name: jce.jar, jce-1.6.jar...
This package is the backport of java.util.concurrent API, introduced in Java 5.0 and further refined...
What is jxl.jar 2.6.12? jxl.jar 2.6.12 is the JAR file for Java Excel API 2.6.12, which is a Java li...