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:
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
2018-05-08, ∼2415🔥, 0💬
Popular Posts:
JDK 11 jdk.jdi.jmod is the JMOD file for JDK 11 JDI (Java Debug Interface) tool. JDK 11 JDI tool com...
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
JDK 11 java.base.jmod is the JMOD file for JDK 11 Base module. JDK 11 Base module compiled class fil...
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
What Is poi-ooxml-3.5.jar? poi-ooxml-3.5.jar is one of the JAR files for Apache POI 3.5, which provi...