#135438 - 2005-03-16 10:37 PM
Re: RFC: ReadXmlString/WriteXmlString
|
jtokach
Seasoned Scripter
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
Quote:
Jim, not too sure I would dismiss XML so quickly. Personally, I'm hoping to get some screaming performance gains versus the builtin WriteLine and WriteProfileString functions. Although I have no data to back this up, the Kixtart builtins are slow as hell (especially writing stuff). Think some benchmarking is in order.
If it wasn't so complicated, I wouldn't mind so much, but it sucks and something seems to be unpredictable. Again, I renew my request for builtin xml support in Kix. =)
Quote:
The other thing ... wish I could figure-out a way to get the output XML properly formatted. When notepadding the result, all the data is scrunched into one long line. Must be a way to introduce some formatting in there.
I posted this in the previous post. It's written in VB. It is terribly inconvenient but not sure it's that big of an issue. We'll need to stick to IE until someone can translate that VB code. I started, but I can't find a suitable replacement for the TypeOf statements, yet...
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
Top
|
|
|
|
#135439 - 2005-03-17 06:09 PM
Re: RFC: ReadXmlString/WriteXmlString
|
jtokach
Seasoned Scripter
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
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.
|
Top
|
|
|
|
#135440 - 2005-03-17 06:57 PM
Re: RFC: ReadXmlString/WriteXmlString
|
jtokach
Seasoned Scripter
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
Jose, I updated your attrib UDF's. Also renamed the one from Rear to Read (I assumed that was a typo.)
Code:
Function WriteXMLattrib($xml, $Path, $Attr, $Value)
Dim $SelectionTag,$AttrTag,$,$eu
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrTag = $SelectionTag.item(0)
If @Serror = "Member not found."
$= WriteXmlValue($xml, $path, "")
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrTag = $SelectionTag.item(0)
EndIf
$eu = $AttrTag.SetAttribute($Attr,$Value)
EndFunction
Function ReadXMLattrib($xml, $Path, $Attr)
Dim $SelectionTag,$AttrName
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrName = $SelectionTag.item(0)
If @Serror = "Member not found."
$ReadXMLattrib = ""
Exit @Error
Else
$ReadXMLattrib =$AttrName.getAttribute($Attr)
EndIf
EndFunction
Edited by jtokach (2005-03-17 06:59 PM)
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
Top
|
|
|
|
#135441 - 2005-03-17 07:07 PM
Re: RFC: ReadXmlString/WriteXmlString
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
|
Top
|
|
|
|
#135442 - 2005-03-17 08:20 PM
Re: RFC: ReadXmlString/WriteXmlString
|
Jose
Seasoned Scripter
Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
|
|
Top
|
|
|
|
#135443 - 2005-03-17 09:22 PM
Re: RFC: ReadXmlString/WriteXmlString
|
Jose
Seasoned Scripter
Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
|
Jim: I have changed two things in the attrib functions. - I have spanish languaje so changed to @error in case path not exist. - took the $eu off and replaced with $Write
Code:
Function WriteXMLattrib($xml, $Path, $Attr, $Value)
Dim $SelectionTag,$AttrTag,$,$Write
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrTag = $SelectionTag.item(0) If @ERROR = "-2147352573"
$= WriteXmlValue($xml, $path, "")
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrTag = $SelectionTag.item(0)
EndIf
$write = $AttrTag.SetAttribute($Attr,$Value)
EndFunction
Function ReadXMLattrib($xml, $Path, $Attr)
Dim $SelectionTag,$AttrName
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrName = $SelectionTag.item(0)
If @ERROR = "-2147352573"
$ReadXMLattrib = ""
Exit @Error
Else
$ReadXMLattrib =$AttrName.getAttribute($Attr)
EndIf
EndFunction
_________________________
Life is fine.
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 566 anonymous users online.
|
|
|