0
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<GTSResponse command="dbget" result="success">
<Record table="Device" partial="true">
    <Field name="accountID" primaryKey="true"><![CDATA[UsRentcar]]></Field>
    <Field name="deviceID" primaryKey="true"><![CDATA[85412452145214]]></Field>
    <Field name="lastOdometerKM">12222.0442925558</Field>
    <Field name="description"><![CDATA[Toyota Land Cruser]]></Field>
</Record>
<Record table="Device" partial="true">
    <Field name="accountID" primaryKey="true"><![CDATA[UsRentcar]]></Field>
    <Field name="deviceID" primaryKey="true"><![CDATA[843254752364]]></Field>
    <Field name="lastOdometerKM">4348.48814289997</Field>
    <Field name="description"><![CDATA[Chevrolet white]]></Field>
</Record>   

I have another Response show above with various records. How can I put together two field values in each record. For example :

String [] ListDevice_&_ListDescripcion = { 85412452145214 , Toyota Land Cruser ; 843254752364, Chevrolet white ;....} ; 

How can I do it? Here is the result that I have . Please help!

name="accountID" UsRentcar
name="deviceID" 85412452145214 ; name="lastOdometerKM" 14214.0020055 ; name="description" Toyota Land Cruser ; name="accountID" UsRentcar ; name="deviceID" 843254752364; name="lastOdometerKM" 4348.488142847 
name="description" Chevrolet white –
Jose Perez
  • 27
  • 5
  • No way `ListDevice_&_ListDescripcion` is a valid identifier – MightyPork Jan 03 '15 at 19:01
  • It's was an example taht i put to show how i would like to get all values from this result: name="accountID" UsRentcar name="deviceID" 85412452145214 ; name="lastOdometerKM" 14214.0020055 ; name="description" Toyota Land Cruser ; name="accountID" UsRentcar ; name="deviceID" 843254752364; name="lastOdometerKM" 4348.488142847 name="description" Chevrolet white – – Jose Perez Jan 03 '15 at 19:08

1 Answers1

0

You can use JAXB for this:

Create the class that represent the XML:

@XmlRootElement(name = "GTSResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public static class GTSResponse {

    @XmlElement(name = "Record")
    List<Record> records;
}

@XmlRootElement(name = "Record")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Record {

    @XmlElement(name = "Field")
    List<Field> fields;

}

@XmlRootElement(name = "Field")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Field {

    @XmlAttribute(name = "name")
    String name;
    @XmlAttribute(name = "primaryKey")
    boolean primaryKey;

    @XmlValue
    String data;
}

And use like this:

@Test
public void testName() throws Exception {
    String f = ...; // path to XML

    JAXBContext context = JAXBContext.newInstance(GTSResponse.class);
    Unmarshaller unma = context.createUnmarshaller();
    // here you can pass a inputStream instead of a new File
    GTSResponse response = (GTSResponse) unma.unmarshal(new File(f));

    List<String> result = new ArrayList<String>();
    for (Record r : response.records) {
        System.out.println("Start record");
        for (Field fi : r.fields) {
            System.out.println(fi.name + ":" + fi.data + "(Primary: "
                    + fi.primaryKey + ")");
            if (fi.name.equals("deviceID") || fi.name.equals("description"))
                result.add(fi.data);
        }
    }

    // The array this print is exactly as you want
    System.out.println(Arrays.toString(result.toArray()));

}

Note, you can use JAXB to map your XML to a class with XPATH, with the annotation @XMLPath, the annotation is part of MOXY.

Arturo Volpe
  • 3,157
  • 3
  • 23
  • 39
  • I'm trying to use it, could you show me all library that you import, cause sometime it suggests me two kind of library. – Jose Perez Jan 03 '15 at 23:18
  • I'm trying to use it, could you show me all library that you import, cause sometime it suggests me two kind of library. Well, some error like this : (Illegal modifier for the class Record; only public, abstract & final are permitted) ; (XmlAccessType cannot be resolved to a variable) ; Illegal modifier for the class GTSResponse; only public, abstract & final are permitted .... – Jose Perez Jan 03 '15 at 23:29
  • Here the [gist](https://gist.github.com/aVolpe/2ad74af81bf002193dab), see the imports. – Arturo Volpe Jan 04 '15 at 07:42
  • Thaank's bro. I gonna try it right now. I Write you back – Jose Perez Jan 04 '15 at 16:31
  • Normally i have some errors like this : 258 is not a valid line number in com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallerImpl ; The Jar file C:/User/.../jaxb-ri/lib/jaxb-impl.jar has no source attachment. How could resolve it bro? – Jose Perez Jan 04 '15 at 17:09
  • It's seems that your library is corrupted, why are you using a library? all the classes are standar classes of the JDK. – Arturo Volpe Jan 04 '15 at 19:04
  • ok but When i'm using it without externals jar from the reference (JAXB) that you sent to me, almost all reserved words show with errors and don't show suggest import. that's why i downloaded the reference JAXB and imported it into my project and i have all this kind of error. – Jose Perez Jan 04 '15 at 19:14
  • You are using Eclipse? what are the errors? I use Eclipse, and dont have any problems – Arturo Volpe Jan 04 '15 at 21:10
  • When i copy and paste all imports from your code, and i put the mouse focus on them i get this error ( Access restriction: The type Unmarshaller is not accessible due to restriction on required library C:\Program Files\Java\jre1.8.0_25\lib\rt.jar) – Jose Perez Jan 04 '15 at 22:44
  • See [this](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar), – Arturo Volpe Jan 04 '15 at 23:14
  • Very helpful bro, i change this code like this and it works perfectly GTSResponse response = (GTSResponse) unma.unmarshal(new ByteArrayInputStream(xml.toString().getBytes())); [85412452145214 , "Toyota Land Cruser"] .... Thank's a lot bro – Jose Perez Jan 05 '15 at 00:06
  • But my idea is to use this result in a listview Android Ecplise. Each deviceID with Description respectively together in a list view Android. How can i fill or poupulate the listview bro? – Jose Perez Jan 05 '15 at 00:12
  • @JosePerez please, mail me or make another question, this is not the rigth place for a discussion. – Arturo Volpe Jan 05 '15 at 13:24
  • ok, here is the link bro : http://stackoverflow.com/questions/27784479/need-to-fill-listview-android-with-the-result – Jose Perez Jan 05 '15 at 17:11
  • I'm trying to resolve it, but when the listview is openning i have this error : E/AndroidRuntime(30636): java.lang.NoClassDefFoundError: javax.xml.bind.JAXBContext. However i'm using all the standar classes of JDK. How can i solve it? – Jose Perez Jan 05 '15 at 20:34