2013-03-14 5 views
9

मैं जेएक्सबी के लिए नया हूं और एक्सएमएल से जावा क्लास इंस्टेंस में रूपांतरण के साथ परेशानी है।JAXB @XmlAttribute @XmlValue वास्तविक उदाहरण

मैं निम्न XML है:

<?xml version="1.0"?> 
<response> 
    <category>client</category> 
    <action>Greeting</action> 
    <code>1000</code> 
    <msg>Your Connection with API Server is Successful</msg> 
    <resData> 
     <data name="svDate">2009-02-16 06:22:21</data> 
    </resData> 
</response> 

और मैं निम्नलिखित जावा कोड का विकास:

/** 
* Copyright 2013. ABN Software. All Rights reserved.<br> 
* Author ...... Andre<br> 
* Created ..... 14.03.2013<br> 
* <br> 
*/ 
package net.regmaster.onlinenic.model; 

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementRef; 
import javax.xml.bind.annotation.XmlRootElement; 

import net.regmaster.onlinenic.enumtype.OnicEnumAction; 
import net.regmaster.onlinenic.enumtype.OnicEnumCategory; 
import net.regmaster.onlinenic.model.resdata.GreetingResData; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

/** 
* @author annik 
* 
*/ 
@XmlRootElement(name = "response") 
// @XmlType(propOrder = { "category", "action", "code", "message"}) 
public class OnicGreeting 
{ 
    private OnicEnumCategory category; 
    private OnicEnumAction action; 
    private Integer   code; 
    private String   message; 
    private GreetingResData resData; 
    // 
    private Logger   LOG = LoggerFactory.getLogger(getClass()); 

    /** 
    * Getter. 
    * 
    * @return the category 
    */ 
    public OnicEnumCategory getCategory() { 
     return category; 
    } 

    /** 
    * Setter. 
    * 
    * @param category 
    *   the category to set 
    */ 
    public void setCategoryEnum(OnicEnumCategory category) { 
     this.category = category; 
    } 

    @XmlElement 
    public void setCategory(String category) { 
     try { 
      this.category = OnicEnumCategory.getEnum(category); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      LOG.error(e.getMessage()); 
     } 
    } 

    /** 
    * Getter. 
    * 
    * @return the action 
    */ 
    public OnicEnumAction getAction() { 
     return action; 
    } 

    /** 
    * Setter. 
    * 
    * @param action 
    *   the action to set 
    */ 
    public void setActionEnum(OnicEnumAction action) { 
     this.action = action; 
    } 
    @XmlElement 
    public void setAction(String action) { 
     try { 
      this.action = OnicEnumAction.getEnum(action); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      LOG.error(e.getMessage()); 
     } 
    } 

    /** 
    * Getter. 
    * 
    * @return the code 
    */ 
    @XmlElement 
    public Integer getCode() { 
     return code; 
    } 

    /** 
    * Setter. 
    * 
    * @param code 
    *   the code to set 
    */ 
    public void setCode(Integer code) { 
     this.code = code; 
    } 

    /** 
    * Getter. 
    * 
    * @return the message 
    */ 
    @XmlElement(name = "msg") 
    public String getMessage() { 
     return message; 
    } 

    /** 
    * Setter. 
    * 
    * @param message 
    *   the message to set 
    */ 
    public void setMessage(String message) { 
     this.message = message; 
    } 

    /** 
    * Getter. 
    * 
    * @return the resData 
    */ 
    @XmlElementRef 
    public GreetingResData getResData() { 
     return resData; 
    } 

    /** 
    * Setter. 
    * 
    * @param resData 
    *   the resData to set 
    */ 
    public void setResData(GreetingResData resData) { 
     this.resData = resData; 
    } 

    @Override 
    public String toString() { 
     return "category=" + category + ", action=" + action + ", code=" + code + ", msg=" + message 
       + ", resData:" + resData.toString(); 
    } 
} 

और

