Hello,
work on my game is progressing. Currently i am outsourcing some definitions to a xml file. Basically i want all my the information to be stored in a xml file so i can edit them nicely.
The parsing works also nice with following code:
[java]
public static void parseRaces(GameLogic game,AssetManager assetManager){
String FileName = "assets/Configurations/Races.xml";
//File file= (File) assetManager.loadAsset(new AssetKey(FileName));
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(new File(FileName));
} catch (JDOMException ex) {
} catch (IOException ex) {
}
Element root = doc.getRootElement();
List listParentElements = root.getChildren("race"); // <ParentElem>
for (int i = 0; i < listParentElements.size(); i++) {
Element tmp = (Element) (listParentElements.get(i));
GameRace currRace=new GameRace(Integer.parseInt(tmp.getChild("id").getTextTrim()),
Boolean.parseBoolean(tmp.getChild("playable").getTextTrim()),
tmp.getChild("name").getTextTrim(),
Boolean.parseBoolean(tmp.getChild("hasWeapons").getTextTrim()),
Boolean.parseBoolean(tmp.getChild("hasUnits").getTextTrim()),
Boolean.parseBoolean(tmp.getChild("hasProjectiles").getTextTrim()),
Boolean.parseBoolean(tmp.getChild("hasBuildings").getTextTrim()),
assetManager
);
game.addRace(currRace);
}
}
[/java]
However, when i build the executable all assets get stored into a .jar and i cannot access anymore the files. I understand that i need to use the AssetManager to get the file. But i cannot figure out how…
Thank you in advance
This will not work in the deployed game, you cannot access files from the project directory directly then anymore Load the xml file via the class path and don’t add the “assets” folder name. As written in the manual, the assets folder is compressed and then being added to the class path of the distributed game.
So, do:
[java]
URL racesUrl=this.getClass().getResource("/Configurations/Races.xml");
[/java]
Hello, since your solution obviously does not work from within a static context i implemented this solution:
[java]
doc = builder.build(new File(Thread.currentThread().getContextClassLoader().getResource(FileName).getFile()));
[/java]
Anything against this usage?
Yep, you are trying to get a File again ^^ I don’t think that works with stuff on the class path, replace “this.getClass().getResource()” in my example with: WhateverYourClassIsCalled.class.getResource()
Edit: Oh, and other than the assetManager, you need to start the path with a slash.
Ah now i understand what you mean.
BTW, i implemented following:
[java]
doc = builder.build((Thread.currentThread().getContextClassLoader().getResource(FileName)));
[/java]
And it works in the distribution package
Add: no slash needed on windows…
The lack of slash is because you are hitting the classloader directly (ie: relative to classpath root) rather than relative to a class. Windows or not has nothing to do with it, really.
…but glad you got it working.
@normen: when i use
[java]URL theurl = this.getClass().getResource("/Models/xml/character.xml");[/java]
i got exception in distributed version:
[java] java.io.FileNotFoundException: E:CASTWAY_PROJECTdist9file:E:CASTWAY_PROJECTdist9libassets.jar!Model
sxmlcharacter.xml
[/java]
what’s wrong? is it becouse of capital letters?
Tnx for help
@oxplay2: How should I know? You get that error when you construct the URL? That URL looks ugly… is this OpenJDK? This can only happen when you use some wrong URL class (check your imports) or the underlying java is messed up somehow.
@normen:
yep, i use java.net.URL and it work on SDK
what other URL should i use? i see no other URL classes
I don’t know, it works on a normal system.
it works on a normal system.
yes my windows 7 for sure is Not normal OS :p
Well it cannot read URLs properly, you only answer half of my questions and still be able to give answers. I don’t know why it doesn’t work on your computer. A “system” is not only the OS but everything else as well. Theres millions of things that could be wrong and I am not your answer-fairy.
so I know you’re not a god.
and it is 3:00 for us now, so we don’t think. Maybe just go sleep? :roll:
Oh my… Reading from an URL in the class path isn’t exactly esoteric or jme-specific. Make sure the name is right, yadda, yadda. :google:
@oxplay2 said:
@normen: when i use
[java]URL theurl = this.getClass().getResource("/Models/xml/character.xml");[/java]
i got exception in distributed version:
[java] java.io.FileNotFoundException: E:CASTWAY_PROJECTdist9file:E:CASTWAY_PROJECTdist9libassets.jar!Model
sxmlcharacter.xml
[/java]
what's wrong? is it becouse of capital letters?
Tnx for help :)
Are you using asset manager as is or did you add your own locators or something?
i really should learn more about file managing. i know nothin about it
i try to load xml file from assets.jar but i’m not using assetManager(at all i don’t know how to use it to load xml file)… should i try somethin like assetManager.getClassLoaders()… getResource ?
so then i try to load xml file to process it:
[java]
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
URL xmlUrl = this.getClass().getResource(file);
Document doc = docBuilder.parse(new File(xmlUrl.getFile()));
[/java]
work via SDK, don’t work via disttribute version
@oxplay2 said:
@normen: when i use
[java]URL theurl = this.getClass().getResource("/Models/xml/character.xml");[/java]
i got exception in distributed version:
[java] java.io.FileNotFoundException: E:CASTWAY_PROJECTdist9file:E:CASTWAY_PROJECTdist9libassets.jar!Model
sxmlcharacter.xml
[/java]
what's wrong? is it becouse of capital letters?
Tnx for help :)
Ok, responding again after your last response because I didn't realize this is just a Java question and not a JME question.
You left out all of the important bits above like the stack trace but I just finished watching Sherlock Holmes so I'm going to do my best at deductions.
My guess is that you create a URL and then try to pass that into new File(). Your current working directory is E:/CASTWAY_PROJECT/dist and so you are getting a file that looks like that.
But really, a URL is not a file. So you are just passing random garbage to new File(). This is not how to read a class resource. URL != File... not even slightly.
Either use getResourceAsStream() and read what you want or use the URL methods for accessing the contents.
Edit: just saw your latest post... so I was right. You are passing incomprehensible garbage to File.
@oxplay2 said:
work via SDK, don't work via disttribute version
The reason it works via that SDK is because the URL is relative and refers to an actual on-the-disk file. Once the assets have been jar'ed up, the URL refers to a entry in the Jar file.
DocumentBuilder can take input streams... that's what you should be using.
…I need to do the facepalm-monkey-smiley…
@pspeed said:
The reason it works via that SDK is because the URL is relative and refers to an actual on-the-disk file. Once the assets have been jar'ed up, the URL refers to a entry in the Jar file.
DocumentBuilder can take input streams... that's what you should be using.
Yes its basic Java, but i never had to use refer to file from jars.
And yes it refer to entry assets Jar, i just don't known it is problem for it.
I will try use stream :)