Jump to content

Opening xml files


Go to solution Solved by jdelaney,

Recommended Posts

I need some help with opening xml files. The script I am writing has to get data from a couple of attributes in the xml files, currently my problem is that I receive two types of xml.
I do not know which type of xml I am receiving, and on opening the xml, if it is one of the types (version = 3.2) I can read it ok, but I don't seem to be able to open the other version to extract the data I need.
I am using _XMLDomWrapper, so far this is the code:

Global $objDoc = 0
#include <_XMLDomWrapper.au3>


$sXMLFile = "B22.xml"

ConsoleWrite("Opening " & $sXMLFile & "..." )

$result = _XMLFileOpen($sXMLFile, 'xmlns:cfdi="http://www.sat.gob.mx/cfd/3"')
ConsoleWrite( "$result = " & $result & "...")

If $result = -1 Then
ConsoleWrite("Error opening xml file" & $sXMLFile & @CR)
    Exit
EndIf

$version = _XMLGetAttrib("/cfdi:Comprobante", "version")
ConsoleWrite("Version " & $version & " Error: " & @error & @CR)

If $version = "3.2" Then
$rfc = _XMLGetAttrib("/cfdi:Comprobante/cfdi:Emisor", "rfc")
$raz = _XMLGetAttrib("/cfdi:Comprobante/cfdi:Emisor", "nombre")
Else


$objDoc = 0
ConsoleWrite( @CR & "File is 2.2, opening: " & $sXMLFile & "..." & @CR )


$result = _XMLFileOpen($sXMLFile, 'xmlns="http://www.sat.gob.mx/cfd/2"')
ConsoleWrite( "$result = " & $result & "...")


$version = _XMLGetAttrib("/Comprobante", "version")
ConsoleWrite("Version " & $version & " Error: " & @error & @CR)


If $result = -1 Then
ConsoleWrite("Error opening xml file" & $sXMLFile & @CR)
Exit
EndIf


$rfc = _XMLGetAttrib("/Comprobante/Emisor", "rfc")
$raz = _XMLGetAttrib("/Comprobante/Emisor", "nombre")
EndIf


ConsoleWrite("RFC: " & $rfc & "  Nombre: " & $raz & @CR ) 

 When I use it with file A32.xml it correctly displays the data, but when using file B22.xml I only get "-1" 
 
Thanks in advance for your help

A32.xml

B22.xml

Link to comment
Share on other sites

odd, it's valid xml:

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load(@DesktopDir & "\b22.xml")
ConsoleWrite($oXML.xml)

probably a limitation with the _xml functions, or passing in a bad param

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

What I need to retrieve are the values of attributes in node "Comprobante", no need to use _XMLDomWrapper, I tried adding the following two lines to your script:

$comprobante = $oXML.selectNodes("/root/Comprobante")
ConsoleWrite("Comprobante = " & $comprobante & @CR)

Still nothing, thanks for the tip

Edited by Lavonio
Link to comment
Share on other sites

  • Solution

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load(@DesktopDir & "\b22.xml")
$oNodes = $oXML.selectnodes("//Comprobante")
ConsoleWrite($oNodes.length & @CRLF)
For $oNode In $oNodes
    ConsoleWrite($oNode.xml & @CRLF)
    $oAttributes = $oNode.Attributes()
    For $oAttribute In $oAttributes
        ConsoleWrite($oAttribute.nodename & ": " & $oAttribute.text & @CRLF)
    Next
Next

which attribute?  use .getattribute("attributename") to return the string value of one attribute

example

$oNode.getattribute("xmlns:implocal")

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...