Jump to content

Help : Write XML on recursive tree menu


Recommended Posts

Helo fantastic forum :P

I would like to build an tree-menu-application for read-write an xml file

the under example only reads a XML file ....

:P THE QUESTION : How to write AND modify the values and the keys of XML file ??

the Code example for ONLY read:

CODE

#Include <Constants.au3>

#NoTrayIcon

Opt("TrayOnEventMode", 1)

Opt("TrayMenuMode", 1)

;------| global |--------------------------------------

Global $commands = ObjCreate("Scripting.Dictionary")

;------| XML |----------------------------------

$xml = ObjCreate("Microsoft.XMLDOM")

If Not IsObj($xml) Then Exit(1) ; error

If Not FileExists("menu.xml") Then Exit(2) ; control

; load

$xml.Async = "false"

$xml.Load("menu.xml") ; <-------| il file che contiene la struttura

; Title

$TitoloMenu = $xml.documentElement.getAttribute("TitoloMenu")

; traymenu

TraySetState()

; tooltip

TraySetToolTip($TitoloMenu)

; menu

buildMenu($xml)

; separator

TrayCreateItem("")

; Exit apps

TrayCreateItem("Exit")

TrayItemSetOnEvent(-1, "ExitScript")

While 1

Sleep(10)

WEnd

; build recursive menu

Func buildMenu( $xml, $query = '/menu/item|/menu/menu', $parent = -1 )

$items = $xml.SelectNodes($query)

If ( $items.Length > 0 ) Then

For $item In $items

If ( $item.nodeName = "menu" ) Then

$idSubMenu = TrayCreateMenu($item.GetAttribute("NomeMenu"), $parent)

buildMenu($item, 'menu|item', $idSubMenu)

ElseIf ( $item.nodeName = "item" ) Then

; recovery a command

$caption = $item.GetAttribute("caption")

$cmd = $item.GetAttribute("cmd")

; create item

$id = TrayCreateItem($caption, $parent)

If $cmd <> "" Then

$commands.Add($id, $cmd)

TrayItemSetOnEvent(-1, "RunCmd")

EndIf

EndIf

Next

EndIf

EndFunc

; run command

Func RunCmd()

Run($commands.Item(@TRAY_ID))

EndFunc

; close

Func ExitScript()

Exit(0)

EndFunc

;->>>>>>> I would like to write function

Func ModifiyXml()

; ???????

EndFunc

Func WriteNewChild_XML()

; ????? thanks

endFunc

Func DeleteChild_XML()

; ????? thanks

endFunc

Func SearchValue_XML()

; ????? thanks

endFunc

attach example of read xml :

..

example xml file :

CODE
<?xml version="1.0" encoding="ISO-8859-1"?>

<menu TitoloMenu="XML-TrayMenu Esempio">

<menu NomeMenu="Utility">

<item caption="Block notes" cmd="notepad.exe" />

<item caption="Explore" cmd="explorer.exe" />

<item caption="Varius" cmd="explorer.exe" />

<menu NomeMenu="Games">

<item caption="Sol Windows XP" cmd="sol.exe" />

</menu>

<menu NomeMenu="Games2">

<item caption="WinMine - XP Game" cmd="winmine.exe" />

</menu>

</menu>

<menu NomeMenu="Varius 2">

<item caption="reboot XP" cmd="example.exe" />

</menu>

<item caption="" />

<item caption="Calc" cmd="calc.exe" />

<item caption="Command Prompt" cmd="cmd.exe" />

</menu>

repeat : THE QUESTION : How to write AND modify the values and the keys/child of XML file ??

thanks to everybody for reply :)

Only_Read_Example.rar

Edited by icemax
Link to comment
Share on other sites

Hi,

there is a XMLWrapper in Scripts & S.

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

#include "_XMLDomWrapper.au3"

Local $sFile = "test.xml"
If FileExists($sFile) Then
    $ret = _XMLFileOpen($sFile)
    If $ret = 0 Then Exit
    
    ;Define attributes for item node
    Dim $aKeys[2] = ["caption","cmd"]
    Dim $aValues[2]
    
    ;Create new node under Utility
    $aValues[0] = "Wordpad"
    $aValues[1] = "wordpad.exe"
    _XMLCreateChildNodeWAttr("//menu[@NomeMenu='Utility']","item",$aKeys,$aValues)
