Page 1 of 1 1
Topic Options
#192896 - 2009-03-14 06:00 PM edit XML file
jeff_eisenberg Offline
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
#192897 - 2009-03-14 06:10 PM Re: edit XML file [Re: jeff_eisenberg]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.
Top
#192898 - 2009-03-15 02:02 AM Re: edit XML file [Re: Allen]
jeff_eisenberg Offline
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 Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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
_________________________
!

download KiXnet

Top
#192902 - 2009-03-15 04:36 AM Re: edit XML file [Re: Lonkero]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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

Top
#192903 - 2009-03-15 04:58 AM Re: edit XML file [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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 \:\)
_________________________
!

download KiXnet

Top
#192909 - 2009-03-15 08:09 PM Re: edit XML file [Re: Lonkero]
jeff_eisenberg Offline
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:
 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	



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 Offline
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:

 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

Top
#192911 - 2009-03-16 12:24 AM Re: edit XML file [Re: Shawn]
jeff_eisenberg Offline
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 Offline
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

 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,

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.058 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org