dúvida na construção de xml

4 respostas
R

implementei a seguinte classe:

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

/**

  • Classe responsável por gerar o arquivo XML

*/
public class PlacaXML {

private Document root;
private DocumentBuilderFactory fabrica;
private DocumentBuilder builder;

	    
public PlacaXML()throws ParserConfigurationException
{
	fabrica = DocumentBuilderFactory.newInstance();
	builder = fabrica.newDocumentBuilder();     
}
		    
 /**
* Constroi o XML 
*
*/
public Document getXMLplacaConsulta( Veiculo placa )
{
	 root = builder.newDocument(); 
	 Element tagplacaInsert = root.createElement( "PLACAS" );

	 tagplacaInsert.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
	 tagplacaInsert.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:noNamespaceSchemaLocation", "/placaInsert.xsd");
	 root.appendChild( tagplacaInsert );
    
	 Element tagPlaca = root.createElement( "PLACA_VEI" );
	 tagplacaInsert.appendChild( tagPlaca );
	         
	 Element info = root.createElement( "PLACA" );
	 info.appendChild( root.createTextNode( placa.getPlaca()));
	 tagplacaInsert.appendChild( info );
		         
	 info = root.createElement( "MUNICIPIO_EMPLACAMENTO" );
	 info.appendChild( root.createTextNode( placa.getMunicipioEmplacamento() ));
	 tagplacaInsert.appendChild( info );
	 
	 info = root.createElement( "UF_EMPLACAMENTO" );
	 info.appendChild( root.createTextNode( placa.getUfEmplacamento()));
	 tagplacaInsert.appendChild( info );
	 
	 return root;
	         
}

}

e ela após imprimir o xml gerado resultou em :

<?xml version="1.0" encoding="UTF-8"?>

<PLACAS xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation="/placaInsert.xsd"><PLACA_VEI/><PLACA>gra1234</PLACA><MUNICIPIO_EMPLACAMENTO>BELO HORIZONTE </MUNICIPIO_EMPLACAMENTO><UF_EMPLACAMENTO>MG</UF_EMPLACAMENTO></PLACAS>

sendo que a tag <PLACA_VEI/> que é filha de <PLACAS> deveria ter aparecido da seguinte forma no exemplo modificado abaixo:

<?xml version="1.0" encoding="UTF-8"?>

<PLACAS xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation="/placaInsert.xsd"><PLACA_VEI><PLACA>gra1234</PLACA><MUNICIPIO_EMPLACAMENTO>BELO HORIZONTE </MUNICIPIO_EMPLACAMENTO><UF_EMPLACAMENTO>MG</UF_EMPLACAMENTO><PLACA_VEI/></PLACAS>

onde foi que eu errei?

Grato,

Paulo

4 Respostas

black_fire

Kra,

Não dei uma olhada no seu código, isso é só uma dica: :?
Tenho usado esses componentes da Jakarta para trabalhar com XML e tem sido muito mais fácil que fazer com DOM na unha.

http://jakarta.apache.org/commons/betwixt/
http://jakarta.apache.org/commons/digester/

Falow kra…
Espero que ajude.

_fs

As sugestões acima são boas, e também tem esse outro aqui:
http://xstream.codehaus.org/

Olha o código fonte para ver como o cara implementou essa funcionalidade :smiley:

javaAurelio

Vou ainda testar o codigo e ver se da certo
Estavo procurando um codigo assim para fazer um db xml

OK :idea:

Pedrosa

Sugiro o Xtream, estamos obtendo ótimos resultados com ele, a parte dele de referência mata a pau:

http://xstream.codehaus.org/graphs.html

Criado 2 de dezembro de 2004
Ultima resposta 28 de fev. de 2007
Respostas 4
Participantes 5