java - Android - 使用 XPath 从 XML 检索值

标签 java android xpath

我从 Web 服务中检索到以下 XML。如何使用 XPath 从 XML 中获取 Dependent FirstName(即 Rajesh)的值?这是针对安卓系统的。谁能帮我解决这个问题吗?

<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>mahesh.narayanan@speridian.com</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>mahesh.narayanan@speridian.com</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>

最佳答案

您没有提及您所针对的 Android 版本,但在当前版本的 Android 上,以下代码将从 XML 中获取名字。您必须将 xml 变量设置为从 Web 服务获取的 XML,然后您将在 firstName 变量中找到结果。

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();

关于java - Android - 使用 XPath 从 XML 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9718259/

相关文章:

php - Simplexml xpath 和 xmlns

.net - 检查节点是否存在报错 "Expression must evaluate to a node-set"如何解决?

java - 如何在Struts2中使用JSF数据表?

java - 读取 .xml 文件进行解析时出现 FileNotFoundException

java - 限制方法接受 ContextCompat 解析的资源

java - 从返回 xml 的请求中获取单个值的最佳方法?

android - 如何在android中的hdmi连接上的应用程序中获取通知

java - 如何使用环境变量从Java代码设置文件?

android - 屏幕锁定、wifi 关闭、手机睡​​眠之前的空闲时间段?

java - 通过 XML 记录进行复杂的搜索查询