JAXB xml轉java對象/java轉xml,JAXB xml物件建構

來源:互聯網
上載者:User
    //xml轉換成bean    public static RequestXML parseToBean(String xmlstr)  {          RequestXML requestXml = null;          try {              JAXBContext jaxbContext = JAXBContext.newInstance(RequestXML.class);              Unmarshaller um = jaxbContext.createUnmarshaller();              requestXml = (RequestXML)um.unmarshal(new ByteArrayInputStream(xmlstr.getBytes()));          } catch (JAXBException e) {              e.getMessage();          }          return requestXml;      }    //bean 轉換成 xml    private static String parseToXml(Object javaBean) throws Exception{        JAXBContext context = JAXBContext.newInstance(xmlBean.getClass());        Marshaller m = context.createMarshaller();                StringWriter sw = new StringWriter();        m.marshal(javaBean,sw);                return sw.toString();    }

JAXB 結構:

xml example:

<?xml version="1.0" encoding="utf-8"?><bean id="10" code="xm1000x">    <name>x.m<name>        <levels>        <level key="1">none</level>        <level key="2">nothing</level>    </levels>        <extras>        <extra>            <type>1</type>            <content>xx.mm.xx</content>        </extra>        <extra>            <type>2</type>            <content>xx.mm.xx</content>        </extra>    </extras>    </bean>        

java bean:

import java.util.ArrayList;import java.util.List;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlElementWrapper;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="bean")public class Bean {private Integer id;private String code;private String name;private List<Level> levels = new ArrayList<Level>();private List<Extra> extras = new ArrayList<Extra>();public Bean(){}@XmlAttributepublic Integer getId() {return id;}@XmlAttributepublic String getCode() {return code;}@XmlElementpublic String getName() {return name;}@XmlElementWrapper(name="levels")@XmlElement(name="level")public List<Level> getLevels() {return levels;}@XmlElementWrapper(name="extras")@XmlElement(name="extra")public List<Extra> getExtras() {return extras;}public void setId(Integer id) {this.id = id;}public void setCode(String code) {this.code = code;}public void setName(String name) {this.name = name;}public void setLevels(List<Level> levels) {this.levels = levels;}public void setExtras(List<Extra> extras) {this.extras = extras;}}

   

import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlValue;@XmlRootElementpublic class Level {        private Integer Key;    private String value;        public Level(){}        @XmlAttribute(name="key")    public Integer getKey() {        return Key;    }    @XmlValue    public String getValue() {        return value;    }    public void setKey(Integer key) {        Key = key;    }    public void setValue(String value) {        this.value = value;    }        }
import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Extra {    private String type;    private String content;        public Extra(){}    @XmlElement    public String getType() {        return type;    }    @XmlElement    public String getContent() {        return content;    }    public void setType(String type) {        this.type = type;    }    public void setContent(String content) {        this.content = content;    }        }

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.