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:
MyXmlToUser.java - Unmarshal XML File to Data Object to
How to unmarshal XML files to data objects using JAXB API?
✍: FYIcenter.com
If you want to unmarshal xml files to data objects using JAXB API,
you can follow these suggestions:
1. Create a JAXBContext instance with the package name of the data type classes generated with the JAXB XJC tool:
JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo");
2. Then create an Unmarshaller instance from the JAXBContext:
Unmarshaller m = c.createUnmarshaller();
3. Finally, unmarshal the XML file to a data object with the Unmarshaller:
User u = (User) m.unmarshal(new FileInputStream("User.xml"));
Here is the entire example program, MyXmlToUser.java:
// Copyright (c) FYIcenter.com import com.fyicenter.demo.User; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import java.io.*; public class MyXmlToUser { public static void main(String[] args) throws Exception { String x = "User.xml"; if (args.length < 1) { System.out.println("USAGE: java MyXmlToUser 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(); 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 MyXmlToUser.java as shown below.
fyicenter$ cd src 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 UserOutput.xml Unmarshal XML file to user object... My user object: Name: Frank Y. Ivy BirthDate: 1970-01-01+00:01 Sex: Male ID: 101
If you are still using Java 8, the test also works.
fyicenter> \local\jdk-1.8.0\bin\javac MyXmlToUser.java fyicenter> \local\jdk-1.8.0\bin\java MyXmlToUser UserOutput.xml Unmarshal XML file to user object... My user object: Name: Frank Y. Ivy BirthDate: 1970-01-01+00:01 Sex: Male ID: 101
⇒ Unmarshaller with No Default Data Validation
⇐ MyUserToXml.java - Marshal Data Object to XML File
2017-06-30, 1667🔥, 0💬
Popular Posts:
Java Cryptography Extension 1.6 JAR File Size and Download Location: File name: jce.jar, jce-1.6.jar...
JRE 8 deploy.jar is the JAR file for JRE 8 Java Control Panel and other deploy tools. JRE (Java Runt...
JDK 11 jrt-fs.jar is the JAR file for JDK 11 JRT-FS (Java RunTime - File System) defined in the "jdk...
What Is log4j-1.2.13.jar? I got the JAR file from logging-log4j-1.2.13.zip .log4j-1.2.13.jar is the ...
What Is HttpComponents httpclient-4.2.2.jar? HttpComponents httpclient-4.2.2.jar is the JAR file for...