Jump to content

XML AppendChild


Go to solution Solved by jdelaney,

Recommended Posts

hello world.

I am trying to append some child nodes? and remove "default".  I am getting thrown off by the long names of each node "<setting name = "Clients" serializeAs="xml">"

 


<?xml version="1.0" encoding="utf-8"?>
<configuration>   
    <userSettings>
        <RAAD.My.MySettings>            
            <setting name="Clients" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>default</string>
                    </ArrayOfString>
                </value>
            </setting>            
        </RAAD.My.MySettings>
    </userSettings>
</configuration>
 
$cfg_file = "C:\user.config"
 
$oXML = ObjCreate("Microsoft.XMLDOM")
 
If Not IsObj($oXML) Then
    MsgBox(0, "", "Unable to create COM session to XML.")
    Exit
EndIf
 
$stest = FileRead($cfg_file)
$oXML.LoadXML($stest)
 
ConsoleWrite($oXML.xML)
Edited by gcue
Link to comment
Share on other sites

gcue,

Here's how to remove 'default'.

EDIT:  Watch out, because it will overwrite that file!   :oops:

$cfg_file = "C:\user.config"
 
$oXML = ObjCreate("Microsoft.XMLDOM")
 
If Not IsObj($oXML) Then
    MsgBox(0, "", "Unable to create COM session to XML.")
    Exit
EndIf
 
$stest = FileRead($cfg_file)
; added this section - borrowed from SmOke_N - http://www.autoitscript.com/forum/topic/23830-censur/?p=166395
$new = StringReplace($stest, "<string>default</string>", "<string></string>")
$stest = $new
FileOpen("C:\user.config", 2)
FileWrite("C:\user.config", $stest)
; section end
$oXML.LoadXML($stest)
 
ConsoleWrite($oXML.xML)
Edited by ZombieKillz
Link to comment
Share on other sites

let me know the specifics if you still need help after this example

$oXML = ObjCreate("Microsoft.XMLDOM")

If Not IsObj($oXML) Then
    MsgBox(0, "", "Unable to create COM session to XML.")
    Exit
EndIf

$oXML.Load(@DesktopDir & "\xml.xml")
ConsoleWrite($oXML.xML)

$oNewChild1 = $oXml.createElement("NewChild1")
$oNewChild1.text = "textNewChild1"
$oNewChild1.setAttribute("Att1","TextAtt1")
$oNewChild2 = $oXml.createElement("NewChild2")
$oNewChild2.text = "textNewChild2"
$oNewChild2.setAttribute("Att2","TextAtt2")
$oNewChild3 = $oXml.createElement("NewChild3")
$oNewChild3.text = "textNewChild3"
$oNewChild3.setAttribute("Att3","TextAtt3")
$oNewChild4 = $oXml.createElement("NewChild4")
$oNewChild4.text = "textNewChild4"
$oNewChild4.setAttribute("Att4","TextAtt4")
$oNewChild5 = $oXml.createElement("NewChild5")
$oNewChild5.text = "textNewChild5"
$oNewChild5.setAttribute("Att5","TextAtt5")

; Remove "default"
$oNode = $oXML.selectSingleNode( '//value//string' )
$oNode.text = ""

;...or do you want to delete the node???
; $oNode.parentNode.removeChild($oNode)

; Add the above, new nodes to //setting
$oParent = $oXML.selectSingleNode( '//setting' )
$oParent.appendChild($oNewChild1)
$oParent.appendChild($oNewChild2)
$oParent.appendChild($oNewChild3)
$oParent.appendChild($oNewChild4)
$oParent.appendChild($oNewChild5)

ConsoleWrite($oXML.xML)
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.
Link to comment
Share on other sites

awesome yes i wanted to remove the default node - it adds  them under a different place tho.. i need them to be within <arrayofstring...></array...>

thanks for your help!

Link to comment
Share on other sites

I have the sample to delete the default node, above...it's commented out, currently

To add the child elems to arrayofstring modify:

$oParent = $oXML.selectSingleNode( '//setting' )
 

to

$oParent = $oXML.selectSingleNode( '//arrayofstring' )
 

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.
Link to comment
Share on other sites

ah ok that works.. BUT in the interest of cutting down the huge XML file.. there are other arrayofstring sections

the one im specifically trying to modify is under <setting name="Clients" serializeAs="Xml">

thank youuu!

Link to comment
Share on other sites

for the delete:

$oNode = $oXML.selectSingleNode( '//setting[@name="Clients"]//value//string' )

for the adds:

$oParent = $oXML.selectSingleNode( '//setting[@name="Clients"]//ArrayOfString' )

note: these are all case sensitive

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.
Link to comment
Share on other sites

thanks again for your help

i have one last question though.

If I want to go backwards - that is, delete the entries I have entered - how can i delete all the children under the selected Node.

I saw RemoveAll here but I cant figure out the syntax I need to use.  I tried:

$oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//value//string')
;$oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//value')
$oNode.parentNode.RemoveAll($oNode)
;$oNode.RemoveAll($oNode)
;$oNode.RemoveAll()

Thank you!

Edited by gcue
Link to comment
Share on other sites

Not sure, I can play around for a min.

For now:

$oParent = $oNode.parentNode
$oChildren = $oParent.childNodes
For $oChild In $oChildren
    $oChild.parentNode.removeChild($oChild)
Next

edit: yeah, not sure.  Looks like it can't be done...workaround above...or, you can delete the parent(which deletes all children), and then recreate it, and your new elements.

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.
Link to comment
Share on other sites

that worked!

i was trying these also:

$oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//ArrayOfString')
;$oNode.RemoveAll()
$oNode.RemoveAll($oNode)

 

no luck

Link to comment
Share on other sites

only thing i notice with that solution is this happens if there are no child nodes


==> The requested action with this object has failed.:
$oParent = $oNode.parentNode
$oParent = $oNode.parentNode^ ERROR
Link to comment
Share on other sites

  • Solution

Where you define $oNode, you should do an error check line prior to acting on it:

If isObj($oNode) Then
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.
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...