0

I have the following XML retrieved from a web service. How do I get the value of Dependent FirstName i.e( Rajesh) from the XML, using XPath? This is for the Android. Can anyone help me on this?

<Enrollment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ContactInfo>
<_contactRelations/>
<ContactId>83</ContactId>
<ImportId>-1</ImportId>
<ExtContactId>1-CCR1</ExtContactId>
<OrgId>EX-PG-30021045</OrgId>
<ParContactId/>
<EmployeeId>M101</EmployeeId>
<FullName>Mahesh CN</FullName>
<FirstName>Mahesh</FirstName>
<MiddleName/>
<LastName>CN</LastName>
<DOB>1980-03-02T00:00:00</DOB>
<Address1>address</Address1>
<Address2/>
<Address3/>
<City>new city</City>
<County>Saint Lawrence</County>
<State>VA</State>
<PostalCode>11111</PostalCode>
<HomePhoneNo/>
<WorkPhoneNo/>
<CellPhoneNo/>
<EmailAddress>[email protected]</EmailAddress>
<Active>1</Active>
<InActiveDt>0001-01-01T00:00:00</InActiveDt>
<HiredDt>2012-03-01T00:00:00</HiredDt>
<Designation>SSE</Designation>
<WorkLocation>Chennai</WorkLocation>
<GroupNo>30021045</GroupNo>
<SubGroupNo>A001</SubGroupNo>
<SSN>010101010</SSN>
<Class>Management</Class>
<EULA>1</EULA>
<Gender>M</Gender>
<Type>PGA</Type>
<PartAEffectiveDt>0001-01-01T00:00:00</PartAEffectiveDt>
<PartBEffectiveDt>0001-01-01T00:00:00</PartBEffectiveDt>
<IsDomesticPartner>false</IsDomesticPartner>
<IsFTStudent>false</IsFTStudent>
<GraduationDt>0001-01-01T00:00:00</GraduationDt>
<CreditHours>-1</CreditHours>
<IsDependentHandicapped>false</IsDependentHandicapped>
<TerminationDt>0001-01-01T00:00:00</TerminationDt>
<ReAssignedToCobra>false</ReAssignedToCobra>
<CobraEffectiveDt>0001-01-01T00:00:00</CobraEffectiveDt>
<RecEffectiveDt>2012-01-01T00:00:00</RecEffectiveDt>
<RecTerminationDt>2012-12-31T00:00:00</RecTerminationDt>
<CreatedOn>2012-03-02T14:57:47.687</CreatedOn>
<CreatedBy>SPERIDIAN\sujeeth.anand</CreatedBy>
<ModifiedOn>2012-03-07T10:05:14.28</ModifiedOn>
<ModifiedBy>HR_MSMITH</ModifiedBy>
<UserInfo>
<AccessGroups/>
<UserId>3</UserId>
<ContactId>1-CCR1</ContactId>
<Login>hr_MSmith</Login>
<Password>cGFzc3dvcmQ=</Password>
<LoginAttemptsCnt>0</LoginAttemptsCnt>
<LoginLockedInd>0</LoginLockedInd>
<AllowInternetUse>1</AllowInternetUse>
<Active>-1</Active>
<InActiveDt>0001-01-01T00:00:00</InActiveDt>
<CreatedOn>0001-01-01T00:00:00</CreatedOn>
<ModifiedOn>0001-01-01T00:00:00</ModifiedOn>
<SessionId>B68F7D913FF44B8D9CB7B9F4686D469D</SessionId>
<SessionData>
<ROOT><SESSIONDATA><CLIENT_INFO><USER_ID>3</USER_ID><CONTACT_ID>83</CONTACT_ID><LOGIN_ID>hr_MSmith</LOGIN_ID><ROLE_ID>|7|</ROLE_ID><OBJECT_ACCESS>|01|03|0301|0302|0304|04|0401|0402|0403|0404|0405|0406|08|</OBJECT_ACCESS><FULL_NAME>Mary Smith</FULL_NAME></CLIENT_INFO><PAGE_VARS /><Messaging /></SESSIONDATA></ROOT>
</SessionData>
</UserInfo>
<x001>
<MoreInfo><countryCode>USA</countryCode><phoneExtension /><faxNumber>3152224321</faxNumber><subscriberId /><memberId>M01</memberId><title /></MoreInfo>
</x001>
<ContactRelations/>
</ContactInfo>
<EnrollmentPlans>
<EnrolledPlan>
<PackageId>1-90SBCPDP</PackageId>
<PackageName>SimplyBlue Copay Deduct Plan</PackageName>
<PackageCategory>Medical</PackageCategory>
<CoverageLevel>EMP_SPOUSE</CoverageLevel>
<CoverageLevelDesc>Employee & Spouse</CoverageLevelDesc>
<CoverageAmt>243.33</CoverageAmt>
<TotalCoverageAmt>737.36</TotalCoverageAmt>
<InstallationRowId>28</InstallationRowId>
<EffectiveDate>01/01/2013</EffectiveDate>
<TerminationDate>12/31/2013</TerminationDate>
<IsNew>false</IsNew>
</EnrolledPlan>
</EnrollmentPlans>
<RowId>5</RowId>
<ContactId>1-CCR1</ContactId>
<Type>OPEN_ENROLL</Type>
<OrgId>30021045</OrgId>
<PlanSetupId>28</PlanSetupId>
<EnrollInfoXml>
<EnrollmentInfo><PersonalInfo><EmployeeID>M101</EmployeeID><FirstName>Mahesh</FirstName><MiddleName /><LastName>CN</LastName><DateOfBirth>03/02/1980</DateOfBirth><Gender>M</Gender><SSN>010101010</SSN><Email>[email protected]</Email><AddressLine1>address</AddressLine1><AddressLine2 /><AddressLine3 /><City>new city</City><State>VA</State><ZipCode>11111</ZipCode><HomePhone /><WorkPhone /></PersonalInfo><WorkInfo><Class>1-CLA1</Class><DateOfHire>03/01/2012</DateOfHire><Designation>SSE</Designation><WorkLocation>Chennai</WorkLocation></WorkInfo><SelectedPackages><Package Id="1-90SBCPDP" Category="Medical" CoverageLevel="EMP_SPOUSE" CoverageAmt="243.33" TotalCoverageAmt="737.36" InstallationRowId="28" EffectiveDate="01/01/2013" TerminationDate="12/31/2013"><Dependents><Dependent ExtContactId="REF-445"><FirstName>Rajesh</FirstName><MiddleName>R</MiddleName><LastName>Rai</LastName><DateOfBirth>01/03/1980</DateOfBirth><Gender>M</Gender><Relationship>DEP</Relationship><AddressLine1>address</AddressLine1><AddressLine2 /><AddressLine3 /><City>new city</City><State>VA</State><ZipCode>11111</ZipCode><HomePhone>4254545</HomePhone><MedicareNumber /><PartA_EffectiveDate /><PartB_EffectiveDate /><IsFullTimeStudent>False</IsFullTimeStudent><CollegeName /><ExpectedGraduationDate /><CreditHours /></Dependent></Dependents></Package></SelectedPackages></EnrollmentInfo>
</EnrollInfoXml>
<EffectiveDt>2013-01-01T00:00:00</EffectiveDt>
<TerminationDt>2013-12-31T00:00:00</TerminationDt>
<Status>CO</Status>
<Active>1</Active>
<TaskId>-1</TaskId>
<CreatedOn>2012-03-12T11:09:09.21</CreatedOn>
<CreatedBy>WSWIN7001\BRYMY DWCC6 G8YPG</CreatedBy>
<ModifiedOn>2012-03-13T15:00:07.95</ModifiedOn>
<ModifiedBy>hr_MSmith</ModifiedBy>
<StatusDesc>Completed (Waiting for approval)</StatusDesc>
<TotalPlanAmount>0</TotalPlanAmount>
<TotalEmployeeContribution>0</TotalEmployeeContribution>
</Enrollment>

1 Answer 1

3

You did not mention which version of Android you are targeting, but on current versions of Android, the following code will get you the first name from the XML. You have to set the xml variable to the XML you got from the web service and you will find the result in the firstName variable.

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

...

  String xml = "<...>";
  String expression = "/Enrollment/FirstName";

  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  builderFactory.setNamespaceAware(true);
  DocumentBuilder builder = builderFactory.newDocumentBuilder();
  InputStream in = new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")));
  Document document = builder.parse(in);

  XPath xpath = XPathFactory.newInstance().newXPath();
  Node firstNameNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
  String firstName = firstNameNode.getTextContent();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.