2010-11-02 17 views
11

का उपयोग कर ऑब्जेक्ट की सूची को XML दस्तावेज़ में कनवर्ट करने के लिए कैसे करें XStream का उपयोग कर ऑब्जेक्ट की सूची XML दस्तावेज़ में कनवर्ट करने के लिए कैसे करें?XStream

और इसे वापस कैसे deserialize?

यह मेरा एक्सएमएल

<?xml version="1.0" encoding="UTF-8"?> 
<persons> 
<person> 
    <fullname>Guilherme</fullname> 
    <age>10</age> 
    <address>address,address,address,address,</address> 
</person> 
<person> 
    <fullname>Guilherme</fullname> 
    <age>10</age> 
    <address>address,address,address,address,</address> 
</person> 
</persons> 

व्यक्ति सेम 3 फ़ील्ड हैं कैसे वापस कस्टम कन्वर्टर्स का उपयोग कर बीन सूची करने के लिए इसे परिवर्तित करने के लिए है?

उत्तर

20

आप आवश्यक रूप से एक CustomConverter की जरूरत नहीं है: आप का पालन करने की जरूरत है।

public class PersonList { 

    private List<Person> list; 

    public PersonList(){ 
     list = new ArrayList<Person>(); 
    } 

    public void add(Person p){ 
     list.add(p); 
    } 
} 

एक्सएमएल के लिए सूची serialise करने के लिए::

:

XStream xstream = new XStream(); 
    xstream.alias("person", Person.class); 
    xstream.alias("persons", PersonList.class); 
    xstream.addImplicitCollection(PersonList.class, "list"); 

    PersonList list = new PersonList(); 
    list.add(new Person("ABC",12,"address")); 
    list.add(new Person("XYZ",20,"address2")); 

    String xml = xstream.toXML(list); 

व्यक्ति वस्तुओं की एक सूची के लिए एक्सएमएल deserialise करने के लिए

आप अपनी सूची धारण करने के लिए एक वर्ग की जरूरत है

String xml = "<persons><person>...</person></persons>"; 
    PersonList pList = (PersonList)xstream.fromXML(xml); 
+0

अपडेट किया है धन्यवाद बी यहां मेरे मामले में मैं केवल deserializing हूँ, मुझे अपने स्वयं के कनवर्टर लिखने की जरूरत है, मैं सूची रूपांतरण –

+0

के लिए उलझन में हूँ आप शायद हमें अपनी सूची वर्ग दिखाना चाहिए। – dogbane

+0

आपके उदाहरण में मुझे एक त्रुटि मिलती है: "कोई फ़ील्ड" सूची "निहित संग्रह के लिए" –

4

बस std toXml और xml विधियों का उपयोग करें, उदाहरण के लिए http://en.wikipedia.org/wiki/XStream देखें। डिफ़ॉल्ट रूपांतरण कैसे काम करते हैं, इस पर http://x-stream.github.io/converters.html भी देखें।

ठीक है, इसलिए डिफ़ॉल्ट कनवर्टर्स आपके मामले में काफी काम नहीं करेंगे।

http://x-stream.github.io/converter-tutorial.html

+0

धन्यवाद बनाएँ, मैंने प्रश्न –

1

लोड एक्सएमएल

public static Object Load(String xmlPath) throws Exception 
{ 
    File FileIn = new File(xmlPath); 
    if(FileIn.exists()) { 
     //Initialise Doc 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document Doc = builder.parse(FileIn); 

     //Initialise XPath 
     XPathFactory xPathFactory = XPathFactory.newInstance(); 
     XPath xpath = xPathFactory.newXPath(); 

     String objectClassLocation = xpath.evaluate("/object/@class",Doc); 
     Object ObjType; 

     //Create List of attributes for the Student 
     XPathExpression xpathExpression = xpath.compile("/object/*"); 
     NodeList ObjTypeAttributes = (NodeList)xpathExpression.evaluate(Doc, XPathConstants.NODESET); 

     ObjType = CreateObject(ObjTypeAttributes, objectClassLocation); 
     return ObjType; 
    } 
    return null; 
} 

वस्तु बनाएं

public static Object CreateObject(NodeList ObjectAttributes, String Location) throws Exception 
{ 
    Class ClassName = Class.forName(Location); 
    Object object = ClassName.newInstance(); 
    Field[] fields = ClassName.getFields(); 

    for(int x = 0; x < fields.length;x++) 
    { 
     for(int y = 0; y<ObjectAttributes.getLength(); y++) 
     { 
      if(!(ObjectAttributes.item(y) instanceof Text)) { 
       String check = ObjectAttributes.item(y).getAttributes().item(0).getNodeValue(); 

       if(fields[x].getName().equals(check)) 
       { 
        Field curField = ClassName.getField(fields[x].getName()); 
        if(ObjectAttributes.item(y).getAttributes().getLength() < 2) { 

         curField.set(object,CreateList(ObjectAttributes.item(y).getChildNodes())); 
        } 
        else { 

         curField.set(object,ObjectAttributes.item(y).getAttributes().item(1).getNodeValue()); 
        } 

       } 

      } 
     } 

    } 
    return object; 

} 

सूची (केवल इस्तेमाल किया है, तो एक्सएमएल वस्तुओं का एक उद्देश्य है)

public static ArrayList CreateList(NodeList ArrayNodeList) throws Exception 
{ 
    ArrayList List = new ArrayList(); 

    for(int x = 0; x < ArrayNodeList.getLength();x++) 
    { 
     if(!(ArrayNodeList.item(x) instanceof Text)) { 
      Node curNode = ArrayNodeList.item(x); 

      NodeList att = curNode.getChildNodes(); 
      String Location = ArrayNodeList.item(x).getAttributes().item(0).getNodeValue(); 
      Object newOne = CreateObject(att, Location); 
      List.add(newOne); 

     } 
    } 
    return List; 
} 

एक्सएमएल उदाहरण मैं प्रयोग किया जाता

<?xml version="1.0" encoding="UTF-8"?> 
<object class="Example.Rps"> 
<field name="Representatives"> 
<object class="Example.Rep"> 
    <field name="RepID" value="888225462"/> 
    <field name="Surname" value="Johnson"/> 
    <field name="Name" value="Dave"/> 
    <field name="Clients"> 
     <object class="Example.Client"> 
      <field name="ClientName" value="Cipla"/> 
      <field name="State" value="New York"/> 
      <field name="grade" value="A"/> 
     </object> 
     <object class="Example.Client"> 
      <field name="ClientName" value="Pharmco"/> 
      <field name="State" value="Iowa"/> 
      <field name="grade" value="B"/> 
     </object> 
    </field> 
</object> 
    <object class="Example.Rep"> 
     <field name="RepID" value="888225462"/> 
     <field name="Surname" value="Dickson"/> 
     <field name="Name" value="Ben"/> 
     <field name="Clients"> 
      <object class="Example.Client"> 
       <field name="ClientName" value="XYZ"/> 
       <field name="State" value="New Mexico"/> 
       <field name="grade" value="A"/> 
      </object> 
      <object class="Example.Client"> 
       <field name="ClientName" value="Pharmco"/> 
       <field name="State" value="Ohio"/> 
       <field name="grade" value="c"/> 
      </object> 
     </field> 
    </object> 
</field> 
</object> 
संबंधित मुद्दे