This site requires JavaScript, please enable it in your browser!
Greenfoot back
lapickett
lapickett wrote ...

2017/11/8

XML file compatibility on JAR files

lapickett lapickett

2017/11/8

#
I exported a greenfoot file as a jar file, and now it can't write to the xml file. It worked before we exported it but in JAR format it doesn't work. Help.
danpost danpost

2017/11/8

#
lapickett wrote...
I exported a greenfoot file as a jar file, and now it can't write to the xml file. It worked before we exported it but in JAR format it doesn't work.
Where was the file located before you created the jar file?
lapickett lapickett

2017/11/8

#
On a dropbox folder, and now I've put the jar file in the same dropbox folder. If you are talking about the xml file, it was inside the project folder
lapickett lapickett

2017/11/8

#
Here is the code where it writes to the xml file:
import org.w3c.dom.*;
import javax.xml.*;
import javax.xml.transform.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.File;

/**
 * Write a description of class SaveData here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaveData {
    public static void addMoney(int money) {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse("./SaveData.xml");
            Element udata = doc.getDocumentElement();
            udata.normalize();
            Node mnode = udata.getElementsByTagName("money").item(0);
            int mony = Integer.parseInt(mnode.getTextContent())+money;
            mnode.setTextContent(""+mony);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("./SaveData.xml"));
            transformer.transform(source, result);
        } catch(Exception e) { e.printStackTrace(); }
    }
    public static void saveHat(String hat) {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse("./SteamPowered.jar/SaveData.xml");
            Element udata = doc.getDocumentElement();
            udata.normalize();
            NodeList hats = udata.getElementsByTagName(hat);
            if(hats.getLength() == 0 && !hat.equals("NoHat")) {
                Element xhat = doc.createElement(hat);
                xhat.appendChild(doc.createTextNode("true"));
                udata.appendChild(xhat);
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File("./SaveData.xml"));
                transformer.transform(source, result);
            }
        } catch(Exception e) { e.printStackTrace(); }
    }
    public static int getMoney() {
        int money = 0;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse("./SaveData.xml");
            Element udata = doc.getDocumentElement();
            udata.normalize();
            Node mnode = udata.getElementsByTagName("money").item(0);
            money = Integer.parseInt(mnode.getTextContent());
        } catch(Exception e) { e.printStackTrace(); }
        return money;
    }
    public static boolean getHat(String key) {
        NodeList hats;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse("./SaveData.xml");
            Element udata = doc.getDocumentElement();
            udata.normalize();
            hats = udata.getElementsByTagName(key);
        } catch(Exception e) { e.printStackTrace(); return false; }
        if(hats.getLength() == 0) {
            return false;
        }
        return true;
    }
}
danpost danpost

2017/11/8

#
Move the xml file to outside the project and adjust the code so that is locates it there before creating the jar file. Do the same with any other files your project makes changes to. You may want to create a new folder to contain both the new jar file and the xml as well the other files.
lapickett lapickett

2017/11/8

#
Is there any way to keep the xml file within the jar file itself?
danpost danpost

2017/11/8

#
lapickett wrote...
Is there any way to keep the xml file within the jar file itself?
Not (that I know of) if the project makes changes to the file. Well, maybe. The way to find out is by testing (and testing) -- finding out what actually is possible and what is not.
lapickett lapickett

2017/11/9

#
What about using preferences, I have seen where people have saved their high scores on scenarios using preferences, could it work with this situation as well? (if so, where can we find the information it stores within greenfoot) Thanks!
danpost danpost

2017/11/9

#
lapickett wrote...
What about using preferences, I have seen where people have saved their high scores on scenarios using preferences, could it work with this situation as well? (if so, where can we find the information it stores within greenfoot) Thanks!
I would not bother with that until the capability of using that storage with HTML 5 is ever resolved.
davmac davmac

2017/11/9

#
I would not bother with that until the capability of using that storage with HTML 5 is ever resolved.
I am not sure I understand this conversation. What is this "preferences"? Is that different from UserInfo storage? The latter should work fine with HTML5. (But, I'm not sure it's suitable for lapickett's needs here).
You need to login to post a reply.