CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #10
    Join Date
    Feb 2003
    Location
    Sunny Glasgow!
    Posts
    258

    Re: how to read XML file in Java?

    Hi, I recently implemented this for my Chess program that i have been writing on and off whenever i can be bothered.
    Fire this code into Eclipse and add the jar file to the classpath.

    Code:
    package com.al.export;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import com.al.board.Board;
    import com.al.board.Square;
    import com.al.game.BlackTeam;
    import com.al.game.WhiteTeam;
    import com.al.pieces.Bishop;
    import com.al.pieces.King;
    import com.al.pieces.Knight;
    import com.al.pieces.Pawn;
    import com.al.pieces.Queen;
    import com.al.pieces.Rook;
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    
    public class XMLConverter{
    	
    	public static boolean saveStringToFile(String fileName, String saveString) {
    		boolean saved = false;
    		BufferedWriter bw = null;
    		try {
    			bw = new BufferedWriter(new FileWriter(fileName));
    			try {
    				bw.write(saveString);
    				saved = true;
    			}
    			finally {
    				bw.close();
    			}
    		}
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    		return saved;
    	}
    	
    	public static String getStringFromFile(String fileName) {
    		BufferedReader br = null;
    		StringBuilder sb = new StringBuilder();
    		try {
    			br = new BufferedReader(new FileReader(fileName));
    			try {
    				String s;
    				while ((s = br.readLine()) != null) {
    					//add linefeed back since stripped by readline()
    					sb.append(s);
    					sb.append("\n");
    				}
    			}
    			finally {
    				br.close();
    			}
    		}
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    		return sb.toString();
    	}
    	
    	public static String convertToXML(Board board) {
    		XStream xstream = new XStream(new DomDriver());
    		xstream.setMode(XStream.ID_REFERENCES);
    		xstream.alias("board", Board.class);
    		xstream.alias("square", Square.class);
    		xstream.alias("whiteTeam", WhiteTeam.class);
    		xstream.alias("blackTeam", BlackTeam.class);
    		xstream.alias("pawn", Pawn.class);
    		xstream.alias("rook", Rook.class);
    		xstream.alias("knight", Knight.class);
    		xstream.alias("bishop", Bishop.class);
    		xstream.alias("queen", Queen.class);
    		xstream.alias("king", King.class);
    		return xstream.toXML(board);
    	}
    	
    	public static Board convertFromXML(String xMLString) {
    		Board board = null;
    		XStream xstream = new XStream(new DomDriver());
    		xstream.setMode(XStream.ID_REFERENCES);
    		xstream.alias("board", Board.class);
    		xstream.alias("square", Square.class);
    		xstream.alias("whiteTeam", WhiteTeam.class);
    		xstream.alias("blackTeam", BlackTeam.class);
    		xstream.alias("pawn", Pawn.class);
    		xstream.alias("rook", Rook.class);
    		xstream.alias("knight", Knight.class);
    		xstream.alias("bishop", Bishop.class);
    		xstream.alias("queen", Queen.class);
    		xstream.alias("king", King.class);
    		Object obj = xstream.fromXML(xMLString);
    		if (obj instanceof Board) {
    			board = (Board) obj;
    		}
    		return board;
    	}
    	
    	public static boolean saveBoardToXMLFile(String fileName, Board board) {
    		return saveStringToFile(fileName, convertToXML(board));
    	}
    	
    	public static Board getBoardFromXMLFile(String fileName) {
    		return convertFromXML(getStringFromFile(fileName));
    	}
    	
    	public static boolean saveBoardToSerialFile(String fileName, Board board) {
    		boolean saved = false;
    		try {
    			ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
    			try {
    				oos.writeObject(board);
    				saved = true;
    			}
    			finally {
    				oos.close();
    			}
    		}
    		catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		return saved;
    	}
    
    	public static Board getBoardFromSerialFile(String fileName) {
    		Board board = null;
    		try {
    			ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(fileName)));
    			try {
    				Object obj = ois.readObject();
    				if (obj instanceof Board) {
    					board = (Board) obj;
    				}
    			}
    			finally {
    				ois.close();
    			}
    		}
    		catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		return board;
    	}
    }
    There are obviously references to Pieces, Boards etc. which you would replace with your classes, but I'll leave that to you.
    Attached Files Attached Files
    Last edited by themoffster; June 8th, 2008 at 01:16 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured