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.
[code]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();
}
}
}[/code]
Daniel Mafinski Biz
