0

I have the following code to open up an XML file for parsing. My problem is with the variable designPath: it currently has to be a relative path from my program's root directory. However, when I pass an absolute path it doesn't work.

// Get the DOM Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Get the DOM Builder
DocumentBuilder builder = factory.newDocumentBuilder();

// Load and Parse the XML document
// document contains the complete XML as a Tree
Document document = builder.parse(ClassLoader.getSystemResourceAsStream(designPath));

How can I make this work with either an absolute or relative path in the designPath variable?

The issue is with the function ClassLoader.getSystemResourceAsStream that takes a "resource of the specified name from the search path used to load classes." according to Java docs but I want to be able to use it with an absolute path.

Note that my designPath may be on a different drive than my program.

2 Answers 2

1

Pass in an InputStream instead:

// Get the DOM Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Get the DOM Builder
DocumentBuilder builder = factory.newDocumentBuilder();

// Load and Parse the XML document
// document contains the complete XML as a Tree
File file = new File(PATH_TO_FILE);
InputStream inputStream = new FileInputStream(file);

Document document = builder.parse(inputStream);

or simple pass a new File object

// Get the DOM Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Get the DOM Builder
DocumentBuilder builder = factory.newDocumentBuilder();

// Load and Parse the XML document
// document contains the complete XML as a Tree
Document document = builder.parse(new File(PATH_TO_FILE));
Sign up to request clarification or add additional context in comments.

Comments

0

Well you can't because as you mentioned, getSystemResourceAsStream only searches the classpath.

You may have to so something like this.

Document document = null;
if (ClassLoader.getSystemResource(designPath) != null)
{
    document = builder.parse(ClassLoader.getSystemResourceAsStream(designPath));
}
else
{
    // use standard file loader
    document = builder.parse(new File(designPath));
}

Swap the if clauses around if you want the file on the file system to take precedence.

3 Comments

What's the difference to passing in the File object straight away, as opposed to passing in the InputStream Object as in the above solution? Is there a difference?
If your classpath is something like CP=/home/user/xmlfiles:program.jar, and the file path is just design.xml, then your code will search /home/user/xmlfiles first, and if that fails, it will then look for design.xml relative to where you ran it.
@mohsaied I think in your case it doesn't matter whether you pass the InputStream or the File object, but in other cases you don't always get an InputStream from a file so it might make more sense to pass the InputStream. I'll update my answer so that others know there is an option!

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.