Salvando dados em um arquivo XML

2 respostas
C

Olá, eu fiz um metodo para pegar diversas informações de uma interface drag’n drop e salvar em um XML. O problema é que quando adiciono vários componentes na interface, e tenho várias informações para ser salva, meu metodo salva só a última informação. Ele entra no for corretamente, mas vai sobreescrevendo. É a primeira vez que uso o JDOM, e trabalho com XML, não consegui encontrar o problema. O Código é o seguinte:

public static void save(String file, Component[] list,Vector<Point> connectionFrom, Vector<Point> connectionTo) throws IOException {
		
		Element tcp = new Element("TestCaseProject");
		Element components = new Element("Components");
		Element name = new Element("Name");
	    Element location = new Element("Location");
	    Element componentsX = new Element("X");
	    Element componentsY = new Element("Y");
	    
	    for (int i=0;i<list.length;i++) {
	    	components.setAttribute("id",Integer.toString(i+1));
	    	name.setText(list[i].getName());
	    	componentsX.setText(String.valueOf(list[i].getLocationOnScreen().getX()));
	    	componentsY.setText(String.valueOf(list[i].getLocationOnScreen().getY()));
		}
    	
	    if (connectionFrom.isEmpty()==false && connectionTo.isEmpty()==false) {
	    	Element connection = new Element("Connection");
		    Element connectionF = new Element("ConnectionFrom");
		    Element connectionFX = new Element("X");
		    connectionFX.setText(connectionFrom.toString().substring(connectionFrom.toString().indexOf("x=")+2,connectionFrom.toString().indexOf(",")));
		    Element connectionFY = new Element("Y");
		    connectionFY.setText(connectionFrom.toString().substring(connectionFrom.toString().indexOf("y=")+2,connectionFrom.toString().indexOf("]]")));
		    Element connectionT = new Element("ConnectionTO");
		    Element connectionTX = new Element("X");
		    connectionTX.setText(connectionTo.toString().substring(connectionTo.toString().indexOf("x=")+2,connectionTo.toString().indexOf(",")));
		    Element connectionTY = new Element("Y");
		    connectionTY.setText(connectionTo.toString().substring(connectionTo.toString().indexOf("y=")+2,connectionTo.toString().indexOf("]]")));
		    connection.addContent(connectionF);
		    connectionF.addContent(connectionFX);
		    connectionF.addContent(connectionFY);
		    connection.addContent(connectionT);
		    connectionT.addContent(connectionTX);
		    connectionT.addContent(connectionTY);
		    tcp.addContent(connection);
	    }
	    
	    location.addContent(componentsX);
	    location.addContent(componentsY);
	    name.addContent(location);
	    components.addContent(name);
	    tcp.addContent(components);
		Document document = new Document(tcp);

		
	    try{
	    	XMLOutputter out = new XMLOutputter();
	    	java.io.FileWriter writer = new java.io.FileWriter(file);

	    	out.output(document,writer);
	    	writer.flush();
	    	writer.close();
	    	
	    }catch(Exception e){
	    	e.getMessage();
	    	
	    }
	    	
	}

E o resultado é:

<?xml version="1.0" encoding="UTF-8" ?> 
- <TestCaseProject>
- <Components id="3">
- <Name>
       comp_TC_2 
     - <Location>
            <X>315.0</X> 
            <Y>87.0</Y> 
       </Location>
  </Name>
  </Components>
  </TestCaseProject>

Não importa quantos nomes eu tenho ou quantas posições, ele sempre salva a última. Alguém pode me ajudar nisso, tem alguma idéia.
Aguardo, desde já obrigado.

2 Respostas

_fs

Dentro do for você está criando e configurando apenas um conjunto Name/Location, e parece que você precisa de uma coleção desses elementos.

Um bom começo seria o for englobar as linhas 5 a 41.

C

Eu consegui resolver o problema hoje de manhã. Mas de qualquer forma obrigado pela resposta.
Abraço.

Criado 5 de maio de 2010
Ultima resposta 6 de mai. de 2010
Respostas 2
Participantes 2