Jump to content
Sign in to follow this  
MadBoy

Better way to code this...

Recommended Posts

MadBoy

Hello,

I was wondering maybe someone has better way to code this.

What it does it creates .xml file and adds entries to it.

<?xml version="1.0" encoding="UTF-16"?>

<temporary>

<temp1>soluti3ons</temp1>

<temp2>myso13411lk</temp2>

<temp3>

<soviet>unio3n</soviet>

<duipa>elkos</duipa>

</temp3>

<progress>

<tasks3>tas1k6</tasks3>

</progress>

</temporary>

So far i made it so it supports if it's one value:

a)

_TempXMLSetting("temp1", "soluti3ons", "write")oÝ÷ ÚíÂÚ笫­¢+Ù}QµÁa51MÑÑ¥¹ ÅÕ½ÐíѵÀ̽ͽ٥ÐÅÕ½Ðì°ÅÕ½ÐíÕ¹¥¼Í¸ÅÕ½Ðì°ÅÕ½ÐíÝÉ¥ÑÅÕ½Ðì¤

Thank You,

MadBoy

Edited by MadBoy

My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites
eltorro

Please give this a try:

#include <Array.au3>
#Include <_XMLDomWrapper.au3>
;FileDelete(@ScriptDir&"\post_deployment_temporary4.xml")
Global $settings_install = @ScriptDir & "\post_deployment_installer.xml"
Global $settings_temporary = @ScriptDir & "\post_deployment_temporary4.xml"
Global $settings_root = "temporary"

_MyXMLWrite($settings_temporary, $settings_root, "progress/tasks1/kolega", "task154")
_MyXMLWrite($settings_temporary, $settings_root, "progress/tasks1", "task999")
_MyXMLWrite($settings_temporary, $settings_root, "tasks2", "task15")
_MyXMLWrite($settings_temporary, $settings_root, "progress/tasks3/kolega", "task444411")
_MyXMLWrite($settings_temporary, $settings_root, "progress/tasks2/kolega", "t44ask10")
_MyXMLWrite($settings_temporary, $settings_root, "proo/tasks2/kolega", "task10")
_MyXMLWrite($settings_temporary, $settings_root, "moo/dupa/tasks2", "task15")
_MyXMLWrite($settings_temporary, $settings_root, "kolega/task", "mydupablada")
_MyXMLWrite($settings_temporary, $settings_root, "pro3gress/ko4lega", "3tas34k10")

Func _MyXMLRead($file, $root_node, $key, $value)
    If FileExists($file) Then
        $mySettings = _XMLFileOpen ($file)
        If $mySettings = -1 Then
            Return SetError(@error, @extended, 0) ; read error
        EndIf
        Local $retval = _GetFirstValueChild("/" & $root_node & "/" & $key)
        If @error Then Return SetError(@error, @extended, 0)
        Return $retval
    Else
        Return SetError(2, 0, 0) ; file not found.
    EndIf
EndFunc   ;==>_MyXMLRead
Func _MyXMLWrite($file, $root_node, $key, $value)
    If $file = "" Then Return SetError(1, 0, 0) ; no filename
    If $key = "" Then Return SetError(2, 0, 0) ; no key
    If $value = "" Then Return SetError(3, 0, 0) ; no value
    Local $ERR, $keyPath, $strNode, $strPath, $bCreated, $TempXML
    If Not FileExists($file) Then
        _XMLCreateFile ($file, $root_node, True)
        $ERR = @error
        If $ERR Then Return SetError(4, $ERR, 0) ; error creating file.
    EndIf
    $TempXML = _XMLFileOpen ($file)
    $ERR = @error
    If $ERR Then Return SetError(5, $ERR, 0) ; error opening the xml file

    If _NodeExists("/" & $key) Then
        _XMLUpdateField ("/" & $root_node & "/" & $key, $value)
    Else
        _CreateNode($root_node,$key)
        _XMLUpdateField ("/" & $root_node & "/" & $key, $value)
    EndIf
    $objDoc = 0
EndFunc   ;==>_MyXMLWrite
Func _NodeExists($node)
    If Not IsObj($objDoc) Then Return SetError(1, 0, 0) ;no xml object
    $node = _StripLeft($node)
    If $node = "" Then Return SetError(2, 0, 0) ; no node
    Local $oNodeExists = $objDoc.selectSingleNode ("/"&$settings_root & "/" & $node)
    If IsObj($oNodeExists) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>_NodeExists
Func _CreateNode($root, $key)
    $keys = StringSplit($key, "\/")
    If IsArray($keys) Then
        $node = ""
        $prevNode = ""
        For $x = 1 To $keys[0]
            $prevNode = $node
            $node &= "/" & $keys[$x]
            If Not _NodeExists($node) Then
                If $x = 1 Then
                    _XMLCreateRootChild ($keys[$x], "")
                    ConsoleWrite(_XMLError()&@LF)
                    ConsoleWrite("Creating " & $keys[$x]&@LF)
                Else
                    _XMLCreateChildNode ("/"&$root & $prevNode, $keys[$x], "")
                    ConsoleWrite(_XMLError()&@LF)
                    ConsoleWrite("Creating " &"/"&$root & $prevNode &" " & $keys[$x] & @LF)
                EndIf
            EndIf
        Next
    Else
        If Not _NodeExists($key) Then
            _XMLCreateRootChild ($key, "")
            ConsoleWrite(_XMLError()&@LF)
            ConsoleWrite("Creating " & $keys&@LF)
        EndIf
    EndIf
    _XMLSaveDoc ()
EndFunc   ;==>_CreateNode
Func _GetFirstValueChild($node)
    $ret_val = _XMLGetChildText ($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1, 3, 0)
    EndIf
EndFunc   ;==>_GetFirstValueChild
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue ($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1, 3, 0)
    EndIf
EndFunc   ;==>_GetFirstValue
Func _StripLeft($sData)
    While (StringLeft($sData, 1) = "\") Or (StringLeft($sData, 1) = "/")
        $sData = StringTrimLeft($sData, 1)
    WEnd
    Return $sData
EndFunc

Edit: updated code to reflect last edited version.

Edited by eltorro

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.