Solved Creating XML File

Discussion in 'Plugin Development' started by jeroen_JDOG, Apr 16, 2014.

Thread Status:
Not open for further replies.
  1. Hello,
    To save data in my plugin I want to use an xml file.
    I've read the following tutorial on how to create an xml file in Java:
    Now I still have an error I can't get rid of, namely a nullpointerexeption.

    I want the following xml setup:
    <world id="world_world">
      <city id="mayor_Jeroen">
    I run the following code in the "onEnable()":
    1. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    2. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    3. Document doc = docBuilder.newDocument();
    5. dataFolder.mkdir();
    6. xmlFile = new File(dataFolder, "save.xml");

    Of course, with a try/catch for ParserConfigurationException.

    Now the error comes if I want to run the following method:
    1. public static void addCity(Player mayor, World world) {
    2. Element city = doc.createElement("city");
    3. if (doc.getElementById("world_" + world.toString()) == null) {
    4. addWorld(world);
    5. }
    6. doc.getElementById("world_" + world.toString()).appendChild(city); // This line is where the nullpointerexception is thrown
    7. city.setAttribute("id", "mayor_" + mayor.getName());
    8. saveToFile();
    9. }

    This saveToFile() method contains the following, again with try/catch:
    1. TransformerFactory transformerFactory = TransformerFactory.newInstance();
    2. Transformer transformer = transformerFactory.newTransformer();
    3. DOMSource source = new DOMSource(doc);
    4. StreamResult result = new StreamResult(xmlFile);
    5. transformer.transform(source, result);

    Now I can't figure out what I am doing wrong...
    Can anyone help me?
    Thanks in advace,
    Jeroen D.

    Solved it by using a YML file instead of XML file.
    For anyone else needing a tutorial on YML:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page