XJC(1)marshaller and unmarshaller JAVA XML

本文介绍了如何利用JAXB(Java Architecture for XML Binding)将Java对象转换为XML字符串,并通过反序列化将XML字符串转换回Java对象的过程。通过遵循一系列步骤,包括准备环境、序列化Java对象到XML、反序列化XML到Java对象,实现了数据在Java和XML之间的双向转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XJC(1)marshaller and unmarshaller JAVA XML

1. Prepare the envirenment
I take the example in the article.

First, I have xsd in place. Which is expense.xsd from the article.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="expenseReport" type="ExpenseT" />

<xs:complexType name="ExpenseT">
<xs:sequence>
<xs:element name="user" type="UserT" />
<xs:element name="items" type="ItemListT" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="UserT">
<xs:sequence>
<xs:element name="userName" type="xs:string" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="ItemListT">
<xs:sequence>
<xs:element name="item" type="ItemT" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="ItemT">
<xs:sequence>
<xs:element name="itemName" type="xs:string" />
<xs:element name="purchasedOn" type="xs:string" />
<xs:element name="amount" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Then I follow the suggestion from my collegue. I use xjc in command window.
syntax: xjc -p package.name xsdFileName
>xjc expense.xsd
The default package for this command is generated.

2. marshaller the JAVA Object 2 XML String
Use objectFactory to prepare the JAVA Object
ObjectFactory factory = new ObjectFactory();
UserT user = factory.createUserT();
user.setUserName("Sanaulla");

ItemT item = factory.createItemT();
item.setItemName("Seagate External HDD");
item.setPurchasedOn("August 24, 2010");
item.setAmount(new BigDecimal("6776.5"));

ItemListT itemList = factory.createItemListT();
itemList.getItem().add(item);

ExpenseT expense = factory.createExpenseT();
expense.setUser(user);
expense.setItems(itemList);

Marshaller
JAXBContext context = JAXBContext.newInstance("generated");
JAXBElement<ExpenseT> element = factory.createExpenseReport(expense);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
StringWriter st = new StringWriter();
marshaller.marshal(element, st);

String xml = st.toString();
System.out.println(xml);

3. Unmarshaller the XML String 2 JAVA Object
JAXBContext jaxbContext = null;
ExpenseT expenseReport = null;
StringReader sr = null;
try {
sr = new StringReader(xml);
jaxbContext = JAXBContext.newInstance(ExpenseT.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
expenseReport = (ExpenseT) unmarshaller.unmarshal(sr);
System.out.println(expenseReport.getUser().getUserName());
} catch (JAXBException e) {
e.printStackTrace();
}

I got the fucking error message as follow.
error message:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"expenseReport"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631)

Solution:
Add this line to the ExpenseT.java object @XmlRootElement(name="expenseReport")

It works fine then. Yes, right now, I can convert JAVA 2 XML, XML 2 JAVA with the help of JAVA XJC annotation.


references:
http://www.xyzws.com/scdjws/studyguide/jaxb_samples2.0.html
http://www.javacodegeeks.com/2011/02/jaxb-generate-xml-xsd.html
http://hi.baidu.com/luohuazju/blog/item/2f3567243c5d2a238744f9c0.html
http://stackoverflow.com/questions/5203312/javax-xml-bind-unmarshalexception-unexpected-element-uri-localgroup
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值