|
|
|||||||
I'm trying to edit a value in an XML file. Can anyone show me a sample script that does so? I can't seem to find a native function that does it so I guess it's a matter of finding text strings for the beginning and ending tags and editing the value between them? A native function would be great if I just can't find it. Thanks, Jeff. |
||||||||
|
|
|||||||
Do a board search for XML... there are many topics, as well as User Defined Functions regarding your question. Post back if you have any troubles. |
||||||||
|
|
|||||||
Do you think you could point me in the right direction? I searched the forms pretty far back and saw lots of disucssion but not too much on code. Maybe I'm not looking in the right place or just don't know how to find it. I just want to modify the value in an xml file either via a funciton or just directly. Do you think you could point me to some sample code? Thanks Jeff. |
||||||||
|
|
|||||||
not that easy to find, true. but here is some code: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=134644&site_id=1#import |
||||||||
|
|
|||||||
Yeah harder to find than expected... those that Lonk provided and the following link to a library of XML UDFs (although never published to UDFs forum). http://www.kixtart.org/forums/ubbthreads...true#Post136437 |
||||||||
|
|
|||||||
that was weird. I remember there was quite a lot of chatter around them way back and I thought they would have made to our library, but seems the authors never got to posting them |
||||||||
|
|
|||||||
Ok. So I want to change the value of "HistoryDays" in the samle XML file below. I tried on example and it just appended a new attribute at the bottom of the file. I htink this one will modify the existig value but I am now getting the "idispatch pointers not allowed in expression" error. Here is the value i wan to modify: Code: <?xml version="1.0"?> <config version="1.0" serial="15" timestamp="1237010152.7"> <Lib> <Chat> <HistoryDays>14</HistoryDays> </Chat> </Lib> <UI> </config> Here is the code i'm running. Does this seem right. I'm pretty new at this stuff so any direction is appreciated. JEff. Code: $filename = "C:config.xml" $xml = LoadXml($filename) $= WriteXmlValue($xml, "lib/config/HistoryAge", "500") SaveXml($xml, $filename) $xml = 0 Function LoadXml($filename) Dim $, $rootNode $loadXml = CreateObject("Microsoft.XMLDOM"); If NOT $loadXml Return EndIf $= $loadXml.Load($filename) EndFunction Function WriteXmlValue($xml, $path, $value) Dim $p, $rootNode, $sectionNode, $parentNode, $childNode $sectionNode = $xml.SelectSingleNode($path); If NOT $sectionNode $parentNode = $xml For Each $node In Split($path,"/") $p = $p + $node $sectionNode = $xml.SelectSingleNode($p) If NOT $sectionNode $sectionNode = $xml.CreateElement($node) $parentNode = $parentNode.AppendChild($sectionNode) Else $parentNode = $sectionNode EndIf $p = $p + "/" Next EndIf If $sectionNode $sectionNode.Text = $value EndIf EndFunction Function SaveXml($xml, $filename) $SaveXml = $xml.Save($filename); EndFunction Function ReadXmlValue($xml, $key, optional $defaultValue) Dim $sectionNode $sectionNode = $xml.SelectSingleNode($key); If NOT $sectionNode $ReadXmlValue = $defaultValue; Else $ReadXmlValue = $sectionNode.FirstChild.Text; EndIf EndFunction Function WriteXMLattrib($xml, $Path, $Attr, $Value) $SelectionTag = $xml.getElementsByTagName($Path) $AttrTag = $SelectionTag.item(0) $eu = $AttrTag.SetAttribute($Attr,$Value); EndFunction Function RearXMLattrib($xml, $Path, $Attr) $SelectionTag = $xml.getElementsByTagName($Path) $AttrName = $SelectionTag.item(0) $RearXMLattrib =$AttrName.getAttribute($Attr) EndFunction |
||||||||
|
|
|||||||
Think you got a mismatch there between the key your referencing (HistoryAge) and the key in the file (HistoryDays) and might have some mal-formed XML there (UI) ... anyways using this xml: Code: <?xml version="1.0"?> <config version="1.0" serial="15" timestamp="1237010152.7"> <Lib> <Chat> <HistoryDays>14</HistoryDays> </Chat> </Lib> <UI/> </config> And this script, seems to work ... Code: Break On $FILENAME = "C:\config.xml" $XML = LoadXML($FILENAME) WriteXMLValue($XML, "config/Lib/Chat/HistoryDays", 123) SaveXML($XML, $FILENAME) Exit 0 Function LoadXml($filename) Dim $, $rootNode $loadXml = CreateObject("Microsoft.XMLDOM"); If NOT $loadXml Return EndIf $= $loadXml.Load($filename) EndFunction Function SaveXml($xml, $filename) $SaveXml = $xml.Save($filename); EndFunction Function ReadXmlValue($xml, $key, optional $defaultValue) Dim $sectionNode $sectionNode = $xml.SelectSingleNode($key); If NOT $sectionNode $ReadXmlValue = $defaultValue; Else $ReadXmlValue = $sectionNode.FirstChild.Text; EndIf EndFunction Function WriteXmlValue($xml, $path, $value) Dim $p, $rootNode, $sectionNode, $parentNode, $childNode $sectionNode = $xml.SelectSingleNode($path); If NOT $sectionNode $parentNode = $xml For Each $node In Split($path,"/") $p = $p + $node $sectionNode = $xml.SelectSingleNode($p) If NOT $sectionNode $sectionNode = $xml.CreateElement($node) $parentNode = $parentNode.AppendChild($sectionNode) Else $parentNode = $sectionNode EndIf $p = $p + "/" Next EndIf If $sectionNode $sectionNode.Text = $value EndIf EndFunction -Shawn |
||||||||
|
|
|||||||
Thanks! The battle with the ever-elusive typo Jeff. |
||||||||
|
|
|||||||
Hi Jeff, I've made a little update to insert Header in xml's file. it insert header when a new xml's file is created, and doesn't insert header if xml's file already exist. Hoping usefull Code: $filename = "config.xml" $xml = LoadXml($filename) $= WriteXmlValue($xml, "lib/config/HistoryAge", "500") SaveXml($xml, $filename) $xml = 0 $newfile = 0 Function LoadXml($filename) Dim $, $rootNode $loadXml = CreateObject("Microsoft.XMLDOM"); If NOT $loadXml Return EndIf $= $loadXml.async="false" if Not Exist ($filename) $newfile = 1 Endif $= $loadXml.Load($filename) EndFunction Function InsertHeader($xml) $header= $xml.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'") $= $xml.insertBefore($header,$xml.childNodes.Item(0)) EndFunction Function SaveXml($xml, $filename) if ($newfile = 1) InsertHeader($xml) Endif $SaveXml = $xml.Save($filename); EndFunction Function ReadXmlValue($xml, $key, optional $defaultValue) Dim $sectionNode $sectionNode = $xml.SelectSingleNode($key); If NOT $sectionNode $ReadXmlValue = $defaultValue; Else $ReadXmlValue = $sectionNode.FirstChild.Text; EndIf EndFunction Function WriteXmlValue($xml, $path, $value) Dim $p, $rootNode, $sectionNode, $parentNode, $childNode $sectionNode = $xml.SelectSingleNode($path); If NOT $sectionNode $parentNode = $xml For Each $node In Split($path,"/") $p = $p + $node $sectionNode = $xml.SelectSingleNode($p) If NOT $sectionNode $sectionNode = $xml.CreateElement($node) $parentNode = $parentNode.AppendChild($sectionNode) Else $parentNode = $sectionNode EndIf $p = $p + "/" Next EndIf If $sectionNode $sectionNode.Text = $value EndIf EndFunction Function WriteXMLattrib($xml, $Path, $Attr, $Value) $SelectionTag = $xml.getElementsByTagName($Path) $AttrTag = $SelectionTag.item(0) $eu = $AttrTag.SetAttribute($Attr,$Value); EndFunction Function ReadXMLattrib($xml, $Path, $Attr) $SelectionTag = $xml.getElementsByTagName($Path) $AttrName = $SelectionTag.item(0) $ReadXMLattrib =$AttrName.getAttribute($Attr) EndFunction Ludo, |