Sign in to follow this  
Followers 0
erifash

XML parsing functions

13 posts in this topic

#1 ·  Posted (edited)

I had created something better than my _INITable functions a while ago and I didn't do much of anything with them until now. They are called XML parsing functions because I thought that would be better tha meta, even though they work on any type of meta tag. So, just thought I'd share with you:

Func _XMLSet(ByRef $s_xml, $s_ele, $s_node)
    If _XMLExists($s_xml, $s_ele) Then _XMLDel($s_xml, $s_ele)
    $s_xml = $s_xml & "<" & $s_ele & ">" & $s_node & "</" & $s_ele & ">" & @CRLF
EndFunc

Func _XMLGet($s_xml, $s_ele)
    If not _XMLExists($s_xml, $s_ele) Then
        SetError(1)
        Return ""
    EndIf
    Return __StringParse($s_xml, "<" & $s_ele & ">", "</" & $s_ele & ">", 1)
EndFunc

Func _XMLDel(ByRef $s_xml, $s_ele)
    If not _XMLExists($s_xml, $s_ele) Then
        SetError(1)
        Return ""
    EndIf
    StringReplace("<" & $s_ele & ">" & _XMLGet($s_xml, $s_ele) & "</" & $s_ele & ">" & @CRLF, "", 0, 1)
EndFunc

Func _XMLExists($s_xml, $s_ele)
    If StringInStr($s_xml, "<" & $s_ele & ">") and StringInStr($s_xml, "</" & $s_ele & ">") Then Return 1
    Return 0
EndFunc

Func _XMLSave($s_xml, $s_file)
    FileWrite($s_file, $s_xml)
    Return not @error
EndFunc

Func _XMLLoad($s_file)
    Return __FileReadAll($s_file)
EndFunc

Func _XMLStartSection(ByRef $s_xml, $s_secname)
    $s_xml = $s_xml & "<" & $s_secname & ">" & @CRLF
EndFunc

Func _XMLEndSection(ByRef $s_xml, $s_secname)
    $s_xml = $s_xml & "</" & $s_secname & ">" & @CRLF
EndFunc

;############################
;#     Helper Functions     #
;############################

Func __FileReadAll($s_file)
    If not FileExists($s_file) Then
        SetError(1)
        Return ""
    EndIf
    Return FileRead($s_file, FileGetSize($s_file))
EndFunc

Func __Test($b_Test, $v_True = 1, $v_False = 0)
    If $b_Test Then Return $v_True
    Return $v_False
EndFunc

Func __StringFindOccurances($sStr1, $sStr2)
    For $i = 1 to StringLen($sStr1)
        If not StringInStr($sStr1, $sStr2, 1, $i) Then ExitLoop
    Next
    Return $i
EndFunc

Func __StringParse($sz_str, $sz_before, $sz_after, $i_occurance = 0)
    Local $sz_sp1 = StringSplit($sz_str, $sz_before, 1)
    If $i_occurance < 0 or $i_occurance > $sz_sp1[0] Then
        SetError(1)
        Return ""
    EndIf
    Local $sz_sp2
    If $i_occurance = 0 Then
        $sz_sp2 = StringSplit($sz_sp1[$sz_sp1[0]], $sz_after, 1)
    Else
        $sz_sp2 = StringSplit($sz_sp1[$i_occurance + 1], $sz_after, 1)
    EndIf
    Return $sz_sp2[1]
EndFunc  ;==>_StringParse()

Hope you like them! ;)

Edited by erifash

Share this post


Link to post
Share on other sites



Oops! ...and here is the example:

$xml = ""
_XMLStartSection($xml, "main")
_XMLSet($xml, "test", "hola")
_XMLSet($xml, "second test", "what")
_XMLStartSection($xml, "tester")
_XMLSet($xml, "bored...", "wtf omg rofl")
_XMLEndSection($xml, "tester")
_XMLEndSection($xml, "main")
_XMLSave($xml, "temp.xml")

$load = _XMLLoad("temp.xml")
FileDelete("temp.xml")
MsgBox(0, "xml equal test", ($xml = $load))

MsgBox(0, "hola", _XMLGet($load, "test"))
MsgBox(0, "what", _XMLGet($load, "second test"))
MsgBox(0, "wtf omg rofl", _XMLGet($load, "bored..."))

Share this post


Link to post
Share on other sites

Doing XML by hand is pointless now since we can use COM and MSXML. You can simplify that code to just a few lines using MSXML.

Share this post


Link to post
Share on other sites

I had created something better than my _INITable functions a while ago and I didn't do much of anything with them until now. They are called XML parsing functions because I thought that would be better tha meta, even though they work on any type of meta tag. So, just thought I'd share with you:

Func _XMLSet(ByRef $s_xml, $s_ele, $s_node)
    If _XMLExists($s_xml, $s_ele) Then _XMLDel($s_xml, $s_ele)
    $s_xml = $s_xml & "<" & $s_ele & ">" & $s_node & "</" & $s_ele & ">" & @CRLF
EndFunc

