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:
MyUserToXml.java - Marshal Data Object to XML File
How to marshal data objects to XML files using JAXB API?
✍: FYIcenter.com
If you want to marshal data objects to XML files 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 a Marshaller instance from the JAXBContext:
Marshaller m = c.createMarshaller();
3. Finally, marshal the data object with the Marshaller to an XML file. You can set JAXB_FORMATTED_OUTPUT property to true, if you want the output XML file to nicely formatted with line breaks.
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true)); m.marshal(u,new FileOutputStream("UserOutput.xml"));
Here is the entire example program,
// Copyright (c) FYIcenter.com import com.fyicenter.demo.User; import com.fyicenter.demo.ObjectFactory; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import java.io.*; public class MyUserToXml { public static void main(String[] args) throws Exception { XMLGregorianCalendar d = DatatypeFactory.newInstance() .newXMLGregorianCalendarDate(1970, 1, 1, 1); System.out.println("Create user object..."); 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("Marshal user object to XML file..."); JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo"); Marshaller m = c.createMarshaller(); m.marshal(u,new FileOutputStream("UserOutput.xml")); } }
Compile and run MyUserToXml.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 \ MyUserToXml.java Note: MyUserToXml.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Create user objec... Marshal user objec to XML file... fyicenter$ more UserOutput.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <User ID="101"> <BirthDate>1970-01-01+00:01</BirthDate> <Name>Frank Y. Ivy</Name> <Sex>Male</Sex> </User>
The output XML file looks good. It should match the XML schema defined in User.xsd.
If you are still using Java 8, the test also works.
fyicenter> \local\jdk-1.8.0\bin\javac MyUserToXml.java fyicenter> \local\jdk-1.8.0\bin\java MyUserToXml Create user objec... Marshal user objec to XML file... fyicenter> type UserOutput.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <User ID="101"> <BirthDate>1970-01-01+00:01</BirthDate> <Name>Frank Y. Ivy</Name> <Sex>Male</Sex> </User>
Â
⇒ MyXmlToUser.java - Unmarshal XML File to Data Object to
⇠MyUserObject.java - Create Object from Data Type Class
⇑ Examples for JAXB (Java Architecture for XML Binding)
⇑⇑ FAQ for jaxb-*.jar - Java Architecture for XML Binding
2017-06-30, 1353👍, 0💬
Popular Posts:
How to run "jarsigner" command from JDK tools.jar file? "jarsigner" command allows you to digitally ...
JDK 11 jdk.internal.vm.ci.jmod is the JMOD file for JDK 11 Internal VM CI module. JDK 11 Internal VM...
What Is HttpComponents httpcore-4.2.2.jar? HttpComponents httpcore-4.2.2.jar is the JAR file for Apa...
Apache Axis2 is the core engine for Web services. It is a complete re-design and re-write of the wid...
JasperReports, the world's most popular open source business intelligence and reporting engine and J...