Jump to content
Sign in to follow this  
dexto

XML read

Recommended Posts

dexto

I needed to read some info from XML so i found out that I needed to use COM objects. Oh well, I went and tried it. Its so complicated and I just want to read an XML (text file)! So, here is what I came up with:

Examples:

Reads:

<realfeel>24</realfeel> using:XMLget($file, 'adc_database\currentconditions\realfeel')

<sun rise="05:59" set="21:14"/> using:XMLget($file, 'adc_database\planets\sun')

<day number="2"><txtlong>Nice weather tomorrow</txtlong></day> using:XMLget($file, 'adc_database\forecast\day number="2"\daytime\txtlong')

<pressure state="Unavailable">1009.995</pressure> using: XMLget($file, 'adc_database\currentconditions\pressure')

Edit: faster and more accurate.

Local $xmlPath = @ScriptDir & '\data.xml'
If Not FileExists($xmlPath) Then Exit
$file = FileRead($xmlPath)
$file = StringReplace($file, @LF, '')
$file = StringSplit($file, @CR, 1)

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_database\currentconditions\realfeel'))
MsgBox(0, 'DONE!!!', XMLget($file, 'adc_database\planets\sun'))
MsgBox(0, 'DONE!!!', XMLget($file, 'adc_database\forecast\day number="1"\daytime\txtlong'))
MsgBox(0, 'DONE!!!', XMLget($file, 'adc_database\currentconditions\pressure'))

Func XMLget($file, $Path); XMLget($file,'adc_database\currentconditions\realfeel')
    $Path = StringSplit($Path, '/\|', 0)
    $lastline = 0
    For $lvl = 1 To $Path[0] Step 1
        For $line = $lastline To $file[0] Step 1
            $lastline = $line
            $hstart = StringInStr($file[$line], '<' & $Path[$lvl] & '>', 0)
            $hstarta = StringInStr($file[$line], '<' & $Path[$lvl] & ' ', 0)
            If $hstart Or $hstarta Then
                If $lvl == $Path[0] Then
                    If $hstart Then
                        $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                        If $end Then
                            $hstart = $hstart + StringLen('<' & $Path[$lvl] & '>')
                            Return StringMid($file[$line], $hstart, $end - $hstart)
                        EndIf
                    EndIf
                    If $hstarta Then
                        $end = StringInStr($file[$line], '/>', 0)
                        If $end Then
                            $hstarta = $hstarta + StringLen('<' & $Path[$lvl] & ' ')
                            $return = StringMid($file[$line], $hstarta, $end - $hstarta)
                            Return $return
                        EndIf
                        $ends = StringInStr($file[$line], '>', 0)
                        If $ends Then
                            $hstart = $ends + 1
                            $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                            If $end Then
                                $return = StringMid($file[$line], $hstart, $end - $hstart)
                                Return $return
                            EndIf
                        EndIf
                    EndIf
                EndIf
                ContinueLoop 2
            EndIf
        Next
        If $line == $file[0] Then ExitLoop
    Next
    Return 'not found'
EndFunc ;==>XMLget

Data for testing:

data.zip

Edited by dexto
  • Like 1

Share this post


Link to post
Share on other sites
Yashied
dexto

Thnx, I'll update it if I have newer versions.

Share this post


Link to post
Share on other sites
VAG

Hi, are you able to read the attributes of this path from your data.xml? I get the ''not found" message. My XML data has the similar format but cannot read out zone="MIZ076" county="MIC163" isactive="0" also.Posted Image

XMLget($file, 'adc_database\watchwarnareas')

Share this post


Link to post
Share on other sites
baamr

Hi,

Is it possible to get more than first value from a XMLget($file, $Path) ?

I mean if You have eg. 10 similar $Path in that same file.

Share this post


Link to post
Share on other sites
baamr

It's hard to handle ever single possible scenario without using the XMLDOM.  Good luck.

 

Thanks for reply,

 

But i am not so familiar with XMLdom.

I get all correct values from eg:

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databasecurrentconditionsrealfeel'))

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseforecastday number="1"daytimetxtlong'))

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databasecurrentconditionspressure'))

 

My problem is when a $path is repeated eg. 3 times like this:

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

 

I will get first value in every case even though i know there is 3 different.

 

How can i get this routine correct?

Share this post


Link to post
Share on other sites
baamr

Can i put $path into array(in some way) or is it a constant?

Share this post


Link to post
Share on other sites
baamr

Hi,

Here`s an example of what i mean.

I have added an extra line in Data saying : 

<planets>
<sun rise="06:05" set="21:20"/>

I tried to add another line in code like this:

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

But it will always give result from this line:

<planets>
<sun rise="05:59" set="21:14"/>

How can i get results from the second line?

Thanks,

data.zip

Share this post


Link to post
Share on other sites
baamr

Hi,

Here`s an example of what i mean.

I have added an extra line in Data saying : 

<planets>

<sun rise="06:05" set="21:20"/>

I tried to add another line in code like this:

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanetssun'))

But it will always give result from this line:

<planets>

<sun rise="05:59" set="21:14"/>

How can i get results from the second line?

Thanks,

 

 

No one wants to reply?

Share this post


Link to post
Share on other sites
guinness

No one wants to reply?

Please don't be impatient, this just fuels people not wanting to help you. If you're stuck ask in GHS about your generic issue.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
baamr

Please don't be impatient, this just fuels people not wanting to help you. If you're stuck ask in GHS about your generic issue.

 

Ok, thanks.

Share this post


Link to post
Share on other sites
jdelaney

Post your code...if it's truley the second instance then:

MsgBox(0, 'DONE!!!', XMLget($file, 'adc_databaseplanets[2]sun'))

Else, anyones guess is as good as mine.  This might be better posted to the Help and Support forum

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
TheSaint

Three things.

[1] Do some reading on how XML works.

[2] There are many posts here about XML and even an old UDF.

[3] Like guinness said, ask questions in General Help, where you can link to topics like this one, especially as the last post before your first here, was almost five years ago. Your questions will be noted far sooner than in this Forum which deals mostly with Example scripts.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
kesavan

Hi,

I downloaded 'Dexto' code. I implement the same for my requirement. It is working fine. However, for some XPATH, it is returning the other XPATH value. 

For eg: If I ask to read Parametes\Inputs\Pressure\Minimum, the script returns Parametes\Inputs\FlowRate\Minimum.  Again, If I ask to read Parametes\Inputs\Temperature\Minimum, the script returns Parametes\Inputs\FlowRate\Minimum.

I have attached the XML and AutoIt script. 

Please suggest me what i am doing wrong. Thanks in advance.

sendxml.zip

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.