Gerando XML com DOM

3 respostas
pix

Estou enfrentando alguns problemas na geração de um XML com o DOM.

1 - Não sei como criar um cabeçalho, algo como
<?xml version="1.0" encoding="UTF-8"?>
2 - No element NFE ele não seta o atributo
NFe.setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
3 - no element infNFe ele ordena por ondem alfabetica, quero que fique como no código.

Se alguém puder ajudar ficarei grato, segue o código.

public class xmlCabecMsg {
    public static void main(String[] args) {
        new xmlCabecMsg();
    }
    
    public xmlCabecMsg() {
        try {
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            
            
            Element enviNFe = doc.createElement("enviNFe");
            enviNFe.setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
            enviNFe.setAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
            enviNFe.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            enviNFe.setAttribute("versao", "1.07");
            doc.appendChild(enviNFe);
            
            Element idLote = doc.createElement("idLote");
            
            enviNFe.appendChild(idLote);
            
            Text textoidLote = doc.createTextNode("000000000001538");
            idLote.appendChild(textoidLote);
            
            Element NFe = doc.createElement("NFe");
            NFe.setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
            enviNFe.appendChild(NFe);
            
            Element infNFe = doc.createElement("infNFe");
            infNFe.setAttribute("versao", "1.07");
            infNFe.setAttribute("id", "NFe43060992665611012850550079000000011485651995");
            NFe.appendChild(infNFe);
            
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();
            
            System.out.println(xmlString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Daniel Mafinski Biz

3 Respostas

lilirc

Para o cabeçalho adicione a seguinte linha no seu código:

trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

Espero ter ajudado… :wink:

pix

Cara, ajudo, obrigado.

E sobre a ordenação não ser por ordem alfabetica, alguém tem algo?

No cabeçalho surgiu um standalone=“no” que não pode estar lá também.

L

Olá pessoal,

Consegui gerar o xml usando o DOM, só que ele não está identando certo…

Ele até pula as linhas, só q não coloca o nó filho pra frente…

Alguém tem alguma solulção??

Obrigado!

Criado 11 de novembro de 2008
Ultima resposta 15 de jan. de 2009
Respostas 3
Participantes 3