Jump to content
Sign in to follow this  
mportal

Autoit Working with XML

Recommended Posts

mportal

I want to create a program in AutoIT in where i can query results from xml that is hosted on the web. For example:

<book>
<title>Night Fall</title>
<author>Demille, Nelson</author>
<publisher>Warner</publisher>
<price>$26.95</price>
<contentType>Fiction</contentType>
<format>Hardback</format>
<isbn>0446576638</isbn>
</book>

I want to create an autoit program that can query the results of Authors from books. This seems like a simple concept but i am completely new. My goal is to learn a little some about AutoIT and a little some about how XML works. Maybe if someone can give me a quick example of how to do this so i can review the code ill learn a bit about how it works. Or if someone can give me a tutorial about this subject ide appreciate it, Thanks for your help!

Share this post


Link to post
Share on other sites
jaberwacky

#include <String.au3>

Global Const $URL = "http://www.someaddress.com/file.xml"

Global Const $file = InetRead($URL)

Global Const $string = BinaryToString($file)

ConsoleWrite(_StringBetween($string, "<author>", "</author>") & @LF)

Something like that ^^^^

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
mportal

mmmm, search me! Is that a sock sewn out of win?

hmm ill give it a look and play around with it for a bit. thanks!

Share this post


Link to post
Share on other sites
NiVZ

I want to create a program in AutoIT in where i can query results from xml that is hosted on the web. For example:

<book>
<title>Night Fall</title>
<author>Demille, Nelson</author>
<publisher>Warner</publisher>
<price>$26.95</price>
<contentType>Fiction</contentType>
<format>Hardback</format>
<isbn>0446576638</isbn>
</book>

I want to create an autoit program that can query the results of Authors from books. This seems like a simple concept but i am completely new. My goal is to learn a little some about AutoIT and a little some about how XML works. Maybe if someone can give me a quick example of how to do this so i can review the code ill learn a bit about how it works. Or if someone can give me a tutorial about this subject ide appreciate it, Thanks for your help!

Hello,

I just finished my first AutoIT XML program. You can start by taking a look at the AutoIT XML UDF in the forum, and also looking at the W3School and Microsoft examples. You'll also need to learn a bit anout XPATH for querying.

First thing you need to do is get the XML version on the PC by finding the number at the end of msxml#.dll in System32 dir (where # is the version)

Next create an XML object and load your document into it

Then set the properties for validating, whitespace , and query language

Finally use XPath to select the nodes you require and then loop through them outputting the data you need.

Here is some quick code (which assumes XML 6.0):

; Set XML Version
$XMLVersion = 6

; Set path to XML File
$XMLFile = "c:\books.xml"

; Create XML Object
$objXML = ObjCreate("Msxml2.DOMdocument." & $XMLVersion & ".0")

; Setup XML Properties
$objXML.async = False
$objXML.preserveWhiteSpace = True
$objXML.validateOnParse = True
$objXML.Load($XMLFile)
$objXML.setProperty ("SelectionLanguage", "XPath")

; Do some queries

; -- Query 1 - Return title of all books ---

; Get all books
$BookNodes = $objXML.SelectNodes("/book")

For $Book in $BookNodes
    ; Write out the title
    ConsoleWrite($Book.SelectSingleNode("title").text & @CRLF)
Next


; -- Query 2 - Return Title and Author of books by Demille, Nelson

; Get all books where author is Demille, Nelson
$SearchNodes = $objXML.SelectNodes("/book[author='Demille, Nelson']")

For $Book in $SearchNodes
    ; Write out the title
    ConsoleWrite($Book.SelectSingleNode("title").text & @TAB & $Book.SelectSingleNode("author").text & @CRLF)
Next

Hopefully that should point you in the right direction

NiVZ

Edited by NiVZ

Share this post


Link to post
Share on other sites
notsure

Hello,

I just finished my first AutoIT XML program. You can start by taking a look at the AutoIT XML UDF in the forum, and also looking at the W3School and Microsoft examples. You'll also need to learn a bit anout XPATH for querying.

First thing you need to do is get the XML version on the PC by finding the number at the end of msxml#.dll in System32 dir (where # is the version)

Next create an XML object and load your document into it

Then set the properties for validating, whitespace , and query language

Finally use XPath to select the nodes you require and then loop through them outputting the data you need.

Here is some quick code (which assumes XML 6.0):

; Set XML Version
$XMLVersion = 6

; Set path to XML File
$XMLFile = "c:\books.xml"

; Create XML Object
$objXML = ObjCreate("Msxml2.DOMdocument." & $XMLVersion & ".0")

; Setup XML Properties
$objXML.async = False
$objXML.preserveWhiteSpace = True
$objXML.validateOnParse = True
$objXML.Load($XMLFile)
$objXML.setProperty ("SelectionLanguage", "XPath")

; Do some queries

; -- Query 1 - Return title of all books ---

; Get all books
$BookNodes = $objXML.SelectNodes("/book")

For $Book in $BookNodes
    ; Write out the title
    ConsoleWrite($Book.SelectSingleNode("title").text & @CRLF)
Next


; -- Query 2 - Return Title and Author of books by Demille, Nelson

; Get all books where author is Demille, Nelson
$SearchNodes = $objXML.SelectNodes("/book[author='Demille, Nelson']")

For $Book in $SearchNodes
    ; Write out the title
    ConsoleWrite($Book.SelectSingleNode("title").text & @TAB & $Book.SelectSingleNode("author").text & @CRLF)
Next

Hopefully that should point you in the right direction

NiVZ

I don't want to hi-jack this topic or something but i was wondering. Do you also know how to send a XML through a SOAP server with autoit? I'm trying to accomplish but i don't know how to talk to the soapserver.

Share this post


Link to post
Share on other sites
NiVZ

I don't want to hi-jack this topic or something but i was wondering. Do you also know how to send a XML through a SOAP server with autoit? I'm trying to accomplish but i don't know how to talk to the soapserver.

@notsure - I followed the SOAP example here and just replaced the strEnvelope with my XML block. Theres also an example further down that page, here , that uses XML to get the stock quotes from Microsoft.

If you need further help you'd be best starting a new topic and tell us more about what you are trying to do.

Edited by NiVZ

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  

×