Func _XMLGet($s_xml, $s_ele)
    If not _XMLExists($s_xml, $s_ele) Then
        SetError(1)
        Return ""
    EndIf
    Return __StringParse($s_xml, "<" & $s_ele & ">", "</" & $s_ele & ">", 1)
EndFunc

Func _XMLDel(ByRef $s_xml, $s_ele)
    If not _XMLExists($s_xml, $s_ele) Then
        SetError(1)
        Return ""
    EndIf
    StringReplace("<" & $s_ele & ">" & _XMLGet($s_xml, $s_ele) & "</" & $s_ele & ">" & @CRLF, "", 0, 1)
EndFunc

Func _XMLExists($s_xml, $s_ele)
    If StringInStr($s_xml, "<" & $s_ele & ">") and StringInStr($s_xml, "</" & $s_ele & ">") Then Return 1
    Return 0
EndFunc

Func _XMLSave($s_xml, $s_file)
    FileWrite($s_file, $s_xml)
    Return not @error
EndFunc

Func _XMLLoad($s_file)
    Return __FileReadAll($s_file)
EndFunc

;############################
;#   Helper Functions    #
;############################

Func __FileReadAll($s_file)
    If not FileExists($s_file) Then
        SetError(1)
        Return ""
    EndIf
    Return FileRead($s_file, FileGetSize($s_file))
EndFunc

Func __Test($b_Test, $v_True = 1, $v_False = 0)
    If $b_Test Then Return $v_True
    Return $v_False
EndFunc

Func __StringFindOccurances($sStr1, $sStr2)
    For $i = 1 to StringLen($sStr1)
        If not StringInStr($sStr1, $sStr2, 1, $i) Then ExitLoop
    Next
    Return $i
EndFunc

Func __StringParse($sz_str, $sz_before, $sz_after, $i_occurance = 0)
    Local $sz_sp1 = StringSplit($sz_str, $sz_before, 1)
    If $i_occurance < 0 or $i_occurance > $sz_sp1[0] Then
        SetError(1)
        Return ""
    EndIf
    Local $sz_sp2
    If $i_occurance = 0 Then
        $sz_sp2 = StringSplit($sz_sp1[$sz_sp1[0]], $sz_after, 1)
    Else
        $sz_sp2 = StringSplit($sz_sp1[$i_occurance + 1], $sz_after, 1)
    EndIf
    Return $sz_sp2[1]
EndFunc ;==>_StringParse()

Hope you like them!  ;)

<{POST_SNAPBACK}>

Where is _XMLStartSection()

and

_XMLEndSection()?


Talking Clockhttp://www.autoitscript.com/forum/index.php?showtopic=20751Talking Headlineshttp://www.autoitscript.com/forum/index.php?showtopic=20655Sometimes, I sits and thinkssometimes, I just sits

Share this post


Link to post
Share on other sites

Doing XML by hand is pointless now since we can use COM and MSXML. You can simplify that code to just a few lines using MSXML.

What if MSXML is not installed on the system running the script? This UDF would be a good way around that issue.

Share this post


Link to post
Share on other sites

Not likely it won't be there. MSXML is installed as part of Internet Explorer (first with IE 4.0 -- and that goes back a while) and with other products.

Dale

What if MSXML is not installed on the system running the script? This UDF would be a good way around that issue.


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Not likely it won't be there. MSXML is installed as part of Internet Explorer (first with IE 4.0 -- and that goes back a while) and with other products.

Dale

Excellent point. I guess that's why is says "Newbie" under my name.;)

Endgame

Edited by Endgame

Share this post


Link to post
Share on other sites

Doing XML by hand is pointless now since we can use COM and MSXML. You can simplify that code to just a few lines using MSXML.

I imagine it would be faster too. Could you post a very quick example please? I'm looking to do some XML parsing and I really don't want to reinvent the wheel.

ben

Share this post


Link to post
Share on other sites

I imagine it would be faster too. Could you post a very quick example please? I'm looking to do some XML parsing and I really don't want to reinvent the wheel.

ben

I'd like to see some example code also

Share this post


Link to post
Share on other sites

Find some examples of using MSXML in VBScript and port them to AutoIt. AutoIt and VBScript are syntactically similar enough that that shouldn't be too hard.

There may also be some examples posted in the forum though I don't know of any off the top of my head.

Share this post


Link to post
Share on other sites

Where is _XMLStartSection() and _XMLEndSection()? B)

Share this post


Link to post
Share on other sites

Where is _XMLStartSection() and _XMLEndSection()? B)

Here ya go!
Func _XMLStartSection(ByRef $s_xml, $s_secname)
    $s_xml = $s_xml & "<" & $s_secname & ">" & @CRLF
EndFunc

Func _XMLEndSection(ByRef $s_xml, $s_secname)
    $s_xml = $s_xml & "</" & $s_secname & ">" & @CRLF
EndFunc
Updated in first post too! :o

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I dont really understand the example...

it doesnt do anything when i run it...

AH! nvm, i get it!

very useful, and exactly what i was looking for!

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

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