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
