I'm getting the following error,
java.io.IOException: Read error
at java.io.FileInputStream.read(Native Method) at
com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity
(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion (Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.example.TestIntegrate.execute(TestIntegrate.java:71)
at com.example.TestIntegrate.main(TestIntegrate.java:42)
Here is my code:
public class TestIntegrate {
private Document doc = null;
public static void main(String[] args) {
FileInputStream fin;
try {
fin = new FileInputStream("C:/Users/xyz/workspace/TEST_2.xml");
FileOutputStream fout = new FileOutputStream("C:/Users/xyz/workspace/OutputFile.xml");
TestIntegrate t = new TestIntegrate();
t.execute(fin, fout);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void execute(InputStream sourceFile, OutputStream targetFile) //throws StreamTransformationException
{
BufferedReader reader;
OutputStreamWriter writer;
try{
// creating the parser object
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
String line = "<Tax>";
String line1 = "</Tax>";
String currentLine;
reader = new BufferedReader(new InputStreamReader(sourceFile));
writer =new OutputStreamWriter(targetFile);
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
while ( (currentLine = reader.readLine() ) != null){
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(line) || trimmedLine.equals(line1) ) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
reader.close();
writer.close();
doc = dBuilder.parse(sourceFile);
writeOutputfile(doc,targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeOutputfile(Document doc,OutputStream targetFile) {
try {
TransformerFactory transformFactory = TransformerFactory.newInstance();
DOMSource source = new DOMSource(doc);
Transformer transformer;
transformer = transformFactory.newTransformer();
Writer outWriter = new StringWriter();
StreamResult result = new StreamResult(targetFile);
transformer.transform(source,result);
}
catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The process I have to do is:
1) reading the file from the source location.. ( here I am using main() just for testing purpose)
2) deleting the <Tax> and </Tax> nodes in the source file.
3) writng the file to target location.
XML FIle:
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<School>
<SSLC>
<name />
<rollno />
</SSLC>
<Tax>
<first_pu>
<name />
<rollno />
</first_pu>
<second_pu>
<name />
<rollno />
</second_pu>
</Tax>
<Tax>
<first_pu>
<name />
<rollno />
</first_pu>
<second_pu>
<name />
<rollno />
</second_pu>
</Tax>
<Tax>
<first_pu>
<name />
<rollno />
</first_pu>
<second_pu>
<name />
<rollno />
</second_pu>
</Tax>
</School>
please tell me the way to resolve this issue..
Thanks in advance..
C:/Users/xyz/workspace/TEST_2.xml?