I have the following XML retreived from webservices.How do i get the value of StructureTransID i.e(254) from the above XML using XPath in android? Can anyone help me on this.
<Response>
−
<NewDataSet>
−
<EnrollmentList>
<Transaction_ID>369</Transaction_ID>
<Transaction_Key>489</Transaction_Key>
<Transaction_Type>ENROLLMENT_INFO</Transaction_Type>
<Corp_ID>2</Corp_ID>
−
<Transaction_Xml>
<EnrollmentInfo><SelectedPackages><Package Id="1-GQGFWG" Category="Medical" CoverageLevel="EMP_CHILDREN" CoverageAmt="355.11" TotalCoverageAmt="710.22" StructureTransID="254" EffectiveDate="01/01/2012" TerminationDate="12/31/2012" /></SelectedPackages><PersonalInfo><EmployeeID>E0211</EmployeeID><FirstName>Michael</FirstName><MiddleName /><LastName>Keaton</LastName><DateOfBirth>10/21/1975</DateOfBirth><Gender>M</Gender><SSN>123456789</SSN><Email>mkeaton@designllc.com</Email><AddressLine1>33 Park Street</AddressLine1><AddressLine2 /><AddressLine3 /><City>Jacksonville</City><State>FL</State><ZipCode>32220</ZipCode><HomePhone>(111) 222-4444</HomePhone><WorkPhone>(913) 244-8188</WorkPhone><Dependents><Dependent><FirstName>Alisha</FirstName><MiddleName /><LastName>Jones</LastName><DateOfBirth>1/13/2009</DateOfBirth><Gender>F</Gender><Relationship>Dependent</Relationship><AddressLine1>33 Park Street</AddressLine1><AddressLine2 /><AddressLine3 /><City>Jacksonville</City><State>FL</State><ZipCode>32220</ZipCode><HomePhone /><MedicareNumber /><PartA_EffectiveDate /><PartB_EffectiveDate /><IsFullTimeStudent>False</IsFullTimeStudent><CollegeName /><ExpectedGraduationDate /><CreditHours /></Dependent></Dependents></PersonalInfo><WorkInfo><Class>A004</Class><DateOfHire>05/01/2010</DateOfHire><Designation>Associate</Designation><WorkLocation>Canton</WorkLocation></WorkInfo><SelectedPackages /></EnrollmentInfo>
</Transaction_Xml>
<Active_Ind>1</Active_Ind>
<Effective_Date>2012-01-01T00:00:00+05:30</Effective_Date>
<Status>CO</Status>
<Created_On>2011-12-20T00:47:20.187+05:30</Created_On>
<Created_By>msmith</Created_By>
<Modified_On>2012-02-10T17:50:15.647+05:30</Modified_On>
<Modified_By>mkeaton</Modified_By>
<Termination_Date>2012-12-31T00:00:00+05:30</Termination_Date>
<Comments>Change in employment status</Comments>
<Task_ID>636</Task_ID>
<User_ID>489</User_ID>
<Import_ID>476</Import_ID>
<Corp_ID1>2</Corp_ID1>
<Employee_ID>E0211</Employee_ID>
<Login>mkeaton</Login>
<Password>cGFzc3dvcmQ=</Password>
<Full_Nm>Michael Keaton</Full_Nm>
<First_Nm>Michael</First_Nm>
<Middle_Nm/>
<Last_Nm>Keaton</Last_Nm>
<DOB>1975-10-21T00:00:00+05:30</DOB>
<Address_1>33 Park Street</Address_1>
<Address_2/>
<Address_3/>
<City>Jacksonville</City>
<State>FL</State>
<Postal_Code>32220</Postal_Code>
<Home_Phone_No>(111) 222-4444</Home_Phone_No>
<Work_Phone_No>(913) 244-8188</Work_Phone_No>
<Cell_Phone_No/>
<Email_Addr>mkeaton@designllc.com</Email_Addr>
<Login_Attempts_Cnt>0</Login_Attempts_Cnt>
<Login_Locked_Ind>0</Login_Locked_Ind>
<Allow_Internet_Use>1</Allow_Internet_Use>
<Active_Ind1>1</Active_Ind1>
<Hired_Dt>2010-05-01T00:00:00+05:30</Hired_Dt>
<Designation>Associate</Designation>
<Work_Location>Canton</Work_Location>
<SubGroup_ID>0001</SubGroup_ID>
<Created_On1>2011-12-20T00:44:04.577+05:30</Created_On1>
<Created_By1>msmith</Created_By1>
<Modified_On1>2012-02-10T17:50:10.343+05:30</Modified_On1>
<Modified_By1>mkeaton</Modified_By1>
<SSN>123456789</SSN>
<Class>A004</Class>
<Status1>A</Status1>
<EULA>1</EULA>
<Gender>M</Gender>
<ReAssignedToCobra>false</ReAssignedToCobra>
<ClassDesc>A004 - Management</ClassDesc>
</EnrollmentList>
</NewDataSet>
</Response>
I have tried this code in Java it works fine,but the same concept does not retreive the value in android.
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XML {
private NodeList nodelist;
private static Element ele;
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
builder = factory.newDocumentBuilder();
doc = builder.parse("/home/stella/person.xml");
ele = doc.getDocumentElement();
XPathFactory factor = XPathFactory.newInstance();
XPath xpath = factor.newXPath();
XPathExpression expr = xpath.compile("//Package");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
{
Node currentItem = nodes.item(i);
String key = currentItem.getAttributes().getNamedItem("StructureTransID").getNodeValue();
String value = currentItem.getTextContent();
System.out.printf("%1s = %2s\n", key, value);
}
}
}
I copied your XML file to Notepad and tried to save it but it reported that “This file contains characters in Unicode format which will be lost if you save this file as an ANSI encoded text file.”. I let it proceed and then copied your program to a new Android project and it works! i.e. it does load your XML file and extract the “254” value.
I then went back to the XML file and attempted to identify the weird characters and it led me to the 3 characters that look like minus signs, located at:
This got me to think that these characters aren’t minus signs, but, perhaps some weird encoding which is preventing it from working with the XML parser on Android. These characters, if correct, may need to be edited or migrated to an encoding that works with Android, perhaps UTF-8?
Anyhow, I think this should give enough pointers on where to look.