Geração de Elemento XML

4 respostas
D

Olá pessoal, estou com um problema na geração de um arquivo xml. Ocorre que estou utilizando o jdom para geração de xml e quando tento gerar o elemento:

<lote nro="000012007" versao_sw="6.0" dtEmissao="DD/MM/AAAA" xmlns="http://www.portal.fucapi.br" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.portal.fucapi.br http://alvaraes.suframa.gov.br:7778/PMNRecEViewController/jsp/importardados/NF.xsd">

Utilizando para isto o seguinte código:

Element lote = new Element("lote");
        
         //"Setando" os atributos 
        Attribute nro = new Attribute("nro","1");  
        lote.setAttribute(nro); 
        
        //"Setando" outro atributo agora utilizando da classe Attribute  
        Attribute versao_sw = new Attribute("versao_sw","6.0");  
        lote.setAttribute(versao_sw);
        
        Attribute dtEmissao = new Attribute("dtEmissao",data);  
        lote.setAttribute(dtEmissao);
      
        Attribute xmlns = new Attribute("xmlns","http://www.portal.fucapi.br");  
        lote.setAttribute(xmlns);
        
        Attribute xmlns_xsi = new Attribute("xmlns:xsi=","http://www.w3.org/2001/XMLSchema-instance");  
        lote.setAttribute(xmlns_xsi);
        
        Attribute xsi_schemaLocation = new Attribute("xsi:schemaLocation=","http://www.portal.fucapi.br http://alvaraes.suframa.gov.br:7778/PMNRecEViewController/jsp/importardados/NF.xsd");  
        lote.setAttribute(xsi_schemaLocation);
Me é retornado o seguinte erro: [quote]The name "xmlns" is not legal for JDOM/XML attributes: An Attribute name may not be "xmlns"; use the Namespace class to manage namespaces.

Agradeço se alguém puder me dar uma ajuda com este problema.

4 Respostas

T

Siga o que ele lhe disse, e use a classe Namespace. No seu caso, o namespace é “http://www.portal.fucapi.br”.

Namespace ns = Namespace.getNamespace("http://www.portal.fucapi.br");
Element lote = new Element ("lote", ns);
...

Não olhei a documentação direito para saber se ele vai reclamar de “xmlns:xsi” também.

D
Olá thingol, obrigado pela dica, mas ao testar da seguinte maneira:
Namespace ns = Namespace.getNamespace("http://www.portal.fucapi.br");
        lote.setNamespace(ns);
        
        Namespace xsi = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema-instance");
        lote.setNamespace(xsi);
        
        Namespace schemaLocation = Namespace.getNamespace("http://www.portal.fucapi.br http://alvaraes.suframa.gov.br:7778/PMNRecEViewController/jsp/importardados/NF.xsd");
        lote.setNamespace(schemaLocation);
O arquivo foi gerado com o seguinte elemento:
<lote xmlns="http://www.portal.fucapi.br http://alvaraes.suframa.gov.br:7778/PMNRecEViewController/jsp/importardados/NF.xsd" nro="1" versao_sw="6.0" dtEmissao="13/02/2008">
Quando gero somente com o ns:
Namespace ns = Namespace.getNamespace("http://www.portal.fucapi.br");
        lote.setNamespace(ns);
O arquivo aparece com o elemento:
<lote xmlns="http://www.portal.fucapi.br" nro="1" versao_sw="6.0" dtEmissao="13/02/2008">
Preciso que ele apareça assim:
<lote nro="1" versao_sw="6.0" dtEmissao="13/02/2008" xmlns="http://www.portal.fucapi.br" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.portal.fucapi.br http://alvaraes.suframa.gov.br:7778/PMNRecEViewController/jsp/importardados/NF.xsd">
Agradeço se alguém puder me ajudar.
elcapote

Puts to precisando justamente disso alguem sabe???

uahuaha + simples impossivel…uhauha achei como era!

Element root = new Element(“projeto”);

root.addAttribute(new Attribute(“schemaLocation”,“xsi”,“http://www.w3.org/2001/XMLSchema-instance","http://maven.apache.org/maven-v4_0_0.xsd”));

N

É antigo... Mas para deixar uma resposta... Ó o meu exemplo:

final Namespace ns = Namespace.getNamespace("http://www.portalfiscal.inf.br/nfe");
final Namespace nsds = Namespace.getNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
final Namespace nsxsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

final Element enviNfe = new Element ("enviNFe", ns);
enviNfe.addNamespaceDeclaration(nsds);
enviNfe.addNamespaceDeclaration(nsxsi);
Criado 13 de fevereiro de 2008
Ultima resposta 2 de dez. de 2009
Respostas 4
Participantes 4