JAXB annotations for Parsing XML

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();
        }

    }

}

你可能感兴趣的:(java,xml,bean,Google)