我有这个XML文件(example.xml):
<PayrollFormInfo xmlns="http://www.irs.com/PayrollFormInfo/2004">
<FormInfo>
<ExportDataVersion>100</ExportDataVersion>
<QBVersion>IFSP</QBVersion>
<FormSetID>FSDYF</FormSetID>
<FormID>INWKS941</FormID>
<FormDesc>Quarterly Federal Tax Return</FormDesc>
<FormFilingPeriod>Quarterly</FormFilingPeriod>
<TotalsBreakoutBy>Daily</TotalsBreakoutBy>
<BeginDate>01/01/2015</BeginDate>
<EndDate>03/31/2015</EndDate>
<EFYes>true</EFYes>
<IncludeInstructionSheet>false</IncludeInstructionSheet>
</FormInfo>
<CompanyInfo>
<CompanyName>PatsFedTaxSemiWeekly20144QRT_CT</CompanyName>
<LegalName>P #a(9t)-'&sFedTaxSemiWeekly2014_4QRTCT</LegalName>
<EmployerID>507754170</EmployerID>
<IntuitPayrollServiceID>1862684</IntuitPayrollServiceID>
<LegalAddressLine1>Def-ault/ .String</LegalAddressLine1>
<LegalCity>St. Louis</LegalCity>
<LegalState>CB</LegalState>
<LegalZip>06300-0987</LegalZip>
<MailingAddressLine1>Default String Addr</MailingAddressLine1>
<MailingCity>Greenwich</MailingCity>
<MailingState>CT</MailingState>
<MailingZip>06830</MailingZip>
<NumberOfEmployees>2</NumberOfEmployees>
<FWTDepositFrequency>SEMI_WEEKLY</FWTDepositFrequency>
<ExemptFlag>false</ExemptFlag>
<EmployerType>Contributing</EmployerType>
<FUTAExemptFlag>false</FUTAExemptFlag>
<FederalTotals>
<PeriodBeginDate>01/01/2015</PeriodBeginDate>
<PeriodEndDate>01/01/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>15384.62</SocialSecurityWages>
<SocialSecurityLiability>-10.10</SocialSecurityLiability>
<MedicareWages>15384.62</MedicareWages>
<MedicareLiability>-1.01</MedicareLiability>
<FederalWages>13076.92</FederalWages>
<FederalWithholding>-101.00</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>1.01</SocialSecurityTips>
</FederalTotals>
<FederalTotals>
<PeriodBeginDate>01/02/2015</PeriodBeginDate>
<PeriodEndDate>01/02/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>15384.62</SocialSecurityWages>
<SocialSecurityLiability>-10.20</SocialSecurityLiability>
<MedicareWages>15384.62</MedicareWages>
<MedicareLiability>-1.02</MedicareLiability>
<FederalWages>13076.92</FederalWages>
<FederalWithholding>-102.00</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>1.02</SocialSecurityTips>
</FederalTotals>
<TaxItemTotals>
<PeriodBeginDate>01/01/2015</PeriodBeginDate>
<PeriodEndDate>03/31/2015</PeriodEndDate>
<TaxTableID>200</TaxTableID>
<TotalWagesAndTips>0.00</TotalWagesAndTips>
<TaxableWagesAndTips>164615.44</TaxableWagesAndTips>
<TaxAmount>0.00</TaxAmount>
</TaxItemTotals>
<PaymentInfo>
<PeriodBeginDate>01/14/2015</PeriodBeginDate>
<PeriodEndDate>01/16/2015</PeriodEndDate>
<TaxTableID>62</TaxTableID>
<Amount>953.86</Amount>
<PaymentDate>04/03/2015</PaymentDate>
</PaymentInfo>
<EmpsWorked>
<TaxTableID>63</TaxTableID>
<TestDate>3/12/2015</TestDate>
<NumEmps>2</NumEmps>
</EmpsWorked>
<EmpSummary>
<EmployeeID>4514230</EmployeeID>
<FirstName>JillCT</FirstName>
<MiddleInitial>S</MiddleInitial>
<LastName>Taylor</LastName>
<SSN>150-86-6794</SSN>
<IsStatutory>false</IsStatutory>
<HasPensionPlan>false</HasPensionPlan>
<EmployeeType>REGULAR</EmployeeType>
</EmpSummary>
<EmpSummary>
<EmployeeID>4514229</EmployeeID>
<FirstName>JoseCT</FirstName>
<MiddleInitial>S</MiddleInitial>
<LastName>George</LastName>
<SSN>792-93-6215</SSN>
<IsStatutory>false</IsStatutory>
<HasPensionPlan>false</HasPensionPlan>
<EmployeeType>REGULAR</EmployeeType>
</EmpSummary>
<FederalReturnType>941</FederalReturnType>
</CompanyInfo>
<EmployeeInfo>
<EmployeeType>REGULAR</EmployeeType>
<EmployeeID>4514230</EmployeeID>
<FirstName>JillCT</FirstName>
<MiddleInitial>S</MiddleInitial>
<LastName>Taylor</LastName>
<SSN>150-86-6794</SSN>
<HireDate>10/09/2009</HireDate>
<IsStatutory>false</IsStatutory>
<HasPensionPlan>false</HasPensionPlan>
<NumberOfExemptions>0</NumberOfExemptions>
<StateLived>CT</StateLived>
<StateWorked>CT</StateWorked>
<Gender>F</Gender>
<AddressLine1>123 default St.</AddressLine1>
<City>Greenwich</City>
<State>CT</State>
<ZipCode>06830</ZipCode>
<FederalTotals>
<PeriodBeginDate>01/01/2015</PeriodBeginDate>
<PeriodEndDate>01/31/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>49269.21</SocialSecurityWages>
<SocialSecurityLiability>-3054.69</SocialSecurityLiability>
<MedicareWages>50769.24</MedicareWages>
<MedicareLiability>-736.16</MedicareLiability>
<FederalWages>46153.84</FederalWages>
<FederalWithholding>-14242.77</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<FederalTotals>
<PeriodBeginDate>02/01/2015</PeriodBeginDate>
<PeriodEndDate>02/28/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>15384.62</SocialSecurityWages>
<SocialSecurityLiability>-953.85</SocialSecurityLiability>
<MedicareWages>35384.62</MedicareWages>
<MedicareLiability>-513.08</MedicareLiability>
<FederalWages>33076.92</FederalWages>
<FederalWithholding>-10637.87</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<FederalTotals>
<PeriodBeginDate>03/01/2015</PeriodBeginDate>
<PeriodEndDate>03/31/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>46153.86</SocialSecurityWages>
<SocialSecurityLiability>-2861.54</SocialSecurityLiability>
<MedicareWages>196153.86</MedicareWages>
<MedicareLiability>-3654.22</MedicareLiability>
<FederalWages>189230.76</FederalWages>
<FederalWithholding>-68440.64</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
</EmployeeInfo>
<EmployeeInfo>
<EmployeeType>REGULAR</EmployeeType>
<EmployeeID>4514229</EmployeeID>
<FirstName>JoseCT</FirstName>
<MiddleInitial>S</MiddleInitial>
<LastName>George</LastName>
<SSN>792-93-6215</SSN>
<HireDate>10/09/2009</HireDate>
<IsStatutory>false</IsStatutory>
<HasPensionPlan>false</HasPensionPlan>
<NumberOfExemptions>0</NumberOfExemptions>
<StateLived>CT</StateLived>
<StateWorked>CT</StateWorked>
<Gender>M</Gender>
<AddressLine1>123 default St.</AddressLine1>
<City>Greenwich</City>
<State>CT</State>
<ZipCode>06830</ZipCode>
<FederalTotals>
<PeriodBeginDate>02/01/2015</PeriodBeginDate>
<PeriodEndDate>02/28/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>15384.62</SocialSecurityWages>
<SocialSecurityLiability>-953.85</SocialSecurityLiability>
<MedicareWages>35384.62</MedicareWages>
<MedicareLiability>-513.08</MedicareLiability>
<FederalWages>33076.92</FederalWages>
<FederalWithholding>-10637.87</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<FederalTotals>
<PeriodBeginDate>01/01/2015</PeriodBeginDate>
<PeriodEndDate>01/31/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>49269.21</SocialSecurityWages>
<SocialSecurityLiability>-3054.69</SocialSecurityLiability>
<MedicareWages>50769.24</MedicareWages>
<MedicareLiability>-736.16</MedicareLiability>
<FederalWages>46153.84</FederalWages>
<FederalWithholding>-14242.77</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<FederalTotals>
<PeriodBeginDate>03/01/2015</PeriodBeginDate>
<PeriodEndDate>03/31/2015</PeriodEndDate>
<GrossPay>0.00</GrossPay>
<NetPay>0.00</NetPay>
<SocialSecurityWages>46153.86</SocialSecurityWages>
<SocialSecurityLiability>-2861.54</SocialSecurityLiability>
<MedicareWages>196153.86</MedicareWages>
<MedicareLiability>-3654.22</MedicareLiability>
<FederalWages>189230.76</FederalWages>
<FederalWithholding>-68440.64</FederalWithholding>
<TaxableFUTAWages>0.00</TaxableFUTAWages>
<TotalFUTAWages>0.00</TotalFUTAWages>
<FUTALiability>0.00</FUTALiability>
<EIC>0.00</EIC>
<SocialSecurityTips>0.00</SocialSecurityTips>
</FederalTotals>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>1</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>63</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>64</TaxTableID>
<TestDate>01/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>62</TaxTableID>
<TestDate>03/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
<WorkedInfo>
<TaxTableID>61</TaxTableID>
<TestDate>02/12/2015</TestDate>
<Worked>true</Worked>
</WorkedInfo>
</EmployeeInfo>
<PaidPreparerInfo>
<Title>Agent in Fact</Title>
<Date>03/15/2015</Date>
<PrintedName>John Doe</PrintedName>
<PhoneNumber>888-555-5555</PhoneNumber>
<FaxNumber>775-555-5555</FaxNumber>
<FEIN>12-3456789</FEIN>
<Address1>123 Main St</Address1>
<City>Reno</City>
<State>NV</State>
<Zip>89500</Zip>
<FirmName>ACME Inc</FirmName>
<EmailAddress>tax_eservice@irs.com</EmailAddress>
</PaidPreparerInfo>
</PayrollFormInfo>
我想将它解析为Map数据结构,如下所示:
[{/PayrollFormInfo/FormInfo[1]/ExportDataVersion[1]=100},{/PayrollFormInfo/FormInfo[1]/QBVersion[1]=IFSP},{/PayrollFormInfo/FormInfo[1]/FormSetID[1]=FSDYF},{/PayrollFormInfo/FormInfo[1]/FormID[1]=INWKS941}, etc...]
注意在XML中有重复的元素节点,因此它们的出现顺序用括号[出现次数]来表征。
我很困惑如何解决这个问题。这似乎是一件简单的事情,但是我很难解决这个问题,我不确定我做错了什么。
以下是我在Java代码中所做的事情:
@Test
public void testP() throws Exception, FactoryConfigurationError, Exception {
File f = new File("xmlDir/example.xml");
Map<String,String> map = p(f);
logger.debug("CONTENTS OF MAP: "+Arrays.toString(map.entrySet().toArray()));
}
public Map<String, String> p(File file) throws Exception {
Map<String, String> map = new LinkedHashMap<String,String>();
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(file));
while(xr.hasNext()) {
int e = xr.next();
if (e == XMLStreamReader.START_ELEMENT) {
String name = xr.getLocalName();
xr.next();
String value = null;
try {
value = xr.getText().trim();
} catch (IllegalStateException exep) {
exep.printStackTrace();
}
if (value != null && !value.isEmpty()) {
map.put(name, value);
}
}
}
return map;
}
输出:
CONTENTS OF MAP: [ExportDataVersion=100, QBVersion=IFSP, FormSetID=FSDYF, FormID=INWKS941, FormDesc=Quarterly Federal Tax Return, FormFilingPeriod=Quarterly, TotalsBreakoutBy=Daily, BeginDate=01/01/2015, EndDate=03/31/2015, EFYes=true, IncludeInstructionSheet=false, CompanyName=PatsFedTaxSemiWeekly20144QRT_CT, LegalName=P #a(9t)-', EmployerID=507754170, IntuitPayrollServiceID=1862684, LegalAddressLine1=Def-ault/ .String, LegalCity=St. Louis, LegalState=CB, LegalZip=06300-0987, MailingAddressLine1=Default String Addr, MailingCity=Greenwich, MailingState=CT, MailingZip=06830, NumberOfEmployees=2, FWTDepositFrequency=SEMI_WEEKLY, ExemptFlag=false, EmployerType=Contributing, FUTAExemptFlag=false, PeriodBeginDate=03/01/2015, PeriodEndDate=03/31/2015, GrossPay=0.00, NetPay=0.00, SocialSecurityWages=46153.86, SocialSecurityLiability=-2861.54, MedicareWages=196153.86, MedicareLiability=-3654.22, FederalWages=189230.76, FederalWithholding=-68440.64, TaxableFUTAWages=0.00, TotalFUTAWages=0.00, FUTALiability=0.00, EIC=0.00, SocialSecurityTips=0.00, TaxTableID=61, TotalWagesAndTips=0.00, TaxableWagesAndTips=164615.44, TaxAmount=0.00, Amount=953.84, PaymentDate=03/27/2015, TestDate=02/12/2015, NumEmps=2, EmployeeID=4514229, FirstName=JoseCT, MiddleInitial=S, LastName=George, SSN=792-93-6215, IsStatutory=false, HasPensionPlan=false, EmployeeType=REGULAR, FederalReturnType=941, HireDate=10/09/2009, NumberOfExemptions=0, StateLived=CT, StateWorked=CT, Gender=M, AddressLine1=123 default St., City=Reno, State=NV, ZipCode=06830, Worked=true, Title=Agent in Fact, Date=03/15/2015, PrintedName=Joe Doe, PhoneNumber=888-555-4444, FaxNumber=775-555-5555, FEIN=12-3456789, Address1=123 Main St, Zip=89500, FirmName=ACME Inc, EmailAddress=tax_eservice@IRS.com]
这里的问题是,由于存在重复元素节点,我当前的实现仅捕获元素节点的1次出现。因此,如果有一种方法可以将密钥设为XPath,那么我可以将上述次数指示为[1]或[2],我不会遇到这个问题。这样我就可以捕获所有的价值观。
非常感谢任何帮助或帮助。