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:
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, ∼2112🔥, 0💬
Popular Posts:
JDK 11 jdk.aot.jmod is the JMOD file for JDK 11 Ahead-of-Time (AOT) Compiler module. JDK 11 AOT Comp...
JDK 11 java.desktop.jmod is the JMOD file for JDK 11 Desktop module. JDK 11 Desktop module compiled ...
xml-commons External Source Code Files are provided in the source package file, xml-commons-external...
JRE 8 plugin.jar is the JAR file for JRE 8 Java Control Panel Plugin interface and tools. JRE (Java ...
What Is jsse.jar (JDK 6) Java Secure Socket Extension? jsse.jar, Java Secure Socket Extension, is Ja...