Jump to content

Read an XML file and check for a node


joos
 Share

Recommended Posts

Help

I have tried doing this using both the object and XML UDF but to no avail. I want to open an xml file

Func ReadIt ()

Fileopen "path\Filename.xml"

CheckforNodeInXMLFile "<NodeName>"

If Node not there then exit

Else

If node is there (true) then continue

CloseFile

EndFunc

Here is the XMLFile

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

<configinfo>

<shop>

<shop name="ShopOne" />

<shop name="ShopTwo" />

<shop name="ShopThree" />

</shop>

<service>

<!-- The default service has been changed at this time

** Only add this back in if needed **

<service name="Default" security="disabled" />

-->

<service name="ServiceOne" />

<service name="ServiceTwo" />

<service name="ServiceThree" />

<service name="ServiceFour" />

<service name="ServiceFive" />

<service name="ServiceSix" />

</service>

</configinfo>

I am trying to find out if the <service> node is there. I have a program which writes this if it is successful. Any pointer or help will be a great help as the little hair I have left is slowly being pulled out

Thanks Joe

Link to comment
Share on other sites

Make sure you use eltorros' excellent XML UDF found here: http://www.autoitscript.com/forum/index.ph...amp;hl=xml++com

This will throw a message box stating if <service> was found or not:

#include <_XMLDomWrapper.au3>

$sXMLFile = "test.xml"

$result = _XMLFileOpen($sXMLFile)
If $result = 0 Then 
    MsgBox(0,"", "Error opening " & $sXMLFile)
    Exit
EndIf

$sXPath = "configinfo/service"

$nodeArray = _XMLSelectNodes($sXPath)

if $nodeArray[0] > 0 Then
    MsgBox(0,"", "Service node found")
Else
    MsgBox(0,"", "Service node NOT found")
EndIf
Link to comment
Share on other sites

All you want to know is is if the <service> node exists? Not if it actually has subnodes?

Yes just that.

I have now opened the file and got each line in it

$sfile = FileOpen("Filename", 0)

; Check if file opened for reading OK if not bomb out

If $sfile = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$sline = FileReadLine($sfile)

If @error = -1 Then ExitLoop

; Do a string in string check for the <service>

$iRoleCheck = StringInStr($sline, "<service>")

If $iRoleCheck = "3" Then

$itest = 1

EndIf

Wend

FileClose($sfile)

Not the best way and not reading the file as an XML file but this will give me the result i need at this time, however, not the way in which I wanted to do it. If the XML route would be better then I will keep plugging away.

Thanks for the help.

Joe

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...