Jump to content
Sign in to follow this  
leevy

I can't get xml value

Recommended Posts

leevy

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)

Share this post


Link to post
Share on other sites
leevy

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

Share this post


Link to post
Share on other sites
weaponx

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

Share this post


Link to post
Share on other sites
leevy

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.

Share this post


Link to post
Share on other sites
weaponx

This is invalid:

xmlns="http://www.aabb.com/"

This can work:

xmlns:aa="http://www.aabb.com/"

What is creating this xml file? Why is it invalid to begin with?

Share this post


Link to post
Share on other sites
weaponx

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

Share this post


Link to post
Share on other sites
leevy

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.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.