2012-10-25 20 views
7

मान लीजिए मैंXstream शून्य मान को क्रमानुसार

class Student 
{ 
String name; 
int age; 
String teacher; 
} 
फिर

:

public class App1 
{ 
    public static void main(String[] args) 
    { 
     Student st = new Student(); 
     st.setName("toto"); 

     XStream xs = new XStream(); 

     xs.alias("student",Student.class); 

     System.out.println(xs.toXML(st)); 
    } 

}

मुझे देता है:

<student> 
    <name>toto</name> 
    <age>0</age> 
</student> 

वहाँ शून्य मान निपटने के लिए एक तरीका है? मेरा मतलब है:

<student> 
    <name>toto</name> 
    <age>0</age> 
    <teacher></teacher> 
</student> 

अगर मैं

st.setTeacher(""); 

है, लेकिन नहीं करता है, तो शिक्षक रिक्त है यह संभव है।

मैंने कस्टम कनवर्टर के साथ प्रयास किया लेकिन ऐसा लगता है कि नल मान कनवर्टर को नहीं भेजे जाते हैं।

public class Student{ 
    String name; 
    int age; 
    String teacher; 
    public void setName(String name){ 
     this.name = name; 
    } 
    public String getName(){ 
     return name; 
    } 
....... 
......//Like that mention other getter and setter method 
} 

अब, तुम सब इस प्रकार अपने कनवर्टर वर्ग बनाने की जरूरत:

public class StudentConverter implements Converter{ 
    public boolean canConvert(Class clazz) { 
     return clazz.equals(Test.class); 
    } 
    public void marshal(Object value, HierarchicalStreamWriter writer, 
     MarshallingContext context) { 
     Student student = (Student) value; 
     writer.startNode("name"); 
     writer.setValue(test.getName()); 
     writer.endNode(); 
     writer.startNode("age"); 
     writer.setValue(test.getAge()); 
     writer.endNode(); 
     writer.startNode("teacher"); 
     writer.setValue(""); 
     writer.endNode(); 
    } 
    public Object unmarshal(HierarchicalStreamReader reader, 
     UnmarshallingContext context) { 
     Student student = new Student(); 
     test.setName(reader.getNodeName()); 
     test.setAge(reader.getNodeName()); 
     test.setTeacher(reader.getNodeName()); 
     return student; 
    } 
} 

Your main class will the like that: 

public class App1{ 
    public static void main(String args[]) { 
     XStream stream = new XStream(new StaxDriver()); 
     Student st = new Student(); 
     st.setName("ABC"); 
     t.setAge(21); 
     stream.registerConverter(new StudentConverter()); 
     stream.alias("student", Student.class); 
     System.out.println(stream.toXML(st)); 
    } 

} 

आशा है कि यह आपकी है

उत्तर

1

सबसे पहले मनुष्य और सेटर तरीके आप में Student वर्ग इस प्रकार प्रदान करते हैं उत्तर .... हैप्पी लर्निंग :)

2

मैं कस्टमस्टाइल नामों के लिए एक्सस्ट्रीम 1.4.7, @XStreamAlias ​​एनोटेशन का उपयोग कर रहा हूं, कस्टम कंस के लिए @XStreamConverter ऊर्ध्वाधर (तिथियों और अन्य कस्टम सेम का प्रतिनिधित्व करने के लिए)। हालांकि, शून्य मूल्यों के लिए एक कस्टम कनवर्टर भी कहा जाता था। मेरा लक्ष्य ऑब्जेक्ट के सभी फ़ील्ड को क्रमबद्ध करना था, जिसमें शून्य शामिल हैं, मुझे XML को अनमर्शल करने की आवश्यकता नहीं थी।

मैं कस्टम प्रतिबिंब कनवर्टर बनाकर ऐसा करने में कामयाब रहा। मैंने XStream लाइब्रेरी से प्रतिबिंब कनवर्टर बढ़ाया और DoMarshal विधि overrode। केवल एक चीज मैं बदल अशक्त info.values ​​के लिए writeField विधि बुला गया था:

StaxDriver driver = new StaxDriver(new NoNameCoder()) { 
    @Override 
    public StaxWriter createStaxWriter(XMLStreamWriter out) throws XMLStreamException { 
     // the boolean parameter controls the production of XML declaration 
     return createStaxWriter(out, false); 
    } 
}; 

XStream xStream = new XStream(driver); 
xStream.autodetectAnnotations(true);//needed to process aliases 
//register MyCustomReflectionConverter 
MyCustomReflectionConverter reflectionConverter = new MyCustomReflectionConverter (xStream.getMapper(), new SunUnsafeReflectionProvider()); 
xStream.registerConverter(reflectionConverter, XStream.PRIORITY_VERY_LOW); 
:

new Object() { 
{ 
    for (Iterator fieldIter = fields.iterator(); fieldIter.hasNext();) { 
     FieldInfo info = (FieldInfo) fieldIter.next(); 
     if (info.value != null) { 
      //leave the code unchanged 
      ... 
     } else { 
      //add this to write null info.value to xml 
      Log.info("MyCustomReflectionConverter -> serialize null field: " + info.fieldName); 
      writeField(info.fieldName, null, info.type, info.definedIn, info.value); 
     } 
    } 
    //... leave the rest of the code unchanged 
} 
}; 

उसके बाद, मैं (यह बहुत कम प्राथमिकता के साथ अपने कनवर्टर रजिस्टर करने के लिए बहुत महत्वपूर्ण है) ऐसे ही Xstream उदाहरण बनाया उसके समाधान के लिए मार्क Nabours को

धन्यवाद here

आशा है कि यह मदद करता है। क्या किसी को इसके लिए बेहतर समाधान मिला है?

0

तुम एक खाली स्ट्रिंग

String teacher = ""; 

या अनुकूलित एक :)

String teacher = StringUtils.EMPTY; 
डाल सकते हैं
संबंधित मुद्दे