In below xml file I have File name and its respective rule numbers in hashmap(named 'ds' in source code) in format {{file1.c,{1234,3456,5087,9900,....}},{file2.c,{1234,3456,5087,9900,....}}>, if for file1.c any rule number in given list exists in xml file for <QACRule> tag (here 5087 is matching) I want to delete its parent node i.e. <Complain> node.
<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> but I'm not understanding the thing that after checking <QACRule> value in hashmap how can I go above and delete the <Complain> node ?
I tried following java code:
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(); with this code I'm facing "javax.xml.stream.XMLStreamException: No element was found to write: java.lang.ArrayIndexOutOfBoundsException: -1" exception and wierdly written output file.
No comments:
Post a Comment