I am working on StaxParser and want to read a xml file.
我使用了以下代码:
public class StaxParserDemo {
public static void main(String args[]){
try{
List<PLQ22> plqList = new ArrayList<>();
PLQ22 currEmp = null;
String tagContent = null;
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream("/home/vipul/Documents/config.xml"));
while(reader.hasNext()){
int event = reader.next();
switch(event){
case XMLStreamConstants.START_ELEMENT:
if ("EpsonPLQ22_PassbookPrinter".equals(reader.getLocalName())){
currEmp = new PLQ22();
}
break;
case XMLStreamConstants.CHARACTERS:
tagContent = reader.getText().trim();
break;
case XMLStreamConstants.END_ELEMENT:
switch(reader.getLocalName()){
case "EpsonPLQ22_PassbookPrinter":
plqList.add(currEmp);
// currEmp.Auto_Start=tagContent;
break;
case "Barcode_Length":
currEmp.Barcode_Length = tagContent;
tagContent=null;
break;
case "Lower_Barcode_length":
currEmp.Lower_Barcode_length = tagContent;
tagContent=null;
break;
case "Upper_Barcode_Length":
currEmp.Upper_Barcode_Length = tagContent;
tagContent=null;
break;
}
if ("EpsonPLQ22_PassbookPrinter".equals(reader.getLocalName())){
currEmp = null;
}
break;
case XMLStreamConstants.START_DOCUMENT:
plqList = new ArrayList<>();
break;
}
}
for ( PLQ22 emp : plqList){
System.out.println(emp);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
class PLQ22{
String Auto_Start;
String Barcode_Length;
String Lower_Barcode_length;
String Upper_Barcode_Length;
@Override
public String toString(){
return Barcode_Length+" "+Lower_Barcode_length+" "+Upper_Barcode_Length;
}
}
我有一个非常大的config.xml文件,它有很多节点,但我想读取EpsonPLQ22_PassbookPrinter节点的值。
在得到我真正想要的值之后,它给了我多个空值。
xml文件是这样的:
<ACU_Config_File>
<ApplicationManager_Config>
<ConsumerDisplay_Config>
<Resolution>1366 x 768</Resolution>
</ConsumerDisplay_Config>
<Maintenance_Application>
<Cash_Deposit_Report>ON</Cash_Deposit_Report>
<Idle_Time>1</Idle_Time>
<Lockout_Time>1</Lockout_Time>
<ATM_EXE_NAME>ATM</ATM_EXE_NAME>
<ATM_PATH>C:\Program Files\Forbes\Microbanker\Bin\ATM.exe</ATM_PATH>
</Maintenance_Application>
<ReportSettings_Config>
<ChequeDeposit_ReportSettings>
<EndorsementReportSettings_Config>
<TransactionId Present="True" />
<ChequeNo Present="True" />
<Endorsement Present="False" />
</EndorsementReportSettings_Config>
<EndorsementSettings_Config Required="False">
.
.
.
.
.
.
.
<EpsonPLQ22_PassbookPrinter>
<Auto_Start>Start</Auto_Start>
<Barcode_Length>7</Barcode_Length>
<Lower_Barcode_length>14</Lower_Barcode_length>
<Upper_Barcode_Length>13</Upper_Barcode_Length>
</EpsonPLQ22_PassbookPrinter>
.
.
.
.
<PassbookPrintingTimeOut>30</PassbookPrintingTimeOut>
<InstructionTimeOut>30</InstructionTimeOut>
<InsertChequeTimeOut>30</InsertChequeTimeOut>
<SelectLanguageTimeOut>30</SelectLanguageTimeOut>
</MFK_TIMERS_CONFIG>
</MFK_ApplicationManager_Config>
</ACU_Config_File>
我需要在代码中进行哪些更改才能获取空值并直接获取节点值。
我得到的输出是这样的:
空 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 空值 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13 7 14 13
提前致谢。