Java Android-使用XPath从XML检索值

Java Android-使用XPath从XML检索值,java,android,xpath,Java,Android,Xpath,我从web服务检索了以下XML。如何使用XPath从XML中获取依赖FirstName的值,即(Rajesh)?这是给安卓的。有人能帮我吗 <Enrollment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ContactInfo> <_contactRelations/> <ContactId&g

我从web服务检索了以下XML。如何使用XPath从XML中获取依赖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>

83
-1
1-CCR1
EX-PG-30021045
M101
马赫什中国
马赫什
CN
1980-03-02T00:00:00
地址
新城
圣劳伦斯
弗吉尼亚州
11111
马赫什。narayanan@speridian.com
1.
0001-01-01T00:00:00
2012-03-01T00:00:00
上海证券交易所
钦奈
30021045
A001
010101010
管理层
1.
M
PGA
0001-01-01T00:00:00
0001-01-01T00:00:00
假的
假的
0001-01-01T00:00:00
-1
假的
0001-01-01T00:00:00
假的
0001-01-01T00:00:00
2012-01-01T00:00:00
2012-12-31T00:00:00
2012-03-02T14:57:47.687
SPERIDIAN\sujeeth.anand
2012-03-07T10:05:14.28
人力资源部
3.
1-CCR1
人力资源部
cGFzc3dvcmQ=
0
0
1.
-1
0001-01-01T00:00:00
0001-01-01T00:00:00
0001-01-01T00:00:00
B68F7D913FF44B8D9CB7B9F4686D469D
383hr|MSmith | 7 | 01 | 03 | 0301 | 0302 | 0304 | 04 | 0401 | 0402 | 0403 | 0405 | 0406 | 08 |玛丽·史密斯
美国3152224321M01
1-90SBCPDP
SimplyBlue公司支付扣除计划
医学的
EMP_配偶
雇员及配偶
243.33
737.36
28
01/01/2013
12/31/2013
假的
5.
1-CCR1
公开注册
30021045
28
M101MaheshCN03/02/1980M010101010mahesh。narayanan@speridian.comaddressnew城市A111111-CLA103/01/2012Sechennairajeshrrai01/03/1980MdepAddress新城市A1111142545False
2013-01-01T00:00:00
2013-12-31T00:00:00
一氧化碳
1.
-1
2012-03-12T11:09:09.21
WSWIN7001\BRYMY DWCC6 G8YPG
2012-03-13T15:00:07.95
人力资源部
已完成(等待批准)
0
0

您没有提到要针对哪个版本的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();
import org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathFactory;
导入java.io.ByteArrayInputStream;
导入java.io.InputStream;
导入java.nio.charset.charset;
...
字符串xml=”“;
字符串表达式=“/Enrollment/FirstName”;
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
setNamespaceAware(true);
DocumentBuilder=builderFactory.newDocumentBuilder();
InputStream in=newbytearrayinputstream(xml.getBytes(Charset.forName(“UTF-8”));
Document=builder.parse(in);
XPath=XPathFactory.newInstance().newXPath();
Node firstNameNode=(Node)xpath.evaluate(表达式、文档、XPathConstants.Node);
字符串firstName=firstNameNode.getTextContent();

您没有提到要针对哪个版本的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();
import org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathFactory;
导入java.io.ByteArrayInputStream;
导入java.io.InputStream;
导入java.nio.charset.charset;
...
字符串xml=”“;
字符串表达式=“/Enrollment/FirstName”;
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
setNamespaceAware(true);
DocumentBuilder=builderFactory.newDocumentBuilder();
InputStream in=newbytearrayinputstream(xml.getBytes(Charset.forName(“UTF-8”));
Document=builder.parse(in);
XPath=XPathFactory.newInstance().newXPath();
Node firstNameNode=(Node)xpath.evaluate(表达式、文档、XPathConstants.Node);
字符串firstName=firstNameNode.getTextContent();