- 浏览: 2552494 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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
JAXB annotations for Parsing XML
The document of annotations is :
http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp
my test XML file content is:
<?xml version="1.0" encoding="UTF-8"?>
<widget vendorId="131" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<component sequence="0">
<name><![CDATA[Promo1]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo1.png]]></imageURL>
</images>
<type>sku</type>
<content><![CDATA[1000080]]></content>
</component>
<component sequence="1">
<name><![CDATA[Promo6]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>category</type>
<!--<content>shop|home|decor|</content>-->
<content><![CDATA[633329]]><!-- this is the menuId -->
</content>
</component>
<component sequence="2">
<name><![CDATA[Promo7]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>static</type>
<content>This is a Test.</content>
</component>
<component sequence="3">
<name><![CDATA[Promo8]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>url</type>
<content>http://www.google.com</content>
</component>
<component sequence="4">
<name><![CDATA[Promo9]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>search</type>
<content><![CDATA[1000080]]></content>
</component>
</widget>
According to the XML, my java objects are:
BannerWidget
BannerComponent
BannerImageCollection
BannerImage
BannerWidget.java is :
package com.xxx.bean.banner;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "widget")
public class BannerWidget
{
@XmlAttribute(name = "vendorId")
private String vendorId;
@XmlElement(name = "component")
private List<BannerComponent> components;
... snip getter and setter
}
@XmlRootElement is for root xml field
@XmlAttribute is for the attribute in the field, eg sequence in <component sequence="0">
@XmlElement is for the value part of the field,
eg <name> in <component sequence="0"><name><![CDATA[Promo1]]></name></component>
BannerComponent.java:
package com.xxxx.bean.banner;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class BannerComponent
{
@XmlAttribute(name = "sequence")
private int sequence;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "images")
private BannerImageCollection images;
@XmlElement(name = "type")
private String type;
@XmlElement(name = "content")
private String content;
...snip getter and setter
}
BannerImageCollection.java:
package com.xxxx.bean.banner;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "images")
public class BannerImageCollection
{
@XmlElement(name = "imageURL")
private List<BannerImage> images;
...snip getter and setter...
}
BannerImage.java:
package com.xxxx.bean.banner;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
public class BannerImage
{
@XmlAttribute(name = "size")
private String size;
@XmlValue
private String imageURL;
...snip getter and setter...
}
@XmlValue is for all the part of property,
eg url part in <imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
My testcase class XMLUtil.java is :
package com.xxxx.utils;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import com.xxxx.bean.banner.BannerWidget;
public class XmlUtil
{
/**
* Comment for main
*
* @param args
* Created by hua.luo.
*/
public static void main(String[] args)
{
JAXBContext jaxbContext = null;
BannerWidget bannerWidget = null;
FileReader fileReader = null;
try
{
fileReader = new FileReader("d://test//promotion.xml");
jaxbContext = JAXBContext.newInstance(BannerWidget.class);
bannerWidget = (BannerWidget) jaxbContext.createUnmarshaller().unmarshal(fileReader);
System.out.println(bannerWidget);
}
catch (JAXBException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
The document of annotations is :
http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp
my test XML file content is:
<?xml version="1.0" encoding="UTF-8"?>
<widget vendorId="131" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<component sequence="0">
<name><![CDATA[Promo1]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo1.png]]></imageURL>
</images>
<type>sku</type>
<content><![CDATA[1000080]]></content>
</component>
<component sequence="1">
<name><![CDATA[Promo6]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>category</type>
<!--<content>shop|home|decor|</content>-->
<content><![CDATA[633329]]><!-- this is the menuId -->
</content>
</component>
<component sequence="2">
<name><![CDATA[Promo7]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>static</type>
<content>This is a Test.</content>
</component>
<component sequence="3">
<name><![CDATA[Promo8]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>url</type>
<content>http://www.google.com</content>
</component>
<component sequence="4">
<name><![CDATA[Promo9]]></name>
<images>
<imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
</images>
<type>search</type>
<content><![CDATA[1000080]]></content>
</component>
</widget>
According to the XML, my java objects are:
BannerWidget
BannerComponent
BannerImageCollection
BannerImage
BannerWidget.java is :
package com.xxx.bean.banner;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "widget")
public class BannerWidget
{
@XmlAttribute(name = "vendorId")
private String vendorId;
@XmlElement(name = "component")
private List<BannerComponent> components;
... snip getter and setter
}
@XmlRootElement is for root xml field
@XmlAttribute is for the attribute in the field, eg sequence in <component sequence="0">
@XmlElement is for the value part of the field,
eg <name> in <component sequence="0"><name><![CDATA[Promo1]]></name></component>
BannerComponent.java:
package com.xxxx.bean.banner;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class BannerComponent
{
@XmlAttribute(name = "sequence")
private int sequence;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "images")
private BannerImageCollection images;
@XmlElement(name = "type")
private String type;
@XmlElement(name = "content")
private String content;
...snip getter and setter
}
BannerImageCollection.java:
package com.xxxx.bean.banner;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "images")
public class BannerImageCollection
{
@XmlElement(name = "imageURL")
private List<BannerImage> images;
...snip getter and setter...
}
BannerImage.java:
package com.xxxx.bean.banner;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
public class BannerImage
{
@XmlAttribute(name = "size")
private String size;
@XmlValue
private String imageURL;
...snip getter and setter...
}
@XmlValue is for all the part of property,
eg url part in <imageURL size="large"><![CDATA[http://www.sanrio.com/deliverables/iphone/promo/promo5.png]]></imageURL>
My testcase class XMLUtil.java is :
package com.xxxx.utils;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import com.xxxx.bean.banner.BannerWidget;
public class XmlUtil
{
/**
* Comment for main
*
* @param args
* Created by hua.luo.
*/
public static void main(String[] args)
{
JAXBContext jaxbContext = null;
BannerWidget bannerWidget = null;
FileReader fileReader = null;
try
{
fileReader = new FileReader("d://test//promotion.xml");
jaxbContext = JAXBContext.newInstance(BannerWidget.class);
bannerWidget = (BannerWidget) jaxbContext.createUnmarshaller().unmarshal(fileReader);
System.out.println(bannerWidget);
}
catch (JAXBException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
发表评论
-
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 405PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 720Elasticsearch 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 ...
相关推荐
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
JAXB(Java Architecture for XML Binding)是Java平台标准的一部分,它提供了一种将Java对象转换为XML文档以及从XML数据恢复Java对象的机制。本篇将详细讲解如何利用JAXB根据XSD(XML Schema Definition)文件生成...
jackson-module-jaxb-annotations-2.8.8.jar
在Java开发中,JAXB(Java Architecture for XML Binding)是一个标准的API,用于将XML文档与Java对象之间进行互相转换。这个技术对于处理XML数据,尤其是解析和生成XML文档非常有用。当我们面临XML文档中存在嵌套子...
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...
而JAXB(Java Architecture for XML Binding)作为一项强大的技术,提供了将XML文档与Java对象模型进行双向转换的能力。本文将详细介绍如何利用JAXB来从一个XML模式(Schema)生成Java类,并最终构建出符合该模式的...
Java Architecture for XML Binding (JAXB) 是Java平台上的一个标准技术,它允许程序开发者将XML文档与Java对象之间进行绑定,实现XML数据的序列化和反序列化。JAXB是Java SE和Java EE环境中的一部分,提供了高效且...
Java Architecture for XML Binding (JAXB) 是Java平台中用于处理XML和Java对象之间转换的一个标准API。它使得在Java应用程序中使用XML数据变得更加方便,无需手动编写大量的转换代码。本教程将详细介绍JAXB如何实现...
JAXB (Java Architecture for XML Binding) 是 Java 中用于对象到XML以及XML到对象转换的API。它使得开发者能够轻松地将Java对象模型映射到XML文档,并反之亦然。在给定的例子中,我们将深入理解如何使用JAXB注解来...
Android支持JAXB(Java Architecture for XML Binding) JAXB(Java Architecture for XML Binding)是Java领域中的一项标准技术,能够根据XML Schema生成Java类,并将XML实例文档反向生成Java对象树。JAXB提供了将...
而JAXB(Java Architecture for XML Binding)是Java提供的一种标准API,它允许我们轻松地在Java对象(javabean)和XML文档之间进行转换。通过JAXB,开发者可以避免手动编写XML解析和序列化代码,极大地提高了开发...
Java Architecture for XML Binding (JAXB) 是Java平台上的一个标准技术,用于在Java对象和XML文档之间进行数据绑定。它允许开发人员通过简单的注解(annotations)将Java类与XML Schema映射,从而实现XML文档的序列...
在Java中,有多种方法可以解析XML,其中StAX(Streaming API for XML)和JAXB(Java Architecture for XML Binding)是两种常用的技术。本篇文章将详细探讨如何结合StAX和JAXB进行高效的XML解析。 StAX是一种事件...
Java Architecture for XML Binding (JAXB) 是Java平台中用于处理XML和Java对象之间转换的一个标准技术。它允许我们将XML文档转换为Java对象,反之亦然,使得数据交换变得更加简单。在Java开发中,JAXB是处理XML数据...
Java Architecture for XML Binding (JAXB) 是一个Java标准,它提供了在Java对象和XML文档之间进行自动转换的能力。这项技术极大地简化了XML数据处理,使得开发者可以方便地将Java对象序列化为XML,或者从XML数据中...
Java Architecture for XML Binding (JAXB) 是Java平台上的一个标准技术,它允许在Java对象(如JavaBeans)和XML文档之间进行互相转换。这个过程被称为对象绑定,它简化了XML数据的处理,使得开发人员无需手动编写...
当我们需要处理XML文件,比如从XML中提取数据时,JAXB(Java Architecture for XML Binding)是一个强大的工具。本教程将详细解释如何在Idea中利用JAXB来读取XML文件中的数据。 JAXB是Java标准API,它提供了将Java...
JAXB (Java Architecture for XML Binding) 是一种基于 Java 的 XML 绑定技术,主要功能是根据 DTD 或者 XML Schema 将 XML Element 转化为 Java 类。JAXB 将 XML 文本流转化为 Java Object,把 XML Processing 转化...