- 浏览: 2552820 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
Update Site will come soon
2021-06-02 04:10 1679I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 356Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 409PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 721Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 295Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 295Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 241MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 294MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 325Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 312Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 332Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 283Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 326K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 362Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 444Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ...
相关推荐
总结来说,`JAVA XML转化BEEN`的核心是使用JAXB框架,通过注解或XSD生成Java Bean,并利用Unmarshaller和Marshaller进行XML与Java对象之间的转换。理解并熟练掌握这一技术,将有助于提高Java应用中处理XML数据的效率...
1. XML解析器生成Java对象: 当我们需要从XML文件中提取数据并创建相应的Java对象时,可以使用DOM(Document Object Model)、SAX(Simple API for XML)或StAX(Streaming API for XML)等解析器。DOM解析器一次性...
通过`xjc`工具,可以将XSD转换为Java源代码,这使得模型和XML结构更加清晰地分离。 JAXB的另一个重要特性是它支持XML绑定的定制,包括命名空间处理、类型转换、列表处理等。开发者可以通过实现`XmlAdapter`接口来自...
5. JAXB性能优化:可以通过缓存生成的Java对象、设置适当的marshaller/unmarshaller属性、分批处理大型XML文件等方式提高JAXB的性能。 6. 其他XML数据绑定框架:除了JAXB,还有许多其他的框架如Castor、 JiBX 和...
2. **生成绑定类**:使用`jaxb2-maven-plugin`或`xjc`命令行工具,根据Java类生成对应的XML绑定类。 3. **解析XML到Java对象**:使用`Unmarshaller`接口将XML文档解析为Java对象。 ```java JAXBContext ...
Java Architecture for XML Binding (JAXB) 是Java平台上的一个标准技术,它允许程序开发者将XML文档与Java对象之间进行绑定,实现XML数据的序列化和反序列化。JAXB是Java SE和Java EE环境中的一部分,提供了高效且...
如果不希望手动添加注解,可以使用JAXB的绑定工具(如`xjc`)从XML Schema (XSD)文件生成对应的Java类。这一步骤将创建带有正确注解的Java类,以便JAXB理解XML结构。 (3) **实例化Java对象** 创建Java对象并填充...
3. 解析XML:有了Java类,我们就可以使用JAXB提供的`Unmarshaller`接口将XML文档解析成Java对象。以下是一个简单的示例: ```java JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Unmarshaller ...
此外,JAXB 也提供了一些有用的工具,例如 XJC 工具,通过定义 schema 的方式可以实现 Java 对象与 XML 的绑定。 JAXB 是一个非常有用的技术,可以帮助我们实现 Java 对象与 XML 的相互转换,并提供了一些有用的...
3. **绑定XML**: 使用Unmarshaller将XML文件转换为Java对象,或者使用Marshaller将Java对象转换为XML。 4. **操作数据**: 在Java对象上进行操作,修改数据,然后使用Marshaller再次生成XML。 5. **保存或发送XML**...
例如,使用`Unmarshaller`对象将XML数据解码为Java对象,使用`Marshaller`对象将Java对象编码为XML。 总结来说,"xml文件转xsd文件"是为了建立XML文档的结构规范,方便数据验证并利用Java等编程语言进行数据处理。...
3. **创建Unmarshaller和Marshaller对象**:使用`javax.xml.bind.JAXBContext`类来创建Unmarshaller和Marshaller对象,这两个对象分别用于Unmarshalling和Marshalling。 4. **Unmarshalling**:使用Unmarshaller...
`Unmarshaller`和`Marshaller`接口分别用于XML到Java对象的反序列化和Java对象到XML的序列化操作。 2. **jaxb-impl.jar**: 它是JAXB的实现库,包含了对`jaxb-api.jar`中接口的具体实现。开发者通常不需要直接与这...
Java Architecture for XML Binding (JAXB) 是Java平台中用于处理XML和Java对象之间转换的一个标准技术。它允许我们将XML文档转换为Java对象,反之亦然,使得数据交换变得更加简单。在Java开发中,JAXB是处理XML数据...
1. **jaxb-api.jar**:这是JAXB2的主要API接口定义,包含了所有的注解和接口,如`@XmlRootElement`、`@XmlElement`等,以及用于转换的核心类,如`Unmarshaller`和`Marshaller`。这个jar文件提供了与XML绑定的基本...
2. **运行时绑定(Runtime Binding)**:在程序运行时,JAXB API提供了`Unmarshaller`和`Marshaller`接口,用于XML到Java对象的转换,以及Java对象到XML的转换。 **XMLSPY的特性** XMLSPY作为一款专业级的XML编辑...
- 反序列化是序列化的逆过程,使用`Unmarshaller`类,可以从XML字符串或文件中创建Java对象实例。 - JAXB会解析XML并创建相应的Java对象层次结构,保持与XML文档结构的一致性。 5. **JAXB与Maven/Gradle集成**: ...
例如,我们有一个XML文件,通过定义一个Java类与XML元素对应,然后使用`Unmarshaller.unmarshal()`方法,就可以将XML数据映射到对应的Java对象实例上。 接着,“对象转化为XML”。当我们需要将Java对象的数据以XML...