leevy Posted September 8, 2008 Posted September 8, 2008 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)
leevy Posted September 8, 2008 Author Posted September 8, 2008 (edited) 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 September 8, 2008 by leevy
weaponx Posted September 8, 2008 Posted September 8, 2008 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
leevy Posted September 9, 2008 Author Posted September 9, 2008 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.
weaponx Posted September 9, 2008 Posted September 9, 2008 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?
weaponx Posted September 9, 2008 Posted September 9, 2008 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
leevy Posted September 10, 2008 Author Posted September 10, 2008 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now