I was wondering if anyone knew if it was possible to use one of the XML parsers in Java to read line-by-line, each of the rows in an XML document and basically reproduce the same document in another XML file? (In my case, take only the lines from Point X to Point Y in the document and copy them). I thought about using using the bufferedreader and bufferedwriter in a small trial run, but it did not quite output the file properly. Below is what I was doing in my trial run, but it is not what I want. So does anyone have any experience with this or have any thoughts or suggestions to offer? Thank you in advance.

JAVA CODE

public class IPDriver { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne/word/document.xml")); BufferedWriter writer = new BufferedWriter(new FileWriter("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne/word/tempdocument.xml")); String line = null; while ((line = reader.readLine()) != null) { writer.write(line); } // Close to unlock. reader.close(); // Close to unlock and flush to disk. writer.close(); } } 

Working JAVA Code Thanks To Ted Hopp

public class IPDriver { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne/word/document.xml"), "UTF-8")); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Extracted Items/ProposalOne/word/tempdocument.xml"), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { writer.write(line); } // Close to unlock. reader.close(); // Close to unlock and flush to disk. writer.close(); } } 
3

3 Answers

If your code didn't copy the file over properly, my guess is that you have a character encoding problem. Since the default encoding for XML is UTF-8 and the default encoding for FileReader is the default encoding for your platform, I suggest doing this instead:

BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream("...input file path..."), "UTF-8" ) ); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("...output file path..."), "UTF-8" ) ); 

XML parsers will give you elements (or element events), not lines. For instance, they cannot distinguish between variations in white space:

<tag attr1="val1" attr2="val2" /> 

versus:

<tag attr1="val1" attr2="val2" /> 

If your requirements include distinguishing those two cases, then an XML parser approach would not work.

6

If you just want a copy, do not make the rookie mistake of using a Reader but copy using InputStream/OutputStream. And even with Readers, why would you read it line by line? Just read buffer-fulls of characters.

So why avoid Reader? Because it adds overhead of decoding bytes to characters (and requiring a Writer to encode from chars to bytes), which is of no value to you. And that can also introduce issues, if you make another common mistake of not specifying encoding to use for Reader or Writer -- that will then use whatever platform default encoding is, which may or may not be encoding that File you are reading is using.

You could easily link a reader and writer with StAX. Using that API, you could also easily create a filter to extract just the portions of the document you want. Here are a couple of links which might help:

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy