Jump to content

I can't get xml value


leevy
 Share

Recommended Posts

xml is:

<LogInResult>

<Result>SUCCESSED</Result>

<Ticket>99148a40-3ff5-43fb-8292-e4b1b7e854d0</Ticket>

</LogInResult>

My Autoit script as follows:

$xml = _XMLFileOpen("D:\login.xml")

$ticket = _XMLGetValue('/LogInResult/Ticket')

MsgBox(4096,"",$APIticket,10)

I can't get the value "99148a40-3ff5-43fb-8292-e4b1b7e854d0", but get "".

hope someone can answer me, thanks. (my english is poor, sorry)

Link to comment
Share on other sites

Sorry, I have miswrite the script. Please refer to this script.

xml is:

<LogInResult>

<Result>SUCCESSED</Result>

<Ticket>99148a40-3ff5-43fb-8292-e4b1b7e854d0</Ticket>

</LogInResult>

My Autoit script as follows:

$xml = _XMLFileOpen("D:\login.xml")

$ticket = _XMLGetValue('/LogInResult/Ticket')

_ArrayDisplay($ticket )

I can't get the value "99148a40-3ff5-43fb-8292-e4b1b7e854d0".

hope someone can answer me, thanks. (my english is poor, sorry)

Edited by leevy
Link to comment
Share on other sites

Since you didn't post your resolution, here it is:

;_XMLGetValue returns an array, this will return the first element
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc
Link to comment
Share on other sites

Since you didn't post your resolution, here it is:

;_XMLGetValue returns an array, this will return the first element
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc
thanks!

my question's root cause is the namespace of xml. I know it now, but no solution. the following is the whole xml:

<?xml version="1.0" encoding="utf-8"?>

<LogInResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.aabb.com/">

<Result>SUCCESSED</Result>

<Ticket>99148a40-3ff5-43fb-8292-e4b1b7e854d0</Ticket>

</LogInResult>

if [xmlns="http://www.aabb.com/"] is [xmlns:aa="http://www.aabb.com/"], the question will be solution. but I don't know how to change [xmlns="http://www.aabb.com/"] to be [xmlns:aa="http://www.aabb.com/"] by autoit.

Link to comment
Share on other sites

Manually specifying the default namespace in _XMLFileOpen should allow you to access the file without modifying it. Here I just added "aa" to the default namespace and than add "aa:" to my xpath.

#include "_XMLDomWrapper.au3"
#include <Array.au3>

$sXMLFile = "Untitled-1.xml"

$result = _XMLFileOpen($sXMLFile, 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:aa="http://www.aabb.com/"')
If $result = 0 Then 
    MsgBox(0,"","File not found")
    Exit
EndIf

$result = _GetFirstValue("//aa:Ticket")
If @ERROR Then
    ConsoleWrite("@ERROR: " & @ERROR & @CRLF)
Else
    ConsoleWrite($result & @CRLF)
EndIf

;_XMLGetValue returns an array (not sure why) this will return the first element
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc
Link to comment
Share on other sites

Manually specifying the default namespace in _XMLFileOpen should allow you to access the file without modifying it. Here I just added "aa" to the default namespace and than add "aa:" to my xpath.

#include "_XMLDomWrapper.au3"
#include <Array.au3>

$sXMLFile = "Untitled-1.xml"

$result = _XMLFileOpen($sXMLFile, 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:aa="http://www.aabb.com/"')
If $result = 0 Then 
    MsgBox(0,"","File not found")
    Exit
EndIf

$result = _GetFirstValue("//aa:Ticket")
If @ERROR Then
    ConsoleWrite("@ERROR: " & @ERROR & @CRLF)
Else
    ConsoleWrite($result & @CRLF)
EndIf

;_XMLGetValue returns an array (not sure why) this will return the first element
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc
Yes! It is exactly right. My script works well now.

Thank you very much.

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...