XML与JAVABean的相互转换

一、XML生成JAVABean

1. 将以下xml报文转为JavaBen实例:

xml version="1.0" encoding="UTF-8"?>

<ROOT>

<CODE>入库预约单号1CODE>

<RELATEDCODE>采购订单1RELATEDCODE>

<STATUS>状态STATUS>

<SUPPLIERCODE>供应商代码SUPPLIERCODE>

<SUPPLIERNAME>供应商名称SUPPLIERNAME>

<LOCATIONCODE>库存地点LOCATIONCODE>

<REVIEWTIME>PAC评审时间REVIEWTIME>

<TOLOCATION>入库仓库 TOLOCATION>

<STORAGEDATE>计划入库 时间STORAGEDATE>

<CREATEDNAME>创建人CREATEDNAME>

<CREATETIME>创建时间CREATETIME>

<UPDATENAME>修改人UPDATENAME>

<UPDATETIME>修改时间UPDATETIME>

<ITEM>

<LINENUMBER>行项目号LINENUMBER>

<ITEMCODE>物料编码ITEMCODE>

<ITEMNAME>物料名称ITEMNAME>

<QUANTITY>数量QUANTITY>

<UNIT>单位UNIT>

<WEIGHT>重量(KG)WEIGHT>

<VOLUME>体积(MM3)VOLUME>

ITEM>

<ITEM>

<LINENUMBER>行项目号LINENUMBER>

<ITEMCODE>物料编码ITEMCODE>

<ITEMNAME>物料名称ITEMNAME>

<QUANTITY>数量QUANTITY>

<UNIT>单位UNIT>

<WEIGHT>重量(KG)WEIGHT>

<VOLUME>体积(MM3)VOLUME>

ITEM>

ROOT>

2.  如图可见,ROOT为根节点,相应的是子节点,子节点ITEM作为根节点还有子节点,相应的JAVABean实体类如下:

@XmlRootElement(name="ROOT")  

public class ROOT {

//入库预约单号

    private String CODE;

    //采购订单

    private String RELATEDCODE;

    //状态

    private String STATUS;

    //供应商代码

    private String SUPPLIERCODE;

    //供应商名称

    private String SUPPLIERNAME;

    //库存地点

    private String LOCATIONCODE;

    //PAC评审时间

    private String REVIEWTIME;

    //入库仓库编码

    private String TOLOCATION;

    //入库时间

    private String STORAGEDATE;

    //创建人

    private String CREATEDNAME;

    //创建日期

    private String CREATETIME;

    //最后修改人

    private String UPDATENAME;

    //最后修改日期

    private String UPDATETIME;

 

    private List ITEM;

 

public ROOT() {

super();

}

 

public ROOT(String cODE, String rELATEDCODE, String sTATUS, String sUPPLIERCODE, String sUPPLIERNAME,

String lOCATIONCODE, String rEVIEWTIME, String tOLOCATION, String sTORAGEDATE, String cREATEDNAME,

String cREATETIME, String uPDATENAME, String uPDATETIME, List iTEM) {

super();

CODE = cODE;

RELATEDCODE = rELATEDCODE;

STATUS = sTATUS;

SUPPLIERCODE = sUPPLIERCODE;

SUPPLIERNAME = sUPPLIERNAME;

LOCATIONCODE = lOCATIONCODE;

REVIEWTIME = rEVIEWTIME;

TOLOCATION = tOLOCATION;

STORAGEDATE = sTORAGEDATE;

CREATEDNAME = cREATEDNAME;

CREATETIME = cREATETIME;

UPDATENAME = uPDATENAME;

UPDATETIME = uPDATETIME;

ITEM = iTEM;

}

        Get...

        Set...

3. 在实体类上增加注解@XmlRootElement(name="ROOT")解析时根节点会根据属性去生成相应的子节点,然后ITEM是多行需要定义为List,然后再定义单独的实体类Bean。如下

注意:实体类必须要有无参构造方法,不然在解析的时候会报错。

import javax.xml.bind.annotation.XmlRootElement;

 

@XmlRootElement  

public class ITEM {

 private String LINENUMBER;  

 private String ITEMCODE;

 private String ITEMNAME;

 private String QUANTITY;

 private String UNIT;

 private String WEIGHT;

 private String VOLUME;

 

 public ITEM() {

super();

 }

 

 public ITEM(String lINENUMBER, String iTEMCODE, String iTEMNAME, String qUANTITY, String uNIT, String wEIGHT,String vOLUME) {

super();

LINENUMBER = lINENUMBER;

ITEMCODE = iTEMCODE;

ITEMNAME = iTEMNAME;

QUANTITY = qUANTITY;

UNIT = uNIT;

WEIGHT = wEIGHT;

VOLUME = vOLUME;

 }

 

 

二、实体类转换为XML

ROOT root = new ROOT

(CODE,RELATEDCODE,STATUS,SUPPLIERCODE,SUPPLIERNAME,LOCATIONCODE,REVIEWTIME,TOLOCATION,STORAGEDATE,CREATEDNAME,CREATETIME,UPDATENAME,UPDATETIME, itemList);

//生成xml

         JAXBContext jc;

         StringWriter writer = new StringWriter();

         jc = JAXBContext.newInstance(ROOT.class);

          Marshaller ms = jc.createMarshaller();

         ms.marshal(root,System.out);  

         ms.marshal(root,writer);

         String xml = writer.getBuffer().toString();

 

三、dom4j解析xml

xml version="1.0" encoding="UTF-8"?>

<ROOT>

<STATUS>1STATUS>

<MESSAGE>MESSAGE>

ROOT>

解析:

            String result = port.warehousingOrder(xml);

            System.out.println("出参:"+result);

            //dom4j解析返回结果

            Document doc = DocumentHelper.parseText(result); // 将字符串转为XML

            Element rootElt = doc.getRootElement(); // 获取根节点

            Element element_status = rootElt.element("STATUS");

            String status = element_status.getStringValue();

            Element element_message = rootElt.element("MESSAGE");

            String message = element_message.getStringValue();

你可能感兴趣的:(JAVA,WEB)