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:
Unmarshaller with No Default Data Validation
Why the Unmarshaller is not generating error on invalid data given in the XML file?
✍: FYIcenter.com
In the Reference Implementation and JRE 8 implementation of JAXB,
the Unmarshaller does not have any default data validation.
If the input XML has any invalid data, the Unmarshaller will will throw data parsing exceptions
or assign null values.
1. Null value example - Create the following invalid XML file, UserError1.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<User ID="2">
<BirthDate>Unknown</BirthDate>
<Name>Frank Y. Ivy</Name>
<Sex>Male</Sex>
</User>
Run the MyXmlToUser.java program given in the previous tutorial. The Unmarshaller returns null for the invalid date in the XML:
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 \ MyXmlToUser.java UserError1.xml Unmarshal XML file to user object... My user object: Name: Frank Y. Ivy BirthDate: null Sex: Male ID: 2 fyicenter> \local\jdk-1.8.0\bin\java MyXmlToUser UserError1.xml Unmarshal XML file to user object... My user object: Name: Frank Y. Ivy BirthDate: null Sex: Male ID: 2
2. Data parsing exception example - Create the following invalid XML file, UserError2.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<User ID="two">
<BirthDate>Unknown</BirthDate>
<Name>Frank Y. Ivy</Name>
<Sex>Male</Sex>
</User>
Run the MyXmlToUser.java program given in the previous tutorial. The Unmarshaller returns NumberFormatException for the invalid integer in the XML:
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 \ MyXmlToUser.java UserError2.xml Exception in thread "main" java.lang.NumberFormatException: Not a number: two at com.sun.xml.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:125) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$18.parse(RuntimeBuiltinLeafInfoImpl.java:742) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$18.parse(RuntimeBuiltinLeafInfoImpl.java:740) ... fyicenter> \local\jdk-1.8.0\bin\javac MyXmlToUser.java fyicenter> \local\jdk-1.8.0\bin\java MyXmlToUser UserError2.xml Exception in thread "main" java.lang.NumberFormatException: Not a number: two at com.sun.xml.internal.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:110) at com.fyicenter.demo.User_JaxbXducedAccessor_id.parse(TransducedAccessor_field_Integer.java:57) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:195) ...
⇒ MyXmlValidator.java - Unmarshal XML File with Schema Validation
⇐ MyXmlToUser.java - Unmarshal XML File to Data Object to
2017-06-30, ∼2171🔥, 0💬
Popular Posts:
Apache Log4j IOStreams is a Log4j API extension that provides numerous classes from java.io that can...
JDK 11 jdk.scripting.nashorn.jm odis the JMOD file for JDK 11 Scripting Nashorn module. JDK 11 Scrip...
maven-model-builder-3.5. 4.jaris the JAR file for Apache Maven 3.5.4 Model Builder module. Apache Ma...
What Is mail.jar of JavaMail 1.4.2? I got the JAR file from javamail-1.4.2.zip. mail.jar in javamail...
JDK 11 jdk.jfr.jmod is the JMOD file for JDK 11 JFR module. JDK 11 JFR module compiled class files a...