0

I get the below error when trying to update an array of xml files.

Snippet if code:

File dir = new File("c:\\XML");

File[] files = dir.listFiles(new FilenameFilter() {

    public boolean accept(File dir, String name) {
        return name.toLowerCase().endsWith(".xml");
    }
});

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(Arrays.toString(files));

ERROR:

java.net.MalformedURLException: no protocol: [c:\XML\file.xml, c:\XML\file2.xml, c:\XML\file3.xml]

Can someone please point me in the right direction?

Thanks a lot,

Adam

3 Answers 3

1

You have to pass your file array's indices one by one to Document#parse() e.g.:

Document doc = docBuilder.parse(files[0]);

You can loop this:

for (File f : files) {
    Document doc = docBuilder.parse(f);

    // do something with the doc
}
Sign up to request clarification or add additional context in comments.

Comments

0

DocumentBuilder.parse accepts as argument a File object, a String containing path to file or URI.

In the above code snippet you are passing an array, You have to modify code as follows:

Document doc = docBuilder.parse(files[i])

And call this recursively setting the value of i based on the size of array files.

Comments

0
Document doc = docBuilder.parse(Arrays.toString(files));

You convert an array of Files to a single String.

[c:\XML\file.xml, c:\XML\file2.xml, c:\XML\file3.xml]

This is the representation you get.

DocumentBuilder#parse doesn't know how to parse several documents at once, and why should it know such a notation for a series of filenames?

You need to iterate over the files:

List<Document> docs = new LinkedList<Document>();
for (File aFile: files) {
    docs.add(docBuilder.parse(files[i].getName())); // Or do something different
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.