Thread: Loading values from a XML file

Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1 Loading values from a XML file 
    Registered Member
    Join Date
    Feb 2007
    Posts
    308
    Thanks given
    0
    Thanks received
    1
    Rep Power
    72
    I made this for a friend but he didn't like it so ill release here. What this does is it loads values from a XML file, its basically the same thing as a "cfg file" but its faster and more efficient. I also documented this out of boredom. Make a new class file named ServerConf and paste this inside:
    Code:
    import java.util.Properties;
    import java.io.IOException;
    import java.io.FileInputStream;
    
    /**
     * This class loads different values for various
     * variables/methods for the server from a XML file.
     *
     * @arthur Samuraiblood2
     */
    
    public class ServerConf {
    
    	Properties props;
    	String dir = "./xml/";
    	String file = "settings.xml";
    	
    	/**
    	 * Uses the defualt file and directory to load 
    	 * the values. The default directory is <code>./xml/</code> and 
    	 * the default file name is <code>settings.xml</code>.
    	 */
    	public ServerConf() {
    		props = new Properties();
    		try {
    			FileInputStream in = new FileInputStream(dir + file);
    			props.loadFromXML(in);
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Sets the files directory and the files name then reads any
    	 * and all values from the specified file.
    	 *
    	 * @param dir The location of the file.
    	 * @param file The name of the file.
    	 */
    	public ServerConf(String dir, String file) {
    		this.dir = dir;
    		this.file = file;
    		props = new Properties();
    		try {
    			FileInputStream in = new FileInputStream(dir + file);
    			if(file.endsWith("xml")) {				
    				props.loadFromXML(in);
    			} else {
    				props.load(in);
    			}
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Gets the name of the servers owner.
    	 *
    	 * @return The servers owners name.
    	 */
    	 public String getOwner() {
    	 	String owner = props.getProperty("Owner");
    	 	return owner;
    	 }
    
    	/**
    	 * Gets the name of the server.
    	 *
    	 * @return The server name.
    	 */
    	public String getServer() {
    		String server = props.getProperty("Server");
    		return server;
    	}
    
    	/**
    	 * Gets the startup message for the server.
    	 *
    	 * @return The message that will be printed.
    	 */
    	public String getStartMsg() {
    		String msg = props.getProperty("Msg");
    		return msg;
    	}
    
    	/**
    	 * Gets the servers port.
    	 *
    	 * @return The servers port.
    	 */
    	public int getServPort() {
    		int port = Integer.parseInt(
    				props.getProperty("Port"));
    		return port;
    	}
    }
    Now, make a folder called xml and make a file named settings.xml, inside that file paste this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
       <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
       <properties>
         <entry key="KEYTYPE">VALUE</entry>
       </properties>
    Replace KEYTYPE with the keytype obviously and replace VALUE with a value.
    RuneWiki - The ultimate source of technical RuneScape information.
     

  2. #2  
    Registered Member

    Join Date
    May 2007
    Posts
    696
    Thanks given
    0
    Thanks received
    0
    Rep Power
    154
    What can we use this for?

    Rep++
    Anybody remember me?
     

  3. #3  
    Stanyer
    Guest
    Npcs, items, etc.
     

  4. #4  
    project-rs owner
    Join Date
    Sep 2006
    Age
    32
    Posts
    914
    Thanks given
    4
    Thanks received
    4
    Rep Power
    49
    thanks want to learn XML soon so i saved it


    edit: need to spread r3p before giving you again

    will do that later
     

  5. #5  
    Pandora
    Guest
    Quote Originally Posted by Pkitten View Post
    What can we use this for?

    Rep++
    why the hell do you rep if u dont know were to use it for(aka u don't use it?)

    nice one samurai
     

  6. #6  
    Registered Member

    Join Date
    May 2007
    Posts
    696
    Thanks given
    0
    Thanks received
    0
    Rep Power
    154
    Quote Originally Posted by Pandora View Post
    why the hell do you rep if u dont know were to use it for(aka u don't use it?)

    nice one samurai
    Because its look good, and I would have used it anyway.
    Anybody remember me?
     

  7. #7  
    PeeHPee
    Guest
    The one I made pwnz that one.
     

  8. #8  
    Stanyer
    Guest
    Quote Originally Posted by PeeHPee View Post
    The one I made pwnz that one.
    And what?

    You're a ***.
    Received Infraction 
     

  9. #9  
    PeeHPee
    Guest
    Quote Originally Posted by Stanyer View Post
    And what?

    You're a ***.
    Lol, I was talking about the one I put in OUR Project, so **** off.

    Wow, coming from the owner of "PkScape"

    EDIT:
    Also, Samurai why not do;

    Code:
         /**
             * Loads the config from a file called "Configuration" which uses XML To
             * parse
             */
        public static void loadConfig() throws IOException {
    	final Properties props = new Properties();
    	props.loadFromXML(new FileInputStream("Configuration.xml"));
    	listenerPort = Integer.parseInt(props.getProperty("port"));
    
        }
    
        /**
             * The int we use to get the listenerPort
             */
        public static int listenerPort;
        static {
    	try {
    	    loadConfig();
    	} catch (final Exception e) {
    	    System.out.println(e);
    	}
        }
    Less code
     

  10. #10  
    Registered Member
    Join Date
    Feb 2007
    Posts
    308
    Thanks given
    0
    Thanks received
    1
    Rep Power
    72
    Quote Originally Posted by Pkitten View Post
    What can we use this for?

    Rep++
    Well there are some premade methods for you that can get the servers port, owner, name, and the start up message for when a player logs in.

    Quote Originally Posted by PeeHPee View Post
    The one I made pwnz that one.
    I can make one better but i cba to release anything good on this community.
    RuneWiki - The ultimate source of technical RuneScape information.
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •