XJC(1)marshaller and unmarshaller JAVA XML

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


你可能感兴趣的:(marshal)