[英]XMLStreamException: No element was found to write: ArrayIndexOutOfBoundsException while parsing xml file using StAX
在下面的xml文件中,我在格式為{{file1.c,{1234,3456,5087,9900,....}},{file2 .c,{1234,3456,5087,9900,....}}>,如果對於file1.c,給定列表中的任何規則號都存在於<QACRule>
標記的xml文件中(此處匹配5087),我想刪除它的父節點,即<Complain>
節點。
<SourceFiles>
**<File name="file1.c">**
<FileSummary>
<QAC>
<StatDev>0</StatDev>
<StatIntendDev>1</StatIntendDev>
</QAC>
<Developer>
<StatDev>0</StatDev>
<StatIntendDev>0</StatIntendDev>
<StatBlank>1</StatBlank>
</Developer>
<Total>
<TotalRank>intended deviation</TotalRank>
<StatDev>0</StatDev>
<StatIntendDev>1</StatIntendDev>
</Total>
</FileSummary>
**<Complain lfd="1">**
<Line>43</Line>
<Description>"#include statements"</Description>
<Rule>
**<QACRule>5087</QACRule>**
<CodingStd>18.1.</CodingStd>
<Deviation>true</Deviation>
</Rule>
<Ranking>
<QACRank>intended deviation</QACRank>
<DeveloperRank></DeveloperRank>
<TotalRank>intended deviation</TotalRank>
</Ranking>
<Comment>these includes must stay here at this position</Comment>
</Complain>
**<Complain lfd="2">**
<Line>140</Line>
<Description>"#include statements"</Description>
<Rule>
**<QACRule>55555</QACRule>**
<CodingStd>18.1.</CodingStd>
<Deviation>true</Deviation>
</Rule>
<Ranking>
<QACRank>intended deviation</QACRank>
<DeveloperRank></DeveloperRank>
<TotalRank>intended deviation</TotalRank>
</Ranking>
<Comment>these includes must stay here at this position</Comment>
</Complain>
</File>
<File name="file2.c>
......
</File>
</SourceFiles>
但是我不明白在檢查哈希表中的<QACRule>
值之后,如何在上面刪除<Complain>
節點?
我嘗試了以下Java代碼:
inFactory = XMLInputFactory.newInstance();
outFactory= XMLOutputFactory.newInstance();
eventReader = inFactory.createXMLEventReader(new FileReader(inputFilePath));
out=new FileOutputStream("C:\\Users\\uidg8154\\Desktop\\new.xml");
eventWriter= outFactory.createXMLEventWriter(out);
while(eventReader.hasNext()){
XMLEvent event=eventReader.nextEvent();
if(event.isStartElement() && event.asStartElement().getName().getLocalPart().equalsIgnoreCase("SourceFiles") ){
isSourceFileParent=true;
System.out.println("-------start of <SourceFies>---------");
srcFiles=new SourceFiles();
}
if(event.isEndElement() && event.asEndElement().getName().getLocalPart().equalsIgnoreCase("SourceFiles")){
isSourceFileParent=false;
System.out.println("-------end of <SourceFies>---------");
}
if(isSourceFileParent==true && event.isStartElement() && event.asStartElement().getName().getLocalPart().equalsIgnoreCase("File")){
fileNameValue=event.asStartElement().getAttributeByName(new QName("name")).getValue();
System.out.println(fileNameValue);
sourceFileNamesFromInputXml.add(fileNameValue);
}
if(isSourceFileParent==true && event.isStartElement() && event.asStartElement().getName().getLocalPart().equalsIgnoreCase("Complain")){
complainIdValue=event.asStartElement().getAttributeByName(new QName("lfd")).getValue();
System.out.println(complainIdValue);
}
if(isSourceFileParent==true && event.isStartElement() && event.asStartElement().getName().getLocalPart().equalsIgnoreCase("QACRule")){
event = eventReader.nextEvent();
isQacRuleParent=true;
isProceed=true;
qacRuleNoValue=event.asCharacters().getData();
}
if(ds.containsKey(fileNameValue) && isProceed==true){
qacRuleNos=ds.get(fileNameValue);
if(qacRuleNos.contains(qacRuleNoValue)){
isIgnoreComplainNode=true;
}
}
if (isSourceFileParent==true && event.isEndElement() && event.asEndElement().getName().getLocalPart().equals("Complain") && isIgnoreComplainNode==true) {
isIgnoreComplainNode=false;
}
if(isIgnoreComplainNode==false){
System.out.println(event);
eventWriter.add(event);
}
}//end of while
eventWriter.flush();
eventWriter.close();
使用此代碼,我將面臨“ javax.xml.stream.XMLStreamException:找不到要寫入的元素:java.lang.ArrayIndexOutOfBoundsException:-1”異常和編寫異常的輸出文件。
您在同時進行stax解析和寫入時,可能需要處理更多上下文。 當您檢測到isIgnoreComplainNode=true
,已經太晚了,因為您已經在輸出中寫入了與以下內容對應的事件:
<Complain lfd="1">
<Line>43</Line>
<Description>"#include statements"</Description>
<Rule>
<QACRule>
如果要避免在輸出中出現這些事件,則應在列表中的<Complain>
打開到</Complain>
關閉之間收集事件,當達到<Complain>
關閉時:檢查isIgnoreComplainNode
:
清除列表並繼續解析。
主要的問題是“如何避免在輸出中寫入不必要的<Complain>
節點”,即“如何刪除並刪除<Complain>
節點”。 如果將事件發送到eventWriter,則為時已晚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.