#192896 - 2009-03-14 06:00 PM
edit XML file
|
jeff_eisenberg
Fresh Scripter
Registered: 2009-02-26
Posts: 45
Loc: CA
|
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.
|
Top
|
|
|
|
#192898 - 2009-03-15 02:02 AM
Re: edit XML file
[Re: Allen]
|
jeff_eisenberg
Fresh Scripter
Registered: 2009-02-26
Posts: 45
Loc: CA
|
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.
|
Top
|
|
|
|
#192899 - 2009-03-15 02:29 AM
Re: edit XML file
[Re: jeff_eisenberg]
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
|
Top
|
|
|
|
#192909 - 2009-03-15 08:09 PM
Re: edit XML file
[Re: Lonkero]
|
jeff_eisenberg
Fresh Scripter
Registered: 2009-02-26
Posts: 45
Loc: CA
|
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:
<?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.
$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
Edited by Benny69 (2009-03-15 08:52 PM) Edit Reason: Added Code Tags
|
Top
|
|
|
|
#192910 - 2009-03-15 11:52 PM
Re: edit XML file
[Re: jeff_eisenberg]
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
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:
<?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 ...
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
|
Top
|
|
|
|
#192911 - 2009-03-16 12:24 AM
Re: edit XML file
[Re: Shawn]
|
jeff_eisenberg
Fresh Scripter
Registered: 2009-02-26
Posts: 45
Loc: CA
|
Thanks!
The battle with the ever-elusive typo
Jeff.
|
Top
|
|
|
|
#212618 - 2017-08-10 04:52 PM
Re: edit XML file
[Re: jeff_eisenberg]
|
OneZero
Just in Town
Registered: 2015-02-03
Posts: 2
Loc: France
|
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
$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,
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 507 anonymous users online.
|
|
|