Sign in to follow this  
Followers 0
N00B08

Need help pulling value from XML file

5 posts in this topic

I am pretty green at this, and not sure how to parse the lines in my XML file to pull back a value. Thus far I am able to open the file, read the lines and trim, but I can't seem to remember how to get the rest.

My goal is to get the IP value so that I can then add it to a UNC path for a backup script.

This is an example of the XML file/format.

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

<Variables>

<Variable Name="Server\ServerName">TEST</Variable>

<Variable Name="Server\ServerIP">192.168.1.103</Variable>

<Variable Name="Install\InstallDir">C:\InstallPath</Variable>

<Variable Name="SystemCheck\Operating System\Name">Microsoft Windows XP Professional</Variable>

<Variable Name="SystemCheck\Operating System\ServicePack">3</Variable>

<Variable Name="SystemCheck\Video\Memory">512</Variable>

<Variable Name="Software\Version">3.0.0.2312</Variable>

<Variable Name="Software\Language">English</Variable>

</Variables>

I was hoping someone would be kind enough to help me....?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can just parse the strings, or learn to use XPaths:

#include <_XMLDOMWrapper.au3>
#include <Array.au3>

$debugging = True ; Internal debug variable inside _XMLDOMWrapper.au3

$sXML_File = @ScriptDir & "\Test1.xml"
_XMLFileOpen($sXML_File)
$aValue = _XMLGetValue('/Variables/Variable[@Name="Server\ServerIP"]')
If IsArray($aValue) Then
    _ArrayDisplay($aValue, "$aValue")
Else
    ConsoleWrite("Error:  $aArray = " & $aValue & @LF)
EndIf

The more complicated the XML file, the less string parsing you want to do, so eventually you want to learn this stuff.

:mellow:

Edit: Same goes for RegExp! (See UEZ below.)

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

One solution:

$XML = '<?xml version="1.0" encoding="utf-8"?>' & @LF & _
'<Variables>' & @LF & @LF & _
'<Variable Name="Server\ServerName">TEST</Variable>' & @LF & _
'<Variable Name="Server\ServerIP">192.168.1.103</Variable>' & @LF & _
'<Variable Name="SystemCheck\Operating System\Name">Microsoft Windows XP Professional</Variable>' & @LF & _
'<Variable Name="SystemCheck\Operating System\ServicePack">3</Variable>' & @LF & _
'<Variable Name="SystemCheck\Video\Memory">512</Variable>' & @LF & _
'<Variable Name="Software\Version">3.0.0.2312</Variable>' & @LF & _
'<Variable Name="Software\Language">English</Variable>' & @LF & _
'</Variables>'

$ip = StringRegExp($XML, '"Server\\ServerIP">(.+)</Variable>', 3)
$ip = $ip[0]

ConsoleWrite($ip & @CRLF)

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thank you both for help and the education!!

I was trying the stringregexp route for the past couple hours, but hit a wall.

...or learn to use XPaths:

I was looking for this right before I posted my question. A friend told me to try the xpath route, but he wasn't familiar with autoit, and I couldn't find the right info when I searched.

Share this post


Link to post
Share on other sites

I was looking for this right before I posted my question.  A friend told me to try the xpath route, but he wasn't familiar with autoit, and I couldn't find the right info when I searched.

You might also find it useful to learn XQuery as well. which extends XPath syntax to give similar functionality to using SQL on a database.

For relatively simple XML files without many xml attributes then the _StringBetween() function is a quick and easy way to parse XML   


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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  
Followers 0