Hail to the King Baby!

-Formatting hooked up! (Needed to apply XSL)
-Updated all code for Option Explicit

Code:
break on

$nul=SetOption("ASCII","On")
$nul=SetOption("Explicit","On")

Dim $filename,$xml,$

$filename = "C:\HailToTheKingBaby.xml"

$xml = LoadXml($filename)

$= WriteXmlValue($xml, "korg/shawn/number", 119)
$= WriteXmlValue($xml, "korg/jose/number", 1772)
$= WriteXmlValue($xml, "korg/jooel/number", 2087)

?"Value=" ReadXmlValue($xml, "korg/jose/number")

SaveXml($xml, $filename)
$xml = 0
exit 1

function LoadXml($filename)
dim $, $rootNode, $objNewPI, $strType

$loadXml = CreateObject("Microsoft.XMLDOM")
$loadXml.async = False
$loadXml.preserverwhitespace = True

if not $loadXml
return
endif

$ = $loadXml.Load($filename)
endfunction


Function FormatXML($objXMLDOM)
Dim $,$strXSL,$objXSL
;Create and Load XSL
; <?xml version="1.0" encoding="UTF-8"?>
; <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
; <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
; <xsl:template match="/ | @* | node()">
; <xsl:copy>
; <xsl:apply-templates select="@* | node()" />
; </xsl:copy>
; </xsl:template>
; </xsl:stylesheet>

$strXSL = "<?xml version=" + chr(34) + "1.0" + chr(34) + " encoding=" +
chr(34) + "UTF-8" + chr(34) + "?>" + @CRLF
$strXSL = $strXSL + "<xsl:stylesheet version=" + chr(34) + "1.0" + chr(34) +
" xmlns:xsl=" + chr(34) + "http://www.w3.org/1999/XSL/Transform" +
chr(34) + ">" + @CRLF
$strXSL = $strXSL + "<xsl:output method=" + chr(34) + "xml" + chr(34) +
" version=" + chr(34) + "1.0" + chr(34) + " encoding=" + chr(34) +
"UTF-8" + chr(34) + " indent=" + chr(34) + "yes" + chr(34) + "/>" + @CRLF
$strXSL = $strXSL + "<xsl:template match=" + chr(34) + "/ | @@* | node()" +
chr(34) + ">" + @CRLF
$strXSL = $strXSL + "<xsl:copy>"+ @CRLF
$strXSL = $strXSL + "<xsl:apply-templates select=" + chr(34) +
"@@* | node()" + chr(34) + " />"+ @CRLF
$strXSL = $strXSL + "</xsl:copy>"+ @CRLF
$strXSL = $strXSL + "</xsl:template>"+ @CRLF
$strXSL = $strXSL + "</xsl:stylesheet>"

;? "XLM Stylesheet: " + @CRLF + $strXSL

$objXSL = CreateObject("Microsoft.XMLDOM")
$objXSL.async = false
$=$objXSL.loadXML($strXSL)

;Transform file
$objXMLDOM.TransformNodeToObject ($objXSL, $objXMLDOM)
$formatXML = $objXMLDOM
EndFunction


function WriteXmlValue($xml, $path, $value)
dim $p, $rootNode, $sectionNode, $parentNode, $childNode,$node
$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)
$xml = formatXML($xml)
$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



Edited by jtokach (2005-03-18 08:06 PM)
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.