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

mitpatoliya
Star Collaborator
Star Collaborator
may be you need to use

document.content.content= document.content.content.replaceAll("(?s)<\\?infNFe Id .*?\\?>\\s*", "<infNFe>");
document.save();

and also remember to save the document.

Hi,

Thanks for your attention!
I tried out what you suggested, but I got some errors so I don't know if I did it right.

I tried this.

document.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", "");
document.content.content= document.content.content.replaceAll("(?s)\\s*", "");
document.save();


It returns TypeError: Cannot call method "replaceAll" of undefined


I tried this.

document.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", "");
document.content.content.replaceAll("(?s)\\s*", "");
document.save();


It returns TypeError: Cannot call method "replaceAll" of undefined


I tried this.

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


It returns TypeError: Cannot call method "replaceAll" of undefined


I tried this

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

It returns: TypeError: Cannot find function replaceAll.


Am I doing wrong?

Thanks,

kaynezhang
World-Class Innovator
World-Class Innovator
Why not just operate your xml using e4x? you can remove xml attribute using delete command.

var content = new xml(fileContent);
delete content.@xmlns; //delete nfeProc xmlns attribute
delete content.infNFe.@versao // delete infNFe versao attribte

Hi,

Thanks for you attention.

I've tried this, but it returns the same result I had before. It deletes xmls, but do not delete versao.

I got this:


<nfeProc versao="2.00">
   <NFe>
     <infNFe Id="NFe35140404108518000102550020000957691354988086" versao="2.00">




But I need to get to this:



<nfeProc>
   <NFe>
     <infNFe>



It deletes only xmls. Why doesn't work okay with "versao" and "Id"???
I've tried so many ways and nothing works.

I am attaching my xml file.

Any clues?

kaynezhang
World-Class Innovator
World-Class Innovator
Try this

var fileContent = document.content;
var xml = new XML(fileContent);
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 nfeProc  versao attribute
delete xml.@xmlns; //delete nfeProc  xmlns attribute

Hi kaynezhang, Thanks for your help!

I tried it and got the same result. It didn't take of versao and Id.

I am sending a picture with it.

I don't know whatelse to try. I have the documentation e4X but I can't make it works.

Thanks again.

kaynezhang
World-Class Innovator
World-Class Innovator
I have just tested in my environmenet.
It is surprising that versao and Id attribute had been removed correctly,but xmls not,the result is opposite to yours ,I'll check why.

Hi kaynezhang,

I think I am doing something wrong on trying this.

I just figured out that: If

I set up this script on a folder;
I upload a xml file to that folder and that runs the script;
So, to see if it worked I opened the uploaded xml file;
But, I figured that the xml file is always the same. Is has no xmlns and has versao and Id even If my code didn't tell to take it of.

So, I tried to print the xml content and a specifc tag. But it prints the complete xml file (with xml (first line) xmlns, versao and Id).

I did like this:



var fileContent = document.content;

var xml = new XML(fileContent);

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 nfeProc  versao attribute

delete xml.@xmlns; //delete nfeProc  xmlns attribute



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

var cnpj = xml.NFe.infNFe.dest.CNPJ;

logger.getSystem().out("#### CNPJ value is ####  " +cnpj);




Am I doing something wrong to see if it works? How did you do it?

Thanks a lot!

Any clues???

I coudn't make this work. I have no idea whatelse to try.

Anyone?!?

Thanks,

Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.