Boa tarde pessoal,
Estou novamente lutando contra o org.w3c.Document e estou apanhando de novo…
Meu problema é o seguinte:
Possuo um org.w3c.Document populado e criei um novo.
Estou importando alguns nós do Document populado para o meu novo Document, mas infelizmente em alguma parte do processo o parser está se entromentendo e colocando xmlns em todos os Element.
Meu codigo é o seguinte:
public Document createEmptyDocument()
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
dbf.setNamespaceAware(true);
try
{
doc = dbf.newDocumentBuilder().newDocument();
}
catch(ParserConfigurationException pce)
{
logger.error("Erro ao criar documento vazio", pce);
}
return doc;
}
Document protNFeDocument = createEmptyDocument();
Element protNFeElement = (ElementImpl)protNFeDocument.createElement("protNFe");
protNFeElement.setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
protNFeElement.setAttribute("versao", "1.10");
Element newInfProtElement = protNFeDocument.createElement("infProt");
//Element infProtElement = (ElementImpl)protDocument.getElementsByTagName("infProt").item(0);
//Element newInfProtElement = (ElementImpl)protNFeDocument.importNode(infProtElement, true);
Element tpAmbElement = (ElementImpl)protDocument.getElementsByTagName("tpAmb").item(0);
Element newTpAmbElement = (ElementImpl)protNFeDocument.importNode(tpAmbElement, true);
newTpAmbElement.removeAttribute("xmlns");
Element verAplicElement = (ElementImpl)protDocument.getElementsByTagName("verAplic").item(0);
Element newVerAplicElement = (ElementImpl)protNFeDocument.importNode(verAplicElement, true);
Element chNFeElement = (ElementImpl)protDocument.getElementsByTagName("chNFe").item(0);
Element newChNFeElement = (ElementImpl)protNFeDocument.importNode(chNFeElement, true);
Element dhRecbtoElement = (ElementImpl)protDocument.getElementsByTagName("dhRecbto").item(0);
Element newDhRecbtoElement = (ElementImpl)protNFeDocument.importNode(dhRecbtoElement, true);
Element nProtElement = (ElementImpl)protDocument.getElementsByTagName("nProt").item(0);
Element newNProtElement = (ElementImpl)protNFeDocument.importNode(nProtElement, true);
Element digValElement = (ElementImpl)protDocument.getElementsByTagName("digVal").item(0);
Element newDigValElement = (ElementImpl)protNFeDocument.importNode(digValElement, true);
Element cStatElement = (ElementImpl)protDocument.getElementsByTagName("cStat").item(0);
Element newCStatElement = (ElementImpl)protNFeDocument.importNode(cStatElement, true);
Element xMotivoElement = (ElementImpl)protDocument.getElementsByTagName("xMotivo").item(0);
Element newXMotivoElement = (ElementImpl)protNFeDocument.importNode(xMotivoElement, true);
newInfProtElement.appendChild(newTpAmbElement);
newInfProtElement.appendChild(newVerAplicElement);
newInfProtElement.appendChild(newChNFeElement);
newInfProtElement.appendChild(newDhRecbtoElement);
newInfProtElement.appendChild(newNProtElement);
newInfProtElement.appendChild(newDigValElement);
newInfProtElement.appendChild(newCStatElement);
newInfProtElement.appendChild(newXMotivoElement);
protNFeElement.appendChild(newInfProtElement);
protNFeDocument.appendChild(protNFeElement);
protNFeDocument.normalize();
O resultado esperado seria:
<protNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.10">
<infProt>
<tpAmb>2</tpAmb>
<verAplic>SP_NFE_PL_005e</verAplic>
<chNFe>35100301993426000156550040000055701474551351</chNFe>
<dhRecbto>2010-05-03T16:59:49</dhRecbto>
<nProt>135100019231072</nProt>
<digVal>5kaGt9wDjZjIIOtat2FnMdRL6zM=</digVal>
<cStat>100</cStat>
<xMotivo>Autorizado o uso da NF-e</xMotivo>
</infProt>
</protNFe>
O resultado obtido é:
<protNFe versao="1.10" xmlns="http://www.portalfiscal.inf.br/nfe">
<infProt>
<tpAmb xmlns="http://www.portalfiscal.inf.br/nfe">2</tpAmb>
<verAplic xmlns="http://www.portalfiscal.inf.br/nfe">SP_NFE_PL_005e</verAplic>
<chNFe xmlns="http://www.portalfiscal.inf.br/nfe">35100301993426000156550040000055701474551351</chNFe>
<dhRecbto xmlns="http://www.portalfiscal.inf.br/nfe">2010-05-03T16:59:49</dhRecbto>
<nProt xmlns="http://www.portalfiscal.inf.br/nfe">135100019231072</nProt>
<digVal xmlns="http://www.portalfiscal.inf.br/nfe">5kaGt9wDjZjIIOtat2FnMdRL6zM=</digVal>
<cStat xmlns="http://www.portalfiscal.inf.br/nfe">100</cStat>
<xMotivo xmlns="http://www.portalfiscal.inf.br/nfe">Autorizado o uso da NF-e</xMotivo>
</infProt>
</protNFe>
Alguem já venceu esta batalha ?
Grato pela atenção,
Estevan Diedrich