PROBLEM WITH PARENT TAG in VBS

Asked

Viewed 46 times

1

So people... all right? I have a little problem here very similar to the one I had yesterday, I have a VBS script that suits me well, it basically reads the XML, takes the date inside the XML and renames the file with this date...

Now I need it to rename XML with CNPJ followed by date, but the problem is that the CNPJ tag is not unique, there are several Cnpjs in xml... I just need the CNPJ inside the tag, I was able to make it work really well here, but there’s some XML that I get that order changes, so how does it get the first CNPJ tag that it has, sometimes it gets the CNPJ from another tag other than the . Could you help me? Follow below the codes:

Excerpt from the XML:

<?xml version = "1.0" encoding = "ISO-8859-1"?>
<cteProc versao="3.00" xmlns="http://www.portalfiscal.inf.br/cte">
    <CTe xmlns="http://www.portalfiscal.inf.br/cte">
        <infCte versao="3.00" Id="CTe33180702012862001050570060001000081465016982">
            <ide>
                <cUF>33</cUF>
                <cCT>sad1698</cCT>
                <CFOP>6asd351</CFOP>
                <natOp>PREST. SERV DE TRANSP. PARA EXECUÇÃO SERV. DA MESMA NATUREZA</natOp>
                <mod>57</mod>
                <serie>6</serie>
                <nCT>10s0008</nCT>
                <dhEmi>2018-07-17T15:37:34-03:00</dhEmi>
                <tpImp>1</tpImp>
                <tpEmis>1</tpEmis>
                <cDV>2</cDV>
                <tpAmb>1</tpAmb>
                <tpCTe>0</tpCTe>
                <procEmi>0</procEmi>
                <verProc>Triangulus_2.0.28.48</verProc>
                <cMunEnv>3304557</cMunEnv>
                <xMunEnv>RIO DE JANEIRO</xMunEnv>
                <UFEnv>RJ</UFEnv>
                <modal>02</modal>
                <tpServ>0</tpServ>
                <cMunIni>3304557</cMunIni>
                <xMunIni>RIO DE JANEIRO</xMunIni>
                <UFIni>RJ</UFIni>
                <cMunFim>3518800</cMunFim>
                <xMunFim>GUARULHOS</xMunFim>
                <UFFim>SP</UFFim>
                <retira>0</retira>
                <xDetRetira>AEROPORTO</xDetRetira>
                <indIEToma>1</indIEToma>
                <toma3>
                    <toma>3</toma>
                </toma3>
            </ide>
            <compl>
                <xCaracSer>PRÓXIMO DIA</xCaracSer>
                <xEmi>Tauany Peçanha Ayre</xEmi>
                <fluxo>
                    <xOrig>SDU</xOrig>
                    <xDest>GRU</xDest>
                </fluxo>
                <destCalc>3518800</destCalc>
                <xObs>ICMS CONFORME RESOLU??O DO SENADO FEDERAL 95/96</xObs>
                <ObsCont xCampo="TIPO DE PAGAMENTO">
                    <xTexto>CONTA CORRENTE - 638877SAO - 19.89</xTexto>
                </ObsCont>
                <ObsCont xCampo="CONTA CORRENTE">
                    <xTexto>638877SAO</xTexto>
                </ObsCont>
                <ObsCont xCampo="FINANCIERO">
                    <xTexto>Lei da transparência 12.741/12, o percentual aproximado dos tributos incidentes sobre o preço do serviço são: Federal: 15,96% Estadual: 4,0%</xTexto>
                </ObsCont>
            </compl>
            <emit>
                <CNPJ>02012862001050</CNPJ>   <---- PRECISO QUE SEJA ESSE CNPJ EM ESPECIFICO
                <IE>84328820</IE>
                <xNome>TAM LINHAS AEREAS SA SDU</xNome>
                <enderEmit>
                    <xLgr>PC SENADOR SALGADO FILHO</xLgr>
                    <nro>0</nro>
                    <xCpl>AEROPORTO SANTOS DUMONT</xCpl>
                    <xBairro>AEROPORTO SANTOS DUMONT</xBairro>
                    <cMun>3304557</cMun>
                    <xMun>RIO DE JANEIRO</xMun>
                    <CEP>20021340</CEP>

VBS file:

DiretorioobjFiles = "C:\DACTE\"
DiretorioDestino = "C:\DACTE\DATA\"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(DiretorioobjFiles)
Wscript.Echo objFolder.Path

Set colFiles = objFolder.Files


For Each objFile in colFiles

On Error Resume Next
If Right(objFile.FileName, 4) = ".xml" Then

    Set objParser = CreateObject("Microsoft.XMLDOM")
    objParser.Load (objFile.path)

    oldFileName = objFile.path

    Set ElemList = objParser.getElementsByTagName("emit").getElementsByTagName("CNPJ")
    CNPJ = ElemList.Item(0).Text

    Set ElemList = objParser.getElementsByTagName("dhEmi")
    dhEmi = Replace(ElemList.Item(0).Text, ":", "_")

    newFileName = DiretorioDestino + CNPJ + "_" + dhEmi + ".xml"

    fso.MoveFile oldFileName, newFileName
End If
Next
  • 1

    Mario, I don’t know about VBS but I decided to give a "search" around. What if you used the command: Set ElemList = objParser.getElementsByTagName("emit") to get to the "Emit" tag and use the command CNPJ = ElemList.FirstChild.Text - the idea is to catch the first child after the tag "Emit". Maybe I can help...

  • I did several tests here with firstchild and it either didn’t work or ended up just converting the first item of my xml... :(

  • But thanks for the tip!!!

1 answer

2

I managed to solve staff, in case someone needs something similar follow the code:

DiretorioobjFiles = "C:\DACTE\"
DiretorioDestino = "C:\DACTE\DATA\"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(DiretorioobjFiles)
Wscript.Echo objFolder.Path

Set colFiles = objFolder.Files


For Each objFile in colFiles

On Error Resume Next
If Right(objFile.FileName, 4) = ".xml" Then

    Set objParser = CreateObject("Microsoft.XMLDOM")
    objParser.Load (objFile.path)

    oldFileName = objFile.path

    Set ElemList = objParser.getElementsByTagName("./emit/CNPJ")
    CNPJ = ElemList.Item(0).Text

    Set ElemList = objParser.getElementsByTagName("dhEmi")
    dhEmi = Replace(ElemList.Item(0).Text, ":", "_")

    newFileName = DiretorioDestino + CNPJ + "_" + dhEmi + ".xml"

    fso.MoveFile oldFileName, newFileName
End If
Next

Browser other questions tagged

You are not signed in. Login or sign up in order to post.