简体   繁体   中英

Deserialize XML in multiple objects using SimpleXML

I built xml files in Android from objects by appending multiple objects to the same file using Simple

<listOfBtDevices>
   <devices class="java.util.ArrayList">
   <BTDevice>
     <address>00:27:13:A3:2D:14</address>
     <bondState>NONE</bondState>
     <deviceType>LAPTOP</deviceType>
     <name>LTPH</name>
     <services>AUDIO CAPTURE NETWORKING OBJECT_TRANSFER RENDERING TELEPHONY</services>
     <rssi>-95</rssi>
   </BTDevice>
   <BTDevice>
     <address>00:27:13:A3:2D:14</address>
     <bondState>NONE</bondState>
     <deviceType>LAPTOP</deviceType>
     <name>LTPH</name>
     <services>AUDIO CAPTURE NETWORKING OBJECT_TRANSFER RENDERING TELEPHONY</services>
     <rssi>-95</rssi>
   </BTDevice>
   <BTDevice>
     <address>00:27:13:A3:2D:14</address>
     <bondState>NONE</bondState>
     <deviceType>LAPTOP</deviceType>
     <name>LTPH</name>
     <services>AUDIO CAPTURE NETWORKING OBJECT_TRANSFER RENDERING TELEPHONY</services>
     <rssi>-95</rssi>
   </BTDevice>
 </devices>
 <timestamp>22.11.2013_10.56.44</timestamp>
</listOfBtDevices>
<listOfBtDevices>
 <devices class="java.util.ArrayList">
    <BTDevice>
     <address>00:27:13:A3:2D:14</address>
     <bondState>NONE</bondState>
     <deviceType>LAPTOP</deviceType>
     <name>LTPH</name>
     <services>AUDIO CAPTURE NETWORKING OBJECT_TRANSFER RENDERING TELEPHONY</services>
     <rssi>-95</rssi>
    </BTDevice>
  </devices>
  <timestamp>22.11.2013_10.56.50</timestamp>
</listOfBtDevices>

In the example above the object is ListOfBtDevices which is compound of a (String)timestamp and an ArrayList of BTDevice. The question is how can I deserialize it in multiple ListOfBtDevice objects using Simple or other framework on the Desktop Computer?

Thank you and sorry if I made mistakes but I am beginner in JAVA.

First of all, for you to be able to deserialize the xml file it needs to have one single root node. not multiple like in your case.

You have:

<listOfBtDevices>
...
</listOfBtDevices>
<listOfBtDevices>
...
</listOfBtDevices>

What it should look like:

<root>
    <listOfBtDevices>
    ...
    </listOfBtDevices>
    <listOfBtDevices>
    ...
    </listOfBtDevices>
</root>

After that you need to deserialize using some framework like Simple or XStream . I would recommend Simple for your purposes and I will give you a rudimentary example using Simple:

To deserialize the XML into objects using Simple you have to create model classes which have the same structure like your xml. You describe how the Objects are mapped to XML using annotations like @Element or @Attribute. In your case it would look something like this:

First you need a class which corresponds to your root node:

// The name you enter here is how the root node is called in your xml
@Root(name = "root")
public class BluetoothDeviceListContainer {

    // It contains a List, inline is set to true because the list items are directly inside the root node, each entry is called "listOfBtDevices"
    @ElementList(entry = "listOfBtDevices", inline = true) 
    List<BluetoothDeviceList> bluetoothDeviceList;

}

And you continue to do this until you have fully described your xml with classes. Next you would create a Class BluetoothDeviceList which corresponds to the "listOfBtDevices" nodes and enter which elements and attributes are contained and this class etc.

When you are finished you can deserialize your xml like this:

Serializer serializer = new Persister();
BluetoothDeviceListContainer container = serializer.read(BluetoothDeviceListContainer.class, xmlAsString);

After adding some getters and if you need setters to your model classes you can access all deserialized properties like this:

BluetoothDeviceList list = container.get(0);
...

Here you can find a complete tutorial for Simple and here some additional examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM