Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

XML String Parsing


  • Please log in to reply
5 replies to this topic

#1 HopkinsProg

HopkinsProg

    Seeker

  • Active Members
  • 17 posts

Posted 15 November 2008 - 01:21 AM

Hey guys, perhaps you can help me with a small issue I'm having.

My goal is to use wininet.dll to submit POST variables to a webpage and download the contents of an XML file in return.

I can successfully submit my variables and I have my XML returned in string form. I would like to keep this in string form, inside the AutoIT script, but I have not found any methods that can directly parse a string, they can only parse files.

So, with a file temporarily created, using AutoIT's FileWrite() function, I am using the XMLDomWrapper script found elsewhere in the forums to parse the XML file.

However, for whatever reason, the string have is not correctly formatted. It looks exactly as I want it to:
<?xml version="1.0"?><output><msg>Hello World!</msg></output>

But, when i pass it to the XML parser, the _GetFirstValue function fails to parse the string, returning 0.

If I manually type in the contents of the file (vs downloading it from the internet), it works perfectly.

Do you all have any suggestions as to why the string I am getting from my webpage looks okay, but for whatever reason will not work when run through the XML parser?


If you know of a way that I can directly parse my string, without having to save a file, that would be the best solution for me.

You assistance is much appreciated. Thank you! :mellow:







#2 Prab

Prab

    Wayfarer

  • Active Members
  • Pip
  • 94 posts

Posted 15 November 2008 - 02:03 AM

You might want to look at _StringInsert(), StringReplace(), and StringRegExpReplace(). I have used all of them to edit strings in my scripts. (I have never used the XMLDomWrapper, so I don't know if these directly apply.)

#3 weaponx

weaponx

    I'm coming for blood, no code of conduct, no law.

  • MVPs
  • 5,366 posts

Posted 15 November 2008 - 02:11 AM

There is an undocumented function in the XML Dom Wrapper called _XMLLoadXML. See if that works for you.

#4 HopkinsProg

HopkinsProg

    Seeker

  • Active Members
  • 17 posts

Posted 15 November 2008 - 02:26 AM

That would be perfect, but I'm still having issues.

If I pass a hand-typed string, it works perfectly. But, if I pass the output from the web page, it crashes the function call with the attached error message.

So you all can see what I'm doing, here's the relevant code:
AutoIt         
#include <guiconstants.au3> #include "WinINet.au3" #Include <_XMLDomWrapper.au3> HotKeySet("{ESC}", "Terminate") DllOpen("wininet.dll") $internet = _WinINet_InternetOpen("Mozilla") If $internet == 0 Then     MsgBox(0, "Error", "Setup Error") EndIf $internetconnect = _WinINet_InternetConnect($internet, $INTERNET_SERVICE_HTTP, "mydomain.com", "80") If @error Then MsgBox(0, "Error", "Error connecting to the internet.") $data = "field=value"&@CRLF $type = "Content-Type: application/x-www-form-urlencoded"&@CRLF $agent = "User-Agent: Mozilla/4.0"&@CRLF $newdata = $agent & $type $httprequest = _WinINet_HttpOpenRequest($internetconnect, "POST", "/getxml.php") $addheader = _WinINet_HttpAddRequestHeaders($httprequest, $newdata, $HTTP_ADDREQ_FLAG_ADD) ConsoleWrite($addheader & @CRLF) $sendrequest = _WinINet_HttpSendRequest($httprequest,Default,StringToBinary($data)) local $readfile = Binary("") Do $readfile &= _WinINet_InternetReadFile($httprequest, 5000) Until @error<>0 Or Not @extended $loadedXML=_XMLLoadXML($readfile) ConsoleWrite("XMLLoadXML: "&$loadedXML&@CRLF) $myMsg=_GetFirstValue("/output/msg") msgbox(64,"INFO",$myMsg) Exit

Attached Thumbnails

  • XMLLoadXML_Error.png


#5 weaponx

weaponx

    I'm coming for blood, no code of conduct, no law.

  • MVPs
  • 5,366 posts

Posted 15 November 2008 - 03:28 AM

It might have to do with the encoding of the returned data. Maybe you can specify a content type for xml in the return request?
"Content-Type", "text/xml"

#6 HopkinsProg

HopkinsProg

    Seeker

  • Active Members
  • 17 posts

Posted 15 November 2008 - 03:59 AM

Yep, already delivered as text/xml. Even tried text/plain. I've checked the source in browsers, and it looks perfect. x-(
Must be something else wrong with it...

Edit:

*facepalm* :)

Yeah... I forgot to convert it back from binary... whoops... :mellow:

Thanks for the help with the rest of it guys! :(

Edited by HopkinsProg, 15 November 2008 - 04:20 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users