cancel
Showing results for 
Search instead for 
Did you mean: 

Problem: XML tag value not right

valfontanette
Champ in-the-making
Champ in-the-making
HI

I am having trouble to get the right content of a XML file. Just because the value of initial tags are not right.
Here is what I mean.



<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
   <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
      <infNFe versao="2.00" Id="NFe35140404108518000102550020000957691354988086">
         <ide>
         …
         </ide>
      
      …
         </infNFe>
   …
   </NFe>
   …
</nfeProc>   



I've used this to remove XML first line.


var content = new XML(node.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", ""));


It works fine.

Now I need to remove the "xmlns" values and the "versao="2.00"" from initial tags.
I need the tags to be like that:


<nfeProc>
    <NFe>
   <infNFe>
   


I've tried a lot of things, but not have success.
The last one I have got somethig was that:



//Remove first line
document.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", "");
var fileContent = new XML(document);

//Remove nfeproc xmls
document.content.replaceAll("(?s)<\\?nfeProc xmlns .*?\\?>\\s*", "<nfeProc>");  //I got this result: <nfeProc versao="2.00">

//Remove nfe xmls
document.content.replaceAll("(?s)<\\?NFe xmlns .*?\\?>\\s*", "<NFe>"); //It works fine:  <NFe>

//Remove infNFe Id
document.content.replaceAll("(?s)<\\?infNFe Id .*?\\?>\\s*", "<infNFe>"); //Do not work



Any help??
10 REPLIES 10

valfontanette
Champ in-the-making
Champ in-the-making
Hi,

I finally got it right.
I find out some of my xml files have a problem and because of that my code didn't work before.

My right code is now like this:



logger.getSystem().out("####BEGIN  #####  ");
var xml = new XML(document.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", ""));

default xml namespace="http://www.portalfiscal.inf.br/nfe"; //to solve xmls parameter
var nfe = xml.child(0);
var infNFe= nfe.child(0);

delete infNFe.@versao; // delete infNFe versao attribte
delete infNFe.@Id;
delete nfe.@xmlns;
delete xml.@versao;
delete xml.@xmlns;

logger.getSystem().out("#### XML CONTENT  #####  " + xml);

var cnpj= xml.NFe.infNFe.dest.CNPJ;
var empresa= xml.NFe.infNFe.dest.xNome;
var competencia = xml.NFe.infNFe.ide.dEmi;
var cnpj_fornecedor = xml.NFe.infNFe.emit.CNPJ;
var fornecedor = xml.NFe.infNFe.emit.xNome;