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:
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
2017-06-30, 1449👍, 0💬
Popular Posts:
Jaxen, Release 1.1.1, is an open source XPath library written in Java. It is adaptable to many diffe...
The JSR 105 XML Digital Signature 1.0.1 FCS implementation provides an API and implementation that a...
JDK 11 jdk.javadoc.jmod is the JMOD file for JDK 11 Java Document tool, which can be invoked by the ...
The goal of the Geronimo project is to produce a server runtime framework that pulls together the be...
How to download and install ojdbc5.jar for Oracle 11g R1? ojdbc5.jar for Oracle 11g R1 is a Java 5 J...