首页 新闻 会员 周边 捐助

XML转换BEAN实例后未提示错误结果再转XML显示为空值, 问题出在哪里?

0
悬赏园豆:10 [待解决问题]

(1)RequestEntity.java:
package spdb.xml2bean;

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

@XmlRootElement(name = "Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"header",
"body"
})
//@XmlRootElement(name = "soapenv:Envelope")
public class RequestEntity {

@XmlAttribute(name="xmlns:soapenv")
protected String soapenv="http://schemas.xmlsoap.org/soap/envelope/";
@XmlAttribute(name="xmlns:eapp")
protected String eapp="http://wx.gmw9.com";

@XmlAttribute(name="xmlns:bus")
protected String bus=null;



@XmlElement(required = true,name="soapenv:Header")
protected spdb.xml2bean.RequestHeader header;

@XmlElement(required = true,name="soapenv:Body")
protected spdb.xml2bean.RequestBody body;


//get set方法省略

public  spdb.xml2bean.RequestHeader getHeader() {
    return  this.header;
}

public void setHeader(spdb.xml2bean.RequestHeader  header) {
    this.header = header;
}



public  spdb.xml2bean.RequestBody getBody() {
    return  this.body;
}

public void setBody(spdb.xml2bean.RequestBody  body) {
    this.body = body;
}

}

(2)
RequestHeader.java:
package spdb.xml2bean;

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 = "soapenv:Header")
public class RequestHeader
{

}

(3)
RequestBody.java

package spdb.xml2bean;

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 = "soapenv:Body")
public class RequestBody {

@XmlElement(required = true,name="eapp:aaaaaa")
public BodyContent  aaaaaa;    	
	


public  BodyContent  getAaaaaa() {
    return  this.aaaaaa;
}

public void setAaaaaa(BodyContent aaaaaa) {
    this.aaaaaa = aaaaaa;
}

}

(4)
JaxbXmlUtil.java

package spdb.xml2bean;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**

  • Jaxb工具类 xml和java类相互转换

  • @author sunjianbo

  • @date 2017年4月17日
    */
    public class JaxbXmlUtil {

    public static final String DEFAULT_ENCODING = "UTF-8";

    /**

    • pojo转换成xml 默认编码UTF-8
    • @param obj 待转化的对象
    • @return xml格式字符串
    • @throws Exception JAXBException
      */
      public static String convertToXml(Object obj) throws Exception {
      return convertToXml(obj, DEFAULT_ENCODING);
      }

    /**

    • pojo转换成xml

    • @param obj 待转化的对象

    • @param encoding 编码

    • @return xml格式字符串

    • @throws Exception JAXBException
      */
      public static String convertToXml(Object obj, String encoding) throws Exception {
      String result = null;

      JAXBContext context = JAXBContext.newInstance(obj.getClass());
      Marshaller marshaller = context.createMarshaller();
      // 指定是否使用换行和缩排对已编组 XML 数据进行格式化的属性名称。
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

      StringWriter writer = new StringWriter();
      marshaller.marshal(obj, writer);
      result = writer.toString();

      return result;
      }

    /**

    • xml转换成JavaBean
    • @param xml xml格式字符串
    • @param t 待转化的对象
    • @return 转化后的对象
    • @throws Exception JAXBException
      */
      @SuppressWarnings("unchecked")
      public static <T> T convertToJavaBean(String xml, Class<T> t)
      {

    T obj = null;
    try
    {

     JAXBContext context = JAXBContext.newInstance(t);
     Unmarshaller unmarshaller = context.createUnmarshaller();
     obj = (T) unmarshaller.unmarshal(new StringReader(xml));
     return obj;
    }
    catch (Exception e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return obj;
    

    }
    @SuppressWarnings("unchecked")
    public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;
    try {
    JAXBContext context = JAXBContext.newInstance(c);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
    e.printStackTrace();
    }

     return t;
    

    }

}

(6)

BodyContent.java

package spdb.xml2bean;

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 = {
"compNo",
"agentCode",
"eAppID"

})
@XmlRootElement(name = "eapp:aaaaaa")
public class BodyContent {

      @XmlElement(required = true,name="eapp:compNo")
    protected String compNo;
    @XmlElement(required = true,name="eapp:agentCode")
    protected String agentCode;
    @XmlElement(required = true,name="eapp:eAppId")
    protected String eAppID;              
    //get set方法省略
    
    public  String  getCompNo() {
        return  this.compNo;
    }
    
    public void setCompNo(String compNo) {
        this.compNo = compNo;
    }
   
    public  String  getAgentCode() {
        return  this.agentCode;
    }
    
    public void setAgentCode(String agentCode) {
        this.agentCode = agentCode;
    }
   
    public  String  getEAppID() {
        return  this.eAppID;
    }
    
    public void setEAppID(String eAppID) {
        this.eAppID = eAppID;
    }
   
    public String toString()
    {
    
        String result = "";
         result += compNo +agentCode +eAppID;
         System.out.println(result);
        return result;
    
}

}

(7)

Test.java

package spdb.xml2bean;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Test {

public static void main(String[] args) throws Exception {
    objectToXml();
    
    xmlToObjetct();
}    
public static  void objectToXml() throws Exception{
	
	RequestEntity myRequestEntity = new RequestEntity();
	BodyContent  myBodyContent = new BodyContent();
	myBodyContent.setAgentCode("testa1");
	myBodyContent.setCompNo("testa2");
	myBodyContent.setEAppID("testa3");
	RequestBody myRequestBody = new RequestBody();
	RequestHeader  myRequestHeader = new RequestHeader();
	
	 myRequestBody.setAaaaaa(myBodyContent);
	 myRequestEntity.setBody(myRequestBody);
	 
	 myRequestEntity.setHeader(myRequestHeader);
    String xml = JaxbXmlUtil.convertToXml(myRequestEntity);
    System.out.println(xml);
}  
public static void xmlToObjetct()throws Exception{
   
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + 
    		"<Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eapp=\"http://wx.gmw9.com\">" + 
    		"    <soapenv:Header/>" + 
    		"    <soapenv:Body>" + 
    		"        <eapp:aaaaaa>" + 
    		"            <eapp:compNo>testa1</eapp:compNo>" + 
    		"            <eapp:agentCode>testa2</eapp:agentCode>" + 
    		"            <eapp:eAppId>testa3</eapp:eAppId>" + 
    		"        </eapp:aaaaaa>" + 
    		"    </soapenv:Body>" + 
    		"</Envelope>";
    
   
    RequestEntity myRequestEntity= JaxbXmlUtil.convertToJavaBean(xml, RequestEntity.class);
    
    
    RequestEntity   re = JaxbXmlUtil.convertToJavaBean(xml, RequestEntity.class);
    String xmlout = JaxbXmlUtil.convertToXml(re);
    System.out.println(xmlout);
   
}

}

(8) console output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eapp="http://wx.gmw9.com">
soapenv:Header/
soapenv:Body
eapp:aaaaaa
eapp:compNotesta2</eapp:compNo>
eapp:agentCodetesta1</eapp:agentCode>
eapp:eAppIdtesta3</eapp:eAppId>
</eapp:aaaaaa>
</soapenv:Body>
</Envelope>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eapp="http://wx.gmw9.com"/>

atom_lxh的主页 atom_lxh | 初学一级 | 园豆:192
提问于:2020-09-06 20:15

建议改进一下排版,支持 markdown 语法

dudu 4年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册