/** 
* Copyright 2013. ABN Software. All Rights reserved.<br> 
* Author ...... Andre<br> 
* Created ..... 14.03.2013<br> 
* <br> 
*/ 
package net.regmaster.onlinenic.model.resdata; 

import java.util.Calendar; 

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 

/** 
* @author annik 
* 
*/ 
@XmlRootElement(name="resData") 
public class GreetingResData extends AbstractResData 
{ 
    String svDate; 

    /** 
    * Constructor. 
    * 
    */ 
    public GreetingResData() { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * Getter. 
    * 
    * @return the svDate 
    */ 
    @XmlAttribute 
    public String getSvDate() { 
     return svDate; 
    } 

    /** 
    * Setter. 
    * 
    * @param svDate 
    *   the svDate to set 
    */ 
    public void setSvDate(String svDate) { 
     this.svDate = svDate; 
    } 
} 

ये कोड उदाहरण चलाने लेकिन डेटा गलत है:

http://i.stack.imgur.com/qCCIM.png

कृपया मेरी मदद करें।

इसके अलावा मुझे समझ नहीं आता मामले में मैं होगा कई अलग अलग

<data ...>..</data> 

मैं क्या कर सकता है आसान है और बस?

<resData> 
<data name="crDate">2004-12-17</data> 
<data name="exDate">2009-01-02</data> 
<data name="password">7fe11fd9d97ee40bdf57e561427c0a6</data> 
<data name="dns">dns1.onlinenic.net</data> 
<data name="dns">dns2.onlinenic.net</data> 
<data name="r_name">123456</data> 
<data name="r_org">123456</data> 
<data name="r_country">BJ</data> 
<data name="r_province">mokcup</data> 
<data name="r_city">123456</data> 
<data name="r_street">123456</data> 
<data name="r_postalcode">123456</data> 
<data name="r_voice">+86.5925391800</data> 
<data name="r_fax">+86.5925391800</data> 
<data name="r_email">[email protected]</data> 
.... 
+0

निम्न उदाहरण में मदद कर सकते हैं: https://github.com/bdoughan/blog20110322 –

उत्तर

9

धन्यवाद ब्लेज Doughan:
मैं इस मामले मतलब है। लेकिन 10 से अधिक विषयों को खोदने के बाद मैंने फैसला किया कि मुझे विपरीत तरीके से शुरू करना है।

मैंने नया परीक्षण बनाया जो मेरा डेटा (ऑब्जेक्ट) मार्शलिंग करता है। असल में, मैंने टीडीडी (टेस्ट ड्राइव डेवलपमेंट) का इस्तेमाल किया जिस तरह से मुझे लगता है। तो, मैंने टेस्ट डेटा के साथ अपने ऑब्जेक्ट्स को भर दिया और मार्शलिंग (डेटा से एक्सएमएल बनाया) लगाया और देखा कि मुझे मिल गया। डेटा गलत था। मैं अन्य विषय के लिए भी देखा (यह एक Java/JAXB: Unmarshall Xml to specific subclass based on an attribute thanx) और अपने डेटा संरचना को सही

ईद

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<response> 
    <code>1000</code> 
    <message>Big message</message> 
    <resData> 
     <data name="svDate">2013.03.14</data> 
    </resData> 
</response> 

प्राप्त करना चाहते अब मेरी डेटा कर रहे हैं याद रखें:

package net.regmaster.onlinenic.model.response.resdata; 

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.XmlValue; 

import org.eclipse.persistence.oxm.annotations.XmlCustomizer; 

/** 
* @author annik 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="data") 
//@XmlCustomizer(ResDataCustomiser.class) 
public class XmlData 
{ 
    @XmlAttribute(name="name") 
    private String name; 

    @XmlValue 
    private String value; 

    /** Getter. 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** Setter. 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** Getter. 
    * @return the value 
    */ 
    public String getValue() { 
     return value; 
    } 

    /** Setter. 
    * @param value the value to set 
    */ 
    public void setValue(String value) { 
     this.value = value; 
    } 
} 

