none
Script to scan XML file for info RRS feed

  • Question

  • All,

    I have run into an issue with some code that I have.  The code scans an XML file and pulls some information out of it.  It was working for a while and then I started getting the error below.  I know that I should check to see if object exists first, but the objects are there, so my bigger issue is why the script is not finding it.

    ---------------------------
    Windows Script Host
    ---------------------------
    Script: D:\IFC\Load-Xref.vbs
    Line: 92
    Char: 61
    Error: Object required: 'objNode.Attributes.getNamedItem(...)'
    Code: 800A01A8
    Source: Microsoft VBScript runtime error

    ---------------------------
    OK   
    ---------------------------

    Code:

    'This code goes through all archived XML files and pulls out the needed information to be loaded into 
    'the Invoice table of the database
    '****************************************************************************************************
    filex = "xml"
    ifcfolder = "D:\IFC\" & trim(Plant) & "\Archive"

    Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
    If FSO.FolderExists(ifcfolder) Then
            Set iFolder = FSO.GetFolder(ifcfolder)
    Else 
          Msgbox("Folder " & ifcfolder & " does not exist")
          WScript.Quit
    End If
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("D:\IFC\" & trim(Plant) & "-Inv.txt", True)

    For Each f2 In fso.GetFolder(ifcfolder).Files
          Do
          Inv_Total = 0
          Inv_Date = ""
          Inv_Number = ""
          Series = ""
                        
          Set xmlDoc = CreateObject("Microsoft.XMLDOM")

          xmlDoc.Async = "True"
          xmlDoc.Load(f2.path)
                                                 
          Set colNodes=xmlDoc.selectNodes("cfdi:Comprobante")

          For Each objNode in colNodes
                Inv_Total = objNode.Attributes.getNamedItem("total").Text
                Inv_Date = objNode.Attributes.getNamedItem("fecha").Text
                Inv_Number = objNode.Attributes.getNamedItem("folio").Text
                Series = objNode.Attributes.getNamedItem("serie").Text
          Next
          a.WriteLine (Inv_Total & "," & Inv_Date & "," & Inv_Number & "," & Series)
          Loop While False
    Next      

    Part of file where data is taken:

    <?xml version="1.0" encoding="utf-8"?><cfdi:Comprobante Version="3.3" Serie="K" Folio="10" Fecha="2017-11-28T17:31:18" FormaPago="99" NoCertificado="00001000000400460285" CondicionesDePago="TR 20th of following month" SubTotal="753.92" Descuento="0" Moneda="USD" TipoCambio="18.52000" Total="874.55" TipoDeComprobante="I" MetodoPago="PPD" LugarExpedicion="37668" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">
    </cfdi:Comprobante>

    Thanks!

    David

    • Moved by Bill_Stewart Monday, March 12, 2018 8:33 PM This is not "fix/debug/rewrite my script for me" forum
    Saturday, January 20, 2018 4:16 PM

All replies

  • The XML posted has nothing to do with the code  I recommend asking the original author to help you with this.  There is not way for us to guess at what is wrong.   I suspect the XML has changed and no longer matches the code.


    \_(ツ)_/

    Saturday, January 20, 2018 4:40 PM
  • I do not understand.  What is it about the XML that indicates that it has nothing to do with the code?

    I have provided the code that is used the segment of file that was read and the error.  

    BTW I am the one that wrote the original script.  It was working.  I will look at the previous XML files.  

    I am at a loss here.  I feel as though your response adds more confusion to me.

    If there is something more that you need to help, please let me know.

    Saturday, January 20, 2018 6:44 PM
  • I have reviewed the old code and it looks as if NameSpace has been introduced.  I am not sure how this is read in differently than the previous XML format.  

    If there are links to how this is done, please let me know.

    Saturday, January 20, 2018 6:48 PM
  • This is what you XML looks like when correctly formatted:

    <?xml version="1.0" encoding="utf-8"?>
    <cfdi:Comprobante Version="3.3" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd" 
    	xmlns:cfdi="http://www.sat.gob.mx/cfd/3"
    	>
    
    	Serie="K" Folio="10" 
    	Fecha="2017-11-28T17:31:18" 
    	FormaPago="99" NoCertificado="00001000000400460285" 
    	CondicionesDePago="TR 20th of following month" 
    	SubTotal="753.92" 
    	Descuento="0" 
    	Moneda="USD" 
    	TipoCambio="18.52000" 
    	Total="874.55" 
    	TipoDeComprobante="I" 
    	MetodoPago="PPD" 
    	LugarExpedicion="37668"
    
    </cfdi:Comprobante>
    
    

    It does not match the code.  Notice there are no nodes with the names the code is using.


    \_(ツ)_/

    Saturday, January 20, 2018 6:52 PM
  • There is also no way the code could possible read that XML because the ML has namespaces and the code is not referencing them.


    \_(ツ)_/

    Saturday, January 20, 2018 7:00 PM
  • I have reviewed the old code and it looks as if NameSpace has been introduced.  I am not sure how this is read in differently than the previous XML format.  

    If there are links to how this is done, please let me know.

    Search for many examples.

    It would be easier to do in PowerShell.

    Also all references to objects in the XML are case sensitive.  You MUST use the case that is in the XML for all tags and names.


    \_(ツ)_/

    Saturday, January 20, 2018 7:52 PM
  • Unfortunately, the XML is not under my control.   Your mock up of how the data should look is understandable.  This is an invoice from Mexico and the format is dictated by the government.  I have been trying to modify the code to fix the errors that cropped up for some time now and have come up empty.  I now know that it is the change to namespace, but that information is not getting me any closer to a solution since I know I cannot change the format of the inbound file.


    Saturday, January 20, 2018 8:40 PM
  • Saturday, January 20, 2018 8:44 PM
  • This is how easy it is in PowerShell:

    [xml]$xml = cat "C:\Users\jvierra\Documents\SAPIEN\Scripts\Untitled.xml"
    $xml.Comprobante| select Total,FEcha,Folio,Serie
    
    $xml.Comprobante| select Total,FEcha,Folio,Serie | Export-Csv output.csv -NoType


    \_(ツ)_/


    • Edited by jrv Saturday, January 20, 2018 9:03 PM
    Saturday, January 20, 2018 8:53 PM