Jump to content

How to change settings to Node in XML


Recommended Posts

I want to update the parameters enclosed in red under node "userSettings--My.MySettings-- setting name =' SubprocessNotAllowed'" for my file ProcessControl.dll.config in target.png as attachment. But I don't know how to write.The way I write as below, it can't reach the parameter on setting name="SubprocessNotAllowed" and can't edit or change or add something. Help or guide me to fix this. Thank you.

#include <Constants.au3>
#include <String.au3>
#include <FileConstants.au3>

$IP_R=IniReadSection("IP.ini","IP") ; Read IP list of client using program.
;$Plant=IniRead("Config.ini", "Config", "Plant", "Default Value") ; Read IP plant

if @error Then
    ConsoleWrite("con not read file")
Else
    For $i = 1 To $IP_R[0][0]
        $IP=$IP_R[$i][1]

         if ping($IP) Then
            if FileExists('\\'&$IP&'\c$\')Then

               Local $oXML = ObjCreate("Microsoft.XMLDOM")
               $oXML.load("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
               If $oXML.parseError.errorCode Then Exit MsgBox($MB_SYSTEMMODAL, "You have an error", $oXML.parseError.reason)


               Local $oFFS = $oXML.SelectSingleNode("//configuration/userSettings/My.MySettings/setting[@name='SubprocessNotAllowed']")

               $result = $oFFS.getAttribute('value')

               ;$oFFS.setAttribute('value', ",MMDDE")
               ;$oXML.save("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config)

                  ConsoleWrite($IP&"    Config :"&$result&@CRLF)
               Else
                  ConsoleWrite($IP&"    Can't c$"&@CRLF)
               EndIf
            Else
            ConsoleWrite($IP&"  Can't ping"&@CRLF)
         EndIf

    Next

EndIf

 

target.png

ProcessControl.dll.config

Link to comment
Share on other sites

Now I can read parameter by script below but don't know how to edit parameter. 😅

#include <Constants.au3>
#include <String.au3>
#include <FileConstants.au3>

$IP_R=IniReadSection("IP.ini","IP") ; Read IP list of client using program.
;$Plant=IniRead("Config.ini", "Config", "Plant", "Default Value") ; Read IP plant

if @error Then
    ConsoleWrite("con not read file")
Else
    For $i = 1 To $IP_R[0][0]
        $IP=$IP_R[$i][1]

         if ping($IP) Then
            if FileExists('\\'&$IP&'\c$\')Then

               Local $oXML = ObjCreate("Microsoft.XMLDOM")
               $oXML.load("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
               If $oXML.parseError.errorCode Then Exit MsgBox($MB_SYSTEMMODAL, "You have an error", $oXML.parseError.reason)

               $oShows = $oXML.selectNodes("//configuration/userSettings/My.MySettings/setting[@name='SubprocessNotAllowed']")

               $parameter="Test"
                  For $oShow In $oShows

                      ConsoleWrite($IP&"  Parameter is : "&$oShow.text & @CRLF)
                      ;$oShow.setAttributeNode($oShows,"test")   --- This method is not work to set 
                      ;$oXML.save("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
                   Next

                  ;ConsoleWrite($IP&"   INTCP :"&$result&@CRLF)
               Else
                  ConsoleWrite($IP&"    Can't c$"&@CRLF)
               EndIf
            Else
            ConsoleWrite($IP&"  Can't ping"&@CRLF)
         EndIf

    Next

EndIf

 

Link to comment
Share on other sites

$oShows.Text = 'New Vaule'

 

check my XML.au3 UDF and _XML_UpdateField()

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Thank you. it work but format in xml not correct.

#include <Constants.au3>
#include <String.au3>
#include <FileConstants.au3>

$IP_R=IniReadSection("IP.ini","IP") ; Read IP list of client using program.
;$Plant=IniRead("Config.ini", "Config", "Plant", "Default Value") ; Read IP plant

if @error Then
    ConsoleWrite("con not read file")
Else
    For $i = 1 To $IP_R[0][0]
        $IP=$IP_R[$i][1]

         if ping($IP) Then
            if FileExists('\\'&$IP&'\c$\')Then

               Local $oXML = ObjCreate("Microsoft.XMLDOM")
               $oXML.load("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
               If $oXML.parseError.errorCode Then Exit MsgBox($MB_SYSTEMMODAL, "You have an error", $oXML.parseError.reason)

               $oShows = $oXML.selectNodes("//configuration/userSettings/My.MySettings/setting[@name='SubprocessNotAllowed']")

               ;$oShows.setAttribute=("ttt")

                  For $oShow In $oShows

                        $oShow.text=('<value> MPWB,MPCL,HDLT,ARLT,MTOG,PDCP </value>')

                        ConsoleWrite($IP&"  Parameter is : "&$oShow.text & @CRLF)
                      $oXML.save("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
                   Next

                  ;ConsoleWrite($IP&"   INTCP :"&$result&@CRLF)
               Else
                  ConsoleWrite($IP&"    Can't c$"&@CRLF)
               EndIf
            Else
            ConsoleWrite($IP&"  Can't ping"&@CRLF)
         EndIf

    Next

EndIf

image.thumb.png.a160d918d8667a5774bf259a04d6a572.png

Edited by KORN
Link to comment
Share on other sites

You are very close.  According to your first post, you are trying to change the text of the <value> node of the <setting> node that has a "name" attribute equal to "SubprocessNotAllowed".  Your xpath correctly selects the <setting> node.  However, the node you want to modify is the child <value>.

The example script below is one way that it could be done.  Note that I used the .SelectSingleNode method because there should be only one matching node.  Using .selectSingleNode removes the need to do a FOR loop on what is ultimately a single node.

Example script:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>

xml_example()

Func xml_example()

    Local $oComErr = ObjEvent("AutoIt.Error", "com_error_handler")
    #forceref $oComErr

    Local $oValueNode = Null

    With ObjCreate("Msxml2.DOMDocument.6.0")
        ;Load XML document
        .PreserveWhitespace = False
        .load(@ScriptDir & "\ProcessControl.dll.config")
        If .parseError.errorCode Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "XML PARSING ERROR", .parseError.reason)

        ;Select node of interest
        $oValueNode = .selectSingleNode('//setting[@name="SubprocessNotAllowed"]/value')
        If Not IsObj($oValueNode) Then Exit MsgBox($MB_ICONERROR, "Error", "Value node not found.")

        ;Display XML before change (parent node is displayed for context)
        ConsoleWrite("Value Before" & @CRLF)
        ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

        ;Modify text of "value" node
        $oValueNode.text = "some,new,value"

        ;Display XML after change (parent node is displayed for context)
        ConsoleWrite(@CRLF & "Value After" & @CRLF)
        ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

;~      ;Save new file
;~      .Save(@ScriptDir & "\New.ProcessControl.dll.config")
    EndWith
EndFunc

Func com_error_handler($oError)
    With $oError
        ConsoleWrite(@CRLF & "COM ERROR DETECTED!" & @CRLF)
        ConsoleWrite("  Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite("  Error Number........... " & StringFormat("0x%08x (%i)", .number, .number) & @CRLF)
        ConsoleWrite("  Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("  Error RetCode.......... " & StringFormat("0x%08x (%i)", .retcode, .retcode) & @CRLF)
        ConsoleWrite("  Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
    EndWith
    Exit
EndFunc

Console output:

Value Before
<setting name="SubprocessNotAllowed" serializeAs="String">
	<value>MPWB,MPCL,HDLT,ARLT,MTOG,PDCP</value>
</setting>

Value After
<setting name="SubprocessNotAllowed" serializeAs="String">
	<value>some,new,value</value>
</setting>

 

Edited by TheXman
Corrected a typo in a comment
Link to comment
Share on other sites

35 minutes ago, TheXman said:

You are very close.  According to your first post, you are trying to change the text of the <value> node of the <setting> node that has a "name" attribute equal to "SubprocessNotAllowed".  Your xpath correctly selects the <setting> node.  However, the node you want to modify is the child <value>.

The example script below is one way that it could be done.  Note that I used the .SelectSingleNode method because there should be only one matching node.  Using .selectSingleNode removes the need to do a FOR loop on what is ultimately a single node.

Example script:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>

xml_example()

Func xml_example()

    Local $oComErr = ObjEvent("AutoIt.Error", "com_error_handler")
    #forceref $oComErr

    Local $oValueNode = Null

    With ObjCreate("Msxml2.DOMDocument.6.0")
        ;Load XML document
        .PreserveWhitespace = False
        .load(@ScriptDir & "\ProcessControl.dll.config")
        If .parseError.errorCode Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "XML PARSING ERROR", .parseError.reason)

        ;Select node of interest
        $oValueNode = .selectSingleNode('//setting[@name="SubprocessNotAllowed"]/value')
        If Not IsObj($oValueNode) Then Exit MsgBox($MB_ICONERROR, "Error", "Value node not found." & @error)

        ;Display XML before change (parent node is displayed for context)
        ConsoleWrite("Value Before" & @CRLF)
        ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

        ;Modify text of "value" node
        $oValueNode.text = "some,new,value"

        ;Display XML after change (parent node is displayed for context)
        ConsoleWrite(@CRLF & "Value After" & @CRLF)
        ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

;~      ;Save new file
;~      .Save(@ScriptDir & "\New.ProcessControl.dll.config")
    EndWith
EndFunc

Func com_error_handler($oError)
    With $oError
        ConsoleWrite(@CRLF & "COM ERROR DETECTED!" & @CRLF)
        ConsoleWrite("  Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite("  Error Number........... " & StringFormat("0x%08x (%i)", .number, .number) & @CRLF)
        ConsoleWrite("  Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("  Error RetCode.......... " & StringFormat("0x%08x (%i)", .retcode, .retcode) & @CRLF)
        ConsoleWrite("  Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
    EndWith
    Exit
EndFunc

Console output:

Value Before
<setting name="SubprocessNotAllowed" serializeAs="String">
	<value>MPWB,MPCL,HDLT,ARLT,MTOG,PDCP</value>
</setting>

Value After
<setting name="SubprocessNotAllowed" serializeAs="String">
	<value>some,new,value</value>
</setting>

 

I have many computers needing to loop to get ipaddresses from files .ini file.Where did I go wrong with this error?

image.png.0c0e82047c65fb25db6254e92a872188.png

#include <Constants.au3>
#include <String.au3>
#include <FileConstants.au3>

$IP_R=IniReadSection("IP.ini","IP") ; Read IP list of client using program.
;$Plant=IniRead("Config.ini", "Config", "Plant", "Default Value") ; Read IP plant

if @error Then
    ConsoleWrite("con not read file")
Else
    For $i = 1 To $IP_R[0][0]
        $IP=$IP_R[$i][0]
        $parameter=$IP_R[$i][1]
         if ping($IP) Then
            if FileExists('\\'&$IP&'\c$\')Then
           Local $oComErr = ObjEvent("AutoIt.Error", "com_error_handler")
             #forceref $oComErr

             Local $oValueNode = Null

             With ObjCreate("Msxml2.DOMDocument.6.0")
                 ;Load XML document
                 .PreserveWhitespace = False
                 .load("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
                 If .parseError.errorCode Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "XML PARSING ERROR", .parseError.reason)

                 ;Select node of interest
                 $oValueNode = .selectSingleNode('/setting[@name="SubprocessNotAllowed"]/value')
                 If Not IsObj($oValueNode) Then Exit MsgBox($MB_ICONERROR, "Error", "Value node not found." & @error)

                 ;Display XML Before change (parent node is displayed for context)
                 ConsoleWrite("Value Before" & @CRLF)
                 ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

                 ;Modify text of setting's "value" node
                 $oValueNode.text = "korn"

                 ;Display XML Before change (parent node is displayed for context)
                 ConsoleWrite(@CRLF & "Value After" & @CRLF)
                 ConsoleWrite($oValueNode.selectSingleNode('..').xml & @CRLF) ;parent node

         ;~      ;Save new file
                  .Save("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")
    EndWith
               Else
                  ConsoleWrite($IP&"    Can't c$"&@CRLF)
               EndIf
            Else
            ConsoleWrite($IP&"  Can't ping"&@CRLF)
         EndIf

    Next

EndIf

 

Link to comment
Share on other sites

2 hours ago, KORN said:

Where did I go wrong with this error?

I'd say that the main issue is that you haven't taken the time to learn/understand what each line of your script is doing and why it's doing it.  Looking at how you implemented my explanation and example into your original script, it appears that you are just cutting and pasting without taking the time to understand any of it.  My explanation and example was provided for you to learn from, not to help you write your script.  The main point of my explanation was that your xpath was not selecting the correct node in order to change its text.

As to why you got the error message, use a little logic and common sense. The error message clearly states that the node was not found.  What does that mean to you? Go look in the specific file being processed and see if that particular node exists.  Given the logic of the script, I would say that it most likely does not. :rolleyes:

 

Edited by TheXman
Link to comment
Share on other sites

On 5/20/2023 at 8:30 PM, TheXman said:

I'd say that the main issue is that you haven't taken the time to learn/understand what each line of your script is doing and why it's doing it.  Looking at how you implemented my explanation and example into your original script, it appears that you are just cutting and pasting without taking the time to understand any of it.  My explanation and example was provided for you to learn from, not to help you write your script.  The main point of my explanation was that your xpath was not selecting the correct node in order to change its text.

As to why you got the error message, use a little logic and common sense. The error message clearly states that the node was not found.  What does that mean to you? Go look in the specific file being processed and see if that particular node exists.  Given the logic of the script, I would say that it most likely does not. :rolleyes:

 

I appreciate for your advice. And sorry for my inexperience and I'm a little hard to understand, but I'm trying. After I understand it, I'm still confused.

Because I try to test , I just change path to real path but it error.
 

From 
.load(@ScriptDir & "\ProcessControl.dll.config") ; But it work and change parameter

change to directory 
.load( "C:\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")

or 
.load("\\"&$IP&"\c$\Hoya Corporation\HOLT-VLProduction\ProcessControl.dll.config")

 

Link to comment
Share on other sites

As @TheXman already pointed out, you have to stop and understand the code provided. Your error doesn't have anything to do with the path of your XML file but with the structure of data in the file, more specific with a node that doesn't exists in some (most / all) XML files that you are processing.

When the words fail... music speaks.

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