और:

package net.regmaster.onlinenic.model.response.resdata; 

import java.util.List; 

import javax.xml.bind.annotation.XmlRootElement; 

/** 
* @author annik 
* 
*/ 
@XmlRootElement 
public class ResData 
{ 
    private List<XmlData> data; 

    /** 
    * Getter. 
    * 
    * @return the data 
    */ 
    public List<XmlData> getData() { 
     return data; 
    } 

    /** 
    * Setter. 
    * 
    * @param data 
    *   the data to set 
    */ 
    public void setData(List<XmlData> data) { 
     this.data = data; 
    } 
} 

और:

package net.regmaster.onlinenic.model.response; 

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

import net.regmaster.onlinenic.enumtype.OnicEnumAction; 
import net.regmaster.onlinenic.enumtype.OnicEnumCategory; 
import net.regmaster.onlinenic.model.response.resdata.ResData; 
import net.regmaster.onlinenic.model.response.resdata.XmlData; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

/** 
* @author annik 
* 
*/ 
@XmlRootElement(name = "response") 
//@XmlType(propOrder = { "category", "action", "code", "message"}) 
public class OnicGreetingResponse 
{ 
    private OnicEnumCategory category; 
    private OnicEnumAction action; 
    private Integer   code; 
    private String   message; 

// private GreetingResData resData; 
    private ResData resData; 

    // 
    @XmlTransient 
    private Logger   LOG = LoggerFactory.getLogger(getClass()); 

    /** 
    * Getter. 
    * 
    * @return the category 
    */ 
    public OnicEnumCategory getCategory() { 
     return category; 
    } 

    /** 
    * Setter. 
    * 
    * @param category 
    *   the category to set 
    */ 
    public void setCategoryEnum(OnicEnumCategory category) { 
     this.category = category; 
    } 

    @XmlElement 
    public void setCategory(String category) { 
     try { 
      this.category = OnicEnumCategory.getEnum(category); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      LOG.error(e.getMessage()); 
     } 
    } 

    /** 
    * Getter. 
    * 
    * @return the action 
    */ 
    public OnicEnumAction getAction() { 
     return action; 
    } 

    /** 
    * Setter. 
    * 
    * @param action 
    *   the action to set 
    */ 
    public void setActionEnum(OnicEnumAction action) { 
     this.action = action; 
    } 
    @XmlElement 
    public void setAction(String action) { 
     try { 
      this.action = OnicEnumAction.getEnum(action); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      LOG.error(e.getMessage()); 
     } 
    } 

    /** 
    * Getter. 
    * 
    * @return the code 
    */ 
    @XmlElement 
    public Integer getCode() { 
     return code; 
    } 

    /** 
    * Setter. 
    * 
    * @param code 
    *   the code to set 
    */ 
    public void setCode(Integer code) { 
     this.code = code; 
    } 

    /** 
    * Getter. 
    * 
    * @return the message 
    */ 
    @XmlElements(value={@XmlElement}) 
    public String getMessage() { 
     return message; 
    } 

    /** 
    * Setter. 
    * 
    * @param message 
    *   the message to set 
    */ 
    public void setMessage(String message) { 
     this.message = message; 
    } 

    /** Getter. 
    * @return the resData 
    */ 
    public ResData getResData() { 
     return resData; 
    } 

    /** Setter. 
    * @param resData the resData to set 
    */ 
    @XmlElement 
    public void setResData(ResData resData) { 
     this.resData = resData; 
    } 

    @Override 
    public String toString() { 
     return "category=" + category + ", action=" + action + ", code=" + code + ", msg=" + message 
       + ", resData:" + resData.toString(); 
    } 
} 

और vu-Alja: मुझे लगता है कि मिल गया है!

और जैसा कि आप देख सकते हैं नीचे यह एक और तरीका है करने के लिए काम करता है: http://i.stack.imgur.com/35nzb.png

संबंधित मुद्दे