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: http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
    Now I still have an error I can't get rid of, namely a nullpointerexeption.

    I want the following xml setup:
    Code:
    <world id="world_world">
      <city id="mayor_Jeroen">
      </city>
    </world
    I run the following code in the "onEnable()":
    Code:java
    1. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    2. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    3. Document doc = docBuilder.newDocument();
    4.  
    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:
    Code:java
    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:
    Code:java
    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:
    http://wiki.bukkit.org/Configuration_API_Reference

    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