1

I have this xml string response ( it's not an xml file, however a xml that show after a request), how can I get fields value? for example fields (accountID, contactName...)

<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<GTSResponse command="dbget" result="success">
    <Record table="Account" partial="true">
        <Field name="accountID" primaryKey="true"><![CDATA[UsRentcar]]></Field>
        <Field name="contactName"><![CDATA[US Car Rental]]></Field>
        <Field name="contactPhone"><![CDATA[17864752145]]></Field>
        <Field name="contactEmail" alternateKeys="email"><![CDATA[uscarrentr@gmail.com]]></Field>
        <Field name="lastLoginTime">1419871821</Field>
        <Field name="description"><![CDATA[US Car Rental"]]></Field>
    </Record>
</GTSResponse>
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
Jose Perez
  • 27
  • 5

1 Answers1

1

You can use Xpath to search for all fields, and get the values, like this:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    String xmlString = ...;
    Document xml = builder.parse(new ByteArrayInputStream(xmlString.getBytes()));

    XPath xPath = XPathFactory.newInstance().newXPath();
    String expression = "/GTSResponse/Record/Field";
    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xml,
            XPathConstants.NODESET);

    System.out.println(nodeList);
    System.out.println(nodeList.getLength());

    for (int i = 0; i < nodeList.getLength(); i++) {
        String value = nodeList.item(i).getFirstChild().getNodeValue();
        String name = nodeList.item(i).getAttributes().getNamedItem("name")
                .toString();
        System.out.println(name + ":" + value);

    }

This output:

    name="accountID":UsRentcar
    name="contactName":US Car Rental
    name="contactPhone":17864752145
    name="contactEmail":uscarrentr@gmail.com
    name="lastLoginTime":1419871821
    name="description":US Car Rental"

To store in variables:

    String accountId = nodeList.item(0).getFirstChild().getNodeValue();
    String contactName = nodeList.item(1).getFirstChild().getNodeValue();
    String contactPhone = nodeList.item(2).getFirstChild().getNodeValue();
    String contactEmail = nodeList.item(3).getFirstChild().getNodeValue();
    String lastLoginTime = nodeList.item(4).getFirstChild().getNodeValue();
    String description = nodeList.item(5).getFirstChild().getNodeValue();
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
  • You import it? `import javax.xml.xpath.XPathFactory;`, you are in Android?, check [this](http://developer.android.com/reference/javax/xml/xpath/XPathFactory.html0) – Arturo Volpe Jan 02 '15 at 00:15
  • Thank's a lot.Very hopeful. I have another Response with various records. How can i put togother 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 Jan 03 '15 at 16:23
  • Please, ask another question IN SO, and discuss there, post the link here. – Arturo Volpe Jan 03 '15 at 18:08
  • right. here is the link http://stackoverflow.com/questions/27758304/need-to-get-values-in-opengts – Jose Perez Jan 03 '15 at 18:47
  • Here is the link, but i edited the post ! http://stackoverflow.com/questions/27758304/need-to-get-values-in-opengts – Jose Perez Jan 03 '15 at 19:00