Categories:
Audio (13)
Biotech (29)
Bytecode (35)
Database (77)
Framework (7)
Game (7)
General (512)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (86)
JavaBeans (16)
JDBC (89)
JDK (337)
JSP (20)
Logging (103)
Mail (54)
Messaging (8)
Network (71)
PDF (94)
Report (7)
Scripting (83)
Security (32)
Server (119)
Servlet (17)
SOAP (24)
Testing (50)
Web (19)
XML (301)
Other Resources:
MyUserObject.java - Create Object from Data Type Class
How to create data objects from data type classes generated from the JAXB XJC tool? I have the User.java generated from User.xsd.
✍: FYIcenter.com
If you have the User.java class generated as in the previous tutorial,
you can follow this tutorial to create User data objects in your XML application.
1. Enter the following Java code, MyUserObject.java, in the .\src\ folder:
// Copyright (c) FYIcenter.com import com.fyicenter.demo.User; import com.fyicenter.demo.ObjectFactory; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; public class MyUserObject { public static void main(String[] args) throws Exception { XMLGregorianCalendar d = DatatypeFactory.newInstance() .newXMLGregorianCalendarDate(1970, 1, 1, 1); ObjectFactory f = new ObjectFactory(); User u = f.createUser(); u.setName("Frank Y. Ivy"); u.setBirthDate(d); u.setSex("Male"); u.setID(101); 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()); } }
2. Compile and run MyUserObject.java as shown below. The compiler will automatically compile the User.java code under the same .\src\ folder:
fyicenter$ cd src fyicenter$ javac MyUserObject.java warning: unknown enum constant XmlAccessType.FIELD reason: class file for javax.xml.bind.annotation.XmlAccessType not found 1 warning fyicenter$ java MyUserObject 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> cd src fyicenter> \local\jdk-1.8.0\bin\javac MyUserObject.java fyicenter> \local\jdk-1.8.0\bin\java MyUserObject My user object: Name: Frank Y. Ivy BirthDate: 1970-01-01+00:01 Sex: Male ID: 101
Â
⇒ MyUserToXml.java - Marshal Data Object to XML File
⇠Identify JAXB Implementation Classes
⇑ Examples for JAXB (Java Architecture for XML Binding)
⇑⇑ FAQ for jaxb-*.jar - Java Architecture for XML Binding
2018-05-08, 1137👍, 0💬
Popular Posts:
Apache Log4j IOStreams is a Log4j API extension that provides numerous classes from java.io that can...
Jakarta Regexp is a 100% Pure Java Regular Expression package that was graciously donated to the Apa...
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...
ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify ex...
maven-core-3.5.4.jar is the JAR file for Apache Maven 3.5.4 Core module. Apache Maven is a software ...