<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 100  文章 - 50  trackbacks - 0
    <2014年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    解析XML文件:xml 文件如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <trans>
    <tran>
    <requestId>111111</requestId>
    <orderNo>13141322</orderNo>
    <returnCode>000001</returnCode>
    <returnInfo>交易失敗</returnInfo>
    </tran>
    <tran>
    <requestId>2222222</requestId>
    <orderNo>13141323</orderNo>
    <returnCode>000000</returnCode>
    <returnInfo>交易成功</returnInfo>
    </tran>
    </trans>
    建立對應的JAVA bean類
    Trans.java類
    package com.jaxb.test;
    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;
    @XmlRootElement(name = "trans")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Trans{
    @XmlElement(name = "tran")
    private List<Tran> data = null;
    public List<Tran> getData() {
    return data;
    }
    public void setData(List<Tran> data) {
    this.data = data;
    }
    }
    Tran.java類
    package com.jaxb.test;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Tran {
    @XmlElement(name = "requestId")
    private Long requestId;
    @XmlElement(name = "orderNo")
    private Long orderNo;
    @XmlElement(name = "returnCode")
    private String returnCode;
    @XmlElement(name = "returnInfo")
    private String returnInfo;
    public Long getRequestId() {
    return requestId;
    }
    public void setRequestId(Long requestId) {
    this.requestId = requestId;
    }
    public Long getOrderNo() {
    return orderNo;
    }
    public void setOrderNo(Long orderNo) {
    this.orderNo = orderNo;
    }
    public String getReturnCode() {
    return returnCode;
    }
    public void setReturnCode(String returnCode) {
    this.returnCode = returnCode;
    }
    public String getReturnInfo() {
    return returnInfo;
    }
    public void setReturnInfo(String returnInfo) {
    this.returnInfo = returnInfo;
    }
    }
    建立測試類
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    public class TestJAXB {
    public static void main(String[] args) {
    try {
    //read xml file to Java object
    Trans trans = TestJAXB.readString(Trans.class,"input.xml");
    List<Tran> datas = trans.getDatas();
    for (Tran o : datas) {
    System.out.println(o.getReturnCode()+";"+o.getReturnInfo());
    }
    //write java object to xml
    Tran tran = new Tran();
          tran.setOrderNo(11111L);
          tran.setRequestId(22222L);
          tran.setReturnCode("0000");
          tran.setReturnInfo("success");
          Tran tran2 = new Tran();
          tran2.setOrderNo(33333L);
          tran2.setRequestId(44444L);
          tran2.setReturnCode("0001");
          tran2.setReturnInfo("failure");
          Trans trxs = new Trans();
          List<Tran> list = new ArrayList<Tran>(); 
          list.add(tran);
          list.add(tran2);
          trxs.setDatas(list);
    File file = new File("D:/output.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Trans.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(trxs, file);
    jaxbMarshaller.marshal(trxs, System.out);
    } catch (JAXBException e) {
    e.printStackTrace();
    }
    }
    @SuppressWarnings("unchecked")
    public static <T> T readString(Class<T> clazz, String context) throws JAXBException {
    try {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();
    return (T) u.unmarshal(new File(context));
    } catch (JAXBException e) {
    throw e;
    }
    }
    }
    posted on 2014-12-11 14:44 fly 閱讀(351) 評論(0)  編輯  收藏 所屬分類: J2EE
    主站蜘蛛池模板: 无码欧精品亚洲日韩一区夜夜嗨| 国产无限免费观看黄网站| 免费福利电影在线观看| 亚洲欧洲日产国码无码久久99 | 国产97视频人人做人人爱免费| 99久久国产精品免费一区二区| 国产男女爽爽爽爽爽免费视频| 亚洲第一区在线观看| 亚洲视频精品在线观看| 最近中文字幕无免费| 亚洲国产电影在线观看| 久久WWW免费人成—看片| 亚洲黄片手机免费观看| 两性色午夜免费视频| 国产亚洲精品免费| 青青草97国产精品免费观看| 67194国产精品免费观看| 亚洲欧洲精品成人久久曰影片 | 无码不卡亚洲成?人片| 国产国产人免费人成成免视频| 岛国av无码免费无禁网站| 亚洲国产成人无码AV在线影院| 中文字幕亚洲免费无线观看日本 | 日韩视频免费在线| 免费的黄色的网站| 亚洲国产AV无码专区亚洲AV | 亚洲AV无码一区二区三区在线观看 | 精品免费久久久久国产一区| 日本红怡院亚洲红怡院最新| 0588影视手机免费看片| 美女扒开尿口给男人爽免费视频| 性做久久久久久免费观看| 久久亚洲AV午夜福利精品一区 | 亚洲精品国产精品乱码不99| 99re这里有免费视频精品| 亚洲精品无码Av人在线观看国产| 日本在线观看免费高清| 免费观看午夜在线欧差毛片| a视频在线免费观看| 亚洲性色精品一区二区在线| 国产AⅤ无码专区亚洲AV|