Jump to content

xml with chrome, json with IE


31290
 Share

Recommended Posts

Hi Guys, 

Since I'm able to get a Dell equipment warranty status thanks to my API key, I'm using an UDF to extract data from an XML file and get the end date. > 

Thing is, when using InetGet, the original file is in JSON format and the UDF is not working anymore, even if I download the file with the xml extension. Therefore, and when I manually download the page with Chrome, I have a proper XML file where the UDF is working fine.

Here's my code:

Spoiler
Local $sFilePath = @DesktopDir & "\3MTXM12.xml"
InetGet("https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/APIKEYICANNOTGIVE", @DesktopDir & "\3MTXM12.xml", $INET_FORCERELOAD)

Local $xmlPath = @DesktopDir & "\3MTXM12.xml"
If Not FileExists($xmlPath) Then Exit
$file = FileRead($xmlPath)
$file = StringReplace($file, @LF, '')
$file = StringSplit($file, @CR, 1)

MsgBox(0, 'DONE!!!', StringTrimRight(XMLget($file, 'AssetEntitlement\EndDate'),9))

Func XMLget($file, $Path)
    $Path = StringSplit($Path, '/\|', 0)
    $lastline = 0
    For $lvl = 1 To $Path[0] Step 1
        For $line = $lastline To $file[0] Step 1
            $lastline = $line
            $hstart = StringInStr($file[$line], '<' & $Path[$lvl] & '>', 0)
            $hstarta = StringInStr($file[$line], '<' & $Path[$lvl] & ' ', 0)
            If $hstart Or $hstarta Then
                If $lvl == $Path[0] Then
                    If $hstart Then
                        $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                        If $end Then
                            $hstart = $hstart + StringLen('<' & $Path[$lvl] & '>')
                            Return StringMid($file[$line], $hstart, $end - $hstart)
                        EndIf
                    EndIf
                    If $hstarta Then
                        $end = StringInStr($file[$line], '/>', 0)
                        If $end Then
                            $hstarta = $hstarta + StringLen('<' & $Path[$lvl] & ' ')
                            $return = StringMid($file[$line], $hstarta, $end - $hstarta)
                            Return $return
                        EndIf
                        $ends = StringInStr($file[$line], '>', 0)
                        If $ends Then
                            $hstart = $ends + 1
                            $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                            If $end Then
                                $return = StringMid($file[$line], $hstart, $end - $hstart)
                                Return $return
                            EndIf
                        EndIf
                    EndIf
                EndIf
                ContinueLoop 2
            EndIf
        Next
        If $line == $file[0] Then ExitLoop
    Next
    Return 'not found'
EndFunc ;==>XMLget

 

I even tried to convert the json to xml > https://www.autoitscript.com/forum/topic/185717-js-json-to-xml/

I took a look here https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/ but I don't understand anything :/

 

The XML read UDF is just perfect for my needs but I'm stuck here... 

Thanks for any help you can provide :)

-31290-

3MTXM12.json

3MTXM12.xml

~~~ Doom Shall Never Die, Only The Players ~~~

Link to comment
Share on other sites

Hi @31290

here is two suggestions for you. Hope they are useful.

Let me know if you have any questions, i will do my best to answer :)

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Load("3MTXM12.xml"); or $oXML.LoadXML(FileRead("3MTXM12.xml"))
$oNodes = $oXML.SelectNodes("./AssetWarrantyDTO/AssetWarrantyResponse/AssetWarrantyResponse/AssetEntitlementData/AssetEntitlement/EndDate"); or $oNodes = $oXML.SelectNodes("//AssetEntitlement/EndDate")
For $oNode In $oNodes
    ConsoleWrite($oNode.text&@CRLF)
Next

ConsoleWrite("-----------------------------------------------"&@CRLF)

$oSC = ObjCreate("ScriptControl")
$oSC.language = "JScript"
$oSC.Eval("Array.prototype.Item = function(i){return this[i];};")
$oJSON = $oSC.Eval("("&FileRead("3MTXM12.json")&")")
For $i=0 To $oJSON.AssetWarrantyResponse.length-1
    For $j=0 To $oJSON.AssetWarrantyResponse.Item($i).AssetEntitlementData.length-1
        ConsoleWrite($oJSON.AssetWarrantyResponse.Item($i).AssetEntitlementData.Item($j).EndDate&@CRLF)
    Next
Next

 

Edited by genius257
Link to comment
Share on other sites

Hi Genius257, 

Thanks a lot for your input. 

But in the meantime, I found this solution:

; Creating the object
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET", "https://sandbox.apidp.dell.com/support/assetinfo/V4/getassetwarranty/ASSET?apikey=XXXX", False)
$oHTTP.SetRequestHeader("Accept", "application/xml")
$oHTTP.Send()

; Download the body response if any, and get the server status response code.
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

If $oStatusCode <> 200 then
 MsgBox(4096, "Response code", $oStatusCode)
EndIf

; Saves the body response regardless of the Response code
 $file = FileOpen(@TempDir & "\update222.xml", 2) ; The value of 2 overwrites the file if it already exists
 FileWrite($file, $oReceived)
 FileClose($file)

Can be useful if someone ever need it :)

Bye

-31290-

~~~ Doom Shall Never Die, Only The Players ~~~

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

×
×
  • Create New...