I'm tryi开发者_StackOverflow中文版ng to use Jaxb in order to unamrshal an xml file. for some reason that I don't understand, I cannot refer to any other location then a full path on my specific computer. In the code below, the commented line doesn't work, but only the one above it. the file does exist (in two locations) and the commented line does work on a diffrent class.
JAXBContext jc = JAXBContext.newInstance(Monopoly.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
//unmarshaller.unmarshal(new File("resources/monopoly_config.xml" ));
unmarshaller.unmarshal(new File( "C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\BoardInfoResources\\monopoly_config.xml"));
UPDATE
Since you are deploying to a server (from your comments), why not load your XML using a ClassLoader? In a server environment you wouldn't be able to count on the File object in the way that you want to (as you have already discovered):
ClassLoader cl = Monopoly.class.getClassLoader();
InputStream xml =
cl.getResourceAsStream("resources/monopoly_config.xml");
JAXBContext jc = JAXBContext.newInstance(Monopoly.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Monopoly monopoly = (Monopoly) unmarshaller.unmarshal(xml);
JAXB will allow you to unmarshal any valid file object. And it will definitely unmarshal files created with relative paths (see answer below for an example):
- How to unmarshal xml message with bad parent/child model
In your example you will need to make sure that your working directory is set correctly. Based that your full path is:
"C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\BoardInfoResources\\monopoly_config.xml"
Assuming your working directory is:
"C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\"
Your relative path appears to be wrong (since there is no resources directory):
"resources/monopoly_config.xml"
You may have meant it to be:
"BoardInfoResources/monopoly_config.xml"
精彩评论