(Fusions) XmlParser.getXmlData(fusionFile, Fusions.class);

来源:互联网 发布:南京魔苹网络怎么样 编辑:程序博客网 时间:2024/05/24 06:02

-----------------------XmlParser.java-------------------------------------------------------

package com.yellowbook.presentation.analytics.utilty;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.yellowbook.presentation.analytics.demo.data.demoaccount.DemoAccounts;

public class XmlParser {

    /**
     * get the xml data by
     *
     * @param fileName
     *            - xml file name
     * @param c
     *            - class type
     * @return
     * @throws JAXBException
     */
    public static Object getXmlData(final String fileName, final Class c)
            throws Throwable {
        String package_path = c.getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance(package_path);
        Unmarshaller mar = jc.createUnmarshaller();

        Object o = (Object) mar.unmarshal(new File(fileName));

        return o;
    }

    /**
     * write xml data to file
     *
     * @param fileName
     *            - xml file name
     * @param c
     *            - class type
     * @throws JAXBException
     */
    public static void writeXmlData(final String fileName, final Object obj)
            throws JAXBException {
        String package_path = obj.getClass().getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance(package_path);
        Marshaller mar = jc.createMarshaller();
        File file = new File(fileName);
        mar.marshal(obj, file);
        
    }

}

---------------------------------ObjectFactory.java--------------------------------------------

package com.yellowbook.presentation.analytics.demo.data.fusion;

import javax.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public Fusions createFusions() {
        return new Fusions();
    }

    public Fusion createFusion() {
        return new Fusion();
    }
}
----------------------------------------------Fusions.java------------------------------
package com.yellowbook.presentation.analytics.demo.data.fusion;

import java.util.ArrayList;
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;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "fusion"
})
@XmlRootElement(name = "Fusions")
public class Fusions {
    @XmlElement(name = "Fusion")
    protected List<Fusion> fusion;

    public List<Fusion> getFusion() {
        if (fusion == null) {
            fusion = new ArrayList<Fusion>();
        }
        return fusion;
    }
}

-------------------------------Fusion.java-------------------------------------------------------------

package com.yellowbook.presentation.analytics.demo.data.fusion;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "date",
        "impressions",
        "costWithMargin",
        "calls",
        "clicks"
})
@XmlRootElement(name = "Fusion")
public class Fusion {

    @XmlElement(name = "date", required = true)
    protected String date;
    @XmlElement(name = "impressions", required = true)
    protected String impressions;
    @XmlElement(name = "clicks", required = true)
    protected String clicks;
    @XmlElement(name = "costWithMargin", required = true)
    protected String costWithMargin;
    @XmlElement(name = "calls", required = true)
    protected String calls;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getImpressions() {
        return impressions;
    }

    public void setImpressions(String impressions) {
        this.impressions = impressions;
    }

    public String getCostWithMargin() {
        return costWithMargin;
    }

    public void setCostWithMargin(String costWithMargin) {
        this.costWithMargin = costWithMargin;
    }

    public String getCalls() {
        return calls;
    }

    public void setCalls(String calls) {
        this.calls = calls;
    }

    public String getClicks() {
        return clicks;
    }

    public void setClicks(String clicks) {
        this.clicks = clicks;
    }
}

0 0