Else
    MsgBox(4096, "Error", _XMLError())
EndIf

Link to comment
Share on other sites

  • 1 month later...

Hello fantastic forum

and ..re-please help :

I want to add a node and delete node to schema ??

this routine is not works :

ADD : routine

XMLCreateRootChild("//menu[@NomeMenu='" & $Val_Group & "']","")

;) not works .. do not create node :D

:lmao: please help for ADD and DELETE nodes or items

Thanks Againg to all ;)

Link to comment
Share on other sites

Your syntax is incorrect.

_XMLCreateRootChild ('menu')

This will NOT have an attribute.

You will need:

Dim $aKeys[1] = ["NomeMenu"]
Dim $aValues[1] = [$Val_Group]
_XMLCreateRootNodeWAttr ('menu',$aKeys,$aValues)

I'm not even sure what you are expecting.

Link to comment
Share on other sites

Your syntax is incorrect.

_XMLCreateRootChild ('menu')

This will NOT have an attribute.

You will need:

Dim $aKeys[1] = ["NomeMenu"]
Dim $aValues[1] = [$Val_Group]
_XMLCreateRootNodeWAttr ('menu',$aKeys,$aValues)

I'm not even sure what you are expecting.

TNX for response .. but not works

this is my XML structure :

<?xml version="1.0" encoding="ISO-8859-1"?>

<menu TitoloMenu="XML-treeview example">

<menu NomeMenu="Utility">

<item caption="Block notes" cmd="notepad.exe" />

<item caption="Explore" cmd="explorer.exe" />

<item caption="Varius" cmd="explorer.exe" />

<menu NomeMenu="Games">

<item caption="Sol Windows XP" cmd="sol.exe" />

</menu>

<menu NomeMenu="Games2">

<item caption="WinMine - XP Game" cmd="winmine.exe" />

</menu>

</menu>

<menu NomeMenu="Varius 2">

<item caption="reboot XP" cmd="example.exe" />

</menu>

</menu>

i want write the new node with name : EXAMPLE or delete the exist node :

example

<menu NomeMenu="EXAMPLE">

...bla ..bla

</menu>

Thanks Again

Link to comment
Share on other sites

The last piece of code I posted works just fine. I tested it.

#include "../_XMLDomWrapper.au3"

Local $sFile = "test.xml"
If FileExists($sFile) Then
    $ret = _XMLFileOpen($sFile)
    If $ret = 0 Then Exit
    
    Dim $aKeys[1] = ["NomeMenu"]
    Dim $aValues[1] = ['EXAMPLE']
    _XMLCreateRootNodeWAttr ('menu',$aKeys,$aValues)
Else
    MsgBox(4096, "Error", _XMLError())
EndIf

Result:

<?xml version="1.0" encoding="ISO-8859-1"?>
<menu TitoloMenu="XML-TrayMenu Esempio">
    <menu NomeMenu="Utility">
        <item caption="Block notes" cmd="notepad.exe"/>
        <item caption="Explore" cmd="explorer.exe"/>
        <item caption="Varius" cmd="explorer.exe"/>
        <menu NomeMenu="Games">
            <item caption="Sol Windows XP" cmd="sol.exe"/>
        </menu>
        <menu NomeMenu="Games2">
            <item caption="WinMine - XP Game" cmd="winmine.exe"/>
        </menu>
        <item/>
        <item caption="Wordpad" cmd="wordpad.exe"/>
    </menu>
    <menu NomeMenu="Varius 2">
        <item caption="reboot XP" cmd="example.exe"/>
    </menu>
    <item caption=""/>
    <item caption="Calc" cmd="calc.exe"/>
    <item caption="Command Prompt" cmd="cmd.exe"/>
    <menu NomeMenu="EXAMPLE"/>
</menu>
Link to comment
Share on other sites

thanks ... yes yes ...

your code alone is perfect

but in my program not works ;) (later I will find the my code problem)

please : why your code :

1 - not write <menu> at the end : ??

2 - ..PLE"/>

practically .. your code write :

<menu NomeMenu="EXAMPLE"/>

but not write :

<menu NomeMenu="EXAMPLE">

</menu>

thanks (very kind)

and ..excuse me

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...