Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying XML File to Write to New XML File in Java

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();
        }
    }
like image 447
This 0ne Pr0grammer Avatar asked Dec 28 '22 18:12

This 0ne Pr0grammer


1 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.

like image 177
Ted Hopp Avatar answered Jan 10 '23 00:01

Ted Hopp