Page 4 of 4 <1234
Topic Options
#135465 - 2005-03-18 09:27 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Damn. I thought I did...

Here's the Overload code implemented into a function. Works great. I guess I'll retrofit all of the function to use this before I tackle the other funcs. I'll update the post aobve with the language changes and this.
Code:
; WriteXMLValue() - Writes a value to an XML element
Function WriteXMLValue($source, $path, $value)
dim $, $p, $rootNode, $sectionNode, $parentNode, $childNode, $node, $xml
; Begin Overloaded Var Sorting
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; End Overloaded Var Sorting

$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

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed. Since the object was passed by ref, no further
; action necessary.
Else
If Instr($source,"<")=0
; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
$xml = 0
Else
; An XML string was passed
; Cannot save the XML string as we have no file name to save to
; so we return it. Since the object was passed by ref, no further
; action necessary.

EndIf
EndIf
; End Overloaded Var Exit Strategy
endfunction

_________________________
-Jim

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

Top
#135466 - 2005-03-19 12:24 AM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Ok, I'm not updating any one particular post anymore, it's too confusing. Here's all of the latest code that I have. Still a lot of work to be done, and I wont be doing any over the weekend if anyone is bored, but we have a basic working model.

Code:
Break On

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

Dim $filename1, $filename2, $filename3
Dim $xml, $

; This is an example by object
$filename1 = "C:\KiXtartByObject.xml"

$xml = LoadXml($filename1)

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

$= WriteXMLAttr($xml, "korg/jim", "Id", 3555)
$= WriteXMLAttr($xml, "korg/jim", "Age", 31)

SaveXml($xml, $filename1)
$xml = 0

; This is an example by filename
$filename2 = "C:\KiXtartByFileName.xml"

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

$= WriteXMLAttr($filename2, "korg/jim", "Id", 3555)
$= WriteXMLAttr($filename2, "korg/jim", "Age", 31)

? "Jim's age: " ReadXMLAttr($filename2, "korg/jim", "Age")
? "Jose's Number: " ReadXmlValue($filename2, "korg/jose/number")

$xml = 0


; This is an example by XML String
$filename3 = "C:\KiXtartByXMLString.xml"
Dim $strXML,$objXML

$strXML = "<korg><jim><E-mail>jim@@foo.com</E-mail></jim></korg>"
$strXML = WriteXmlValue($strXML, "korg/jim/id",3555)
; Note that after the initial write, the return becomes an object, therefore
; capturing the returns isn't necessary, but doesn't hurt either and is consistent.
$strXML = WriteXmlValue($strXML, "korg/jim/height",6)
$strXML = WriteXmlValue($strXML, "korg/jim/weight",230)
$strXML = WriteXmlValue($strXML, "korg/jim/eyes",2)

$strXML = WriteXMLAttr($strXML, "korg/jim", "Age", 31)
$strXML = WriteXMLAttr($strXML, "korg/jim", "Drink", "Beer")

? "Jim's age: " ReadXMLAttr($strXML, "korg/jim", "Age")


SaveXml($strXML, $filename3)

? "Jim's Drink: " ReadXMLAttr($filename3, "korg/jim", "Age")
$ = RemoveXMLAttr($filename3, "korg/jim", "Age")
? "Jim's Drink: " ReadXMLAttr($filename3, "korg/jim", "Age")

$strXML = 0


exit 1


; CreateXMLElement() - Creates an empty element pair
Function CreateXMLElement()

EndFunction

; EnumXMLAttr() - Enums all attributes of an element
Function EnumXMLAttr()

EndFunction

; EnumXMLElement() - Enums child elements of a given element
Function EnumXMLElement()

EndFunction

;FormatXML() - Formats the output style of an XMLDOM object to the standard
Function FormatXML($objXMLDOM)

;? "V" vartype($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

;LoadXML() - Load an existing .XML file, instantiate XMLDOM object for use
;regardless of file existance.
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

; ReadXMLAttr() - Read a specific attribute of an XML element
Function ReadXMLAttr($source, $Path, $Attr)
Dim $SelectionTag,$AttrName,$,$xml
; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$SelectionTag = $xml.getElementsByTagName($Path)
$AttrName = $SelectionTag.item(0)
If @ERROR = "-2147352573"
$ReadXMLAttr = ""
Exit @Error
Else
$ReadXMLAttr =$AttrName.getAttribute($Attr)
EndIf

; Begin Overloaded Var Exit Strategy
; None needed
; End Overloaded Var Exit Strategy
EndFunction

;ReadXMLElement() - Reads the value of an XML element
Function ReadXMLValue($source, $key, optional $defaultValue)
Dim $sectionNode,$xml,$

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf

; *** End Overloaded Var Sorting ***

$sectionNode = $xml.SelectSingleNode($key)
If not $sectionNode
$ReadXmlValue = $defaultValue
Else
$ReadXmlValue = $sectionNode.FirstChild.Text
EndIf
EndFunction

; RemoveXMLAttr() - Deletes an XML element attribute
Function RemoveXMLAttr($source, $Path, $attr, optional $id)
Dim $SelectionTag,$AttrTag,$erase,$,$xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$SelectionTag = $xml.getElementsByTagName($Path)
If $id=""
$id=0
EndIf
$AttrTag = $SelectionTag.item($id)
$erase = $AttrTag.removeAttribute($attr)

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$RemoveXMLAttr = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$RemoveXMLAttr = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy

EndFunction

; RemoveXMLElement() - Deletes an XML element (Recursively?)
Function RemoveXMLElement()

EndFunction

;SaveXML() - Saves an XMLDOM object to a file
; Requires: FormatXML()
Function SaveXML($xml, $filename)
$xml = FormatXML($xml)
$SaveXml = $xml.Save($filename)
EndFunction

Function WriteXMLAttr($source, $Path, $Attr, $Value)
Dim $SelectionTag,$AttrTag,$,$Write,$xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$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)

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$WriteXMLAttr = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$WriteXMLAttr = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy
EndFunction

; WriteXMLValue() - Writes a value to an XML element
Function WriteXMLValue($source, $path, $value)
dim $, $p, $rootNode, $sectionNode, $parentNode, $childNode, $node, $xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$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

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$WriteXMLValue = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$WriteXMLValue = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy

endfunction


; ******************************************************************************
; ApplyXSL() - Applies an XSL template. (Much potential here for an expanded
; set, see http://www.devguru.com/Technologies/xslt/quickref/xslt_index.html
Function ApplyXSL()

EndFunction

; FilterXML() - Apply a filter to an XMLDOM object. (Search the db. XSL related.)
Function FilterXML()

EndFunction


Function EnumXML($source)
dim $nodelist,$i
$NodeList = $source.getElementsByTagName("*")
For Each $i In $NodeList
? "Element " +$i.tagName
;? "attributes: " $i.attributes
;? "baseName: " $i.baseName
;? "childNodes: " $i.childNodes
;? "dataType: " $i.dataType
;? "definition: " $i.definition
;? "firstChild: " $i.firstChild.nodeName
;? "lastChild: " $i.lastChild.nodeName
;? "namespaceURI: " $i.namespaceURI
;? "nextSibling: " $i.nextSibling.nodeName
;? "nodeName: " $i.nodeName
;? "nodeType: " $i.nodeType
;? "nodeTypeString: " $i.nodeTypeString
;? "nodeValue: " $i.nodeValue
;? "ownerDocument: " $i.ownerDocument
;? "parentNode: " $i.parentNode.nodeName
;? "parsed: " $i.parsed
;? "prefix: " $i.prefix
;? "previousSibling: " $i.previousSibling.nodeName
;? "specified: " $i.specified
;? "tagName: " $i.tagName
;? "text: " $i.text
? "xml: " $i.xml
?
Next
EndFunction

_________________________
-Jim

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

Top
#135467 - 2005-03-23 01:13 AM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I think wraps it up, except for the XSL stuff. The ENUM's return collections. Haven't done a decent amount of testing with all of this and error checking is weak.

Code:
Break On
$nul=SetOption("ASCII","On")
$nul=SetOption("Explicit","On")
$nul=SetOption("WrapAtEOL","On")

Dim $filename1, $filename2, $filename3
Dim $xml, $

; This is an example by object
$filename1 = "C:\KiXtartByObject.xml"

$xml = LoadXml($filename1)

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

$= WriteXMLAttr($xml, "korg/jim", "Id", 3555)
$= WriteXMLAttr($xml, "korg/jim", "Age", 31)

$= WriteXMLAttr($xml, "korg/jose", "Id", 4565)
$= WriteXMLAttr($xml, "korg/jose", "Age", 22)

$= WriteXMLAttr($xml, "korg/admins/jim", "Id", 1234)
$= WriteXMLAttr($xml, "korg/admins/jim", "Age", 58)

SaveXml($xml, $filename1)
$xml = 0

; This is an example by filename
$filename2 = "C:\KiXtartByFileName.xml"

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

$= WriteXMLAttr($filename2, "korg/jim", "Id", 3555)
$= WriteXMLAttr($filename2, "korg/jim", "Age", 31)

? "Jim's age: " ReadXMLAttr($filename2, "korg/jim", "Age")
? "Jose's Number: " ReadXmlValue($filename2, "korg/jose/number")

$xml = 0


; This is an example by XML String
$filename3 = "C:\KiXtartByXMLString.xml"
Dim $strXML,$objXML

$strXML = "<korg><jim><E-mail>jim@@foo.com</E-mail></jim></korg>"
$strXML = WriteXmlValue($strXML, "korg/jim/id",3555)
; Note that after the initial write, the return becomes an object, therefore
; capturing the returns isn't necessary, but doesn't hurt either and is consistent.
$strXML = WriteXmlValue($strXML, "korg/jim/height",6)
$strXML = WriteXmlValue($strXML, "korg/jim/weight",230)
$strXML = WriteXmlValue($strXML, "korg/jim/eyes",2)

$strXML = WriteXMLAttr($strXML, "korg/jim", "Age", 31)
$strXML = WriteXMLAttr($strXML, "korg/jim", "Drink", "Beer")

? "Jim's age: " ReadXMLAttr($strXML, "korg/jim", "Age")
? "Jim's Drink: " ReadXMLAttr($filename3, "korg/jim", "Drink")

SaveXml($strXML, $filename3)

$ = RemoveXMLAttr($filename3, "korg/jim", "Age")
? "Jim's Age: " ReadXMLAttr($filename3, "korg/jim", "Age")

$strXML = 0

; Enumerate elements and attributes
Dim $colE
Dim $colXMLElements
Dim $colAttrs,$colAttr

?? "Enuming All XML Elements"
; Return all elements in the XML file
$colXMLElements = EnumXMLElement($filename1,"*")
For Each $colE In $colXMLElements
? "XML Element Name : " + $colE.TagName
;? "XML Element Value: " + $colE.Text
Next

?? "Enuming Child XML Elements of korg"
; Return all elements in the XML file
$colXMLElements = EnumXMLElement($filename1,"korg/*")
For Each $colE In $colXMLElements
? "XML Element Name : " + $colE.TagName
;? "XML Element Value: " + $colE.Text
Next

?? "Enuming Child XML Elements of korg/shawn"
; Return all elements in the XML file
$colXMLElements = EnumXMLElement($filename1,"korg/shawn/*")
For Each $colE In $colXMLElements
? "XML Element Name : " + $colE.TagName
;? "XML Element Value: " + $colE.Text
Next

?? "Enuming XML Number Elements and their values"
; Return all "number" elements and their value
$colXMLElements = EnumXMLElement($filename1,"number")
For Each $colE In $colXMLElements
? "XML Element Name : " + $colE.TagName
? "XML Element Value: " + $colE.Text
Next

?? "Enuming XML Elements With Attributes and the attribute values (Without EnumXMLAttr)"
; Return all elements that have attributes and enum the attr the long way
$colXMLElements = EnumXMLElement($filename1,"*")
For Each $colE In $colXMLElements
If $colE.Attributes.length > 0
? "XML Element Name : " + $colE.TagName
Dim $strAttr
For Each $strAttr In $colE.Attributes
? " XML Attribute Name : " + $strAttr.Name
? " XML Attribute Value: " + $strAttr.Value
Next
EndIf
Next

?? "Enuming XML Elements With Attributes and the attribute values (With EnumXMLAttr)"
; Return all elements that have attributes and enum the attr the long way
$colXMLElements = EnumXMLElement($filename1,"*")
For Each $colE In $colXMLElements
If $colE.Attributes.length > 0
? "XML Element Name : " + $colE.TagName
$colAttrs = EnumXMLAttr($filename1,$colE.parentNode.nodeName+ "/" + $colE.TagName)
For Each $colAttr In $colAttrs
? "XML Attribute Name : " + $colAttr.Name
? "XML Attribute Value: " + $colAttr.Value
Next
EndIf
Next

?? "Enuming XML Attributes and the attribute values of a specific element (precise path)"
$colAttrs = EnumXMLAttr($filename1,"korg/admins/jim")
For Each $colAttr In $colAttrs
? "XML Attribute Name : " + $colAttr.Name
? "XML Attribute Value: " + $colAttr.Value
Next

?? "Enuming XML Attributes and the attribute values of a specific element (parent only path)"
$colAttrs = EnumXMLAttr($filename1,"admins/jim")
For Each $colAttr In $colAttrs
? "XML Attribute Name : " + $colAttr.Name
? "XML Attribute Value: " + $colAttr.Value
Next

?? "Enuming XML Attributes and the attribute values of the 1st element found called 'Jim'"
$colAttrs = EnumXMLAttr($filename1,"jim")
For Each $colAttr In $colAttrs
? "XML Attribute Name : " + $colAttr.Name
? "XML Attribute Value: " + $colAttr.Value
Next


?? "Creating the forums..."

$=CreateXMLElement($filename1,"korg/forums/starters")
$=CreateXMLElement($filename1,"korg/forums/scripts")
$=CreateXMLElement($filename1,"korg/forums/General Discussion")
$=CreateXMLElement($filename1,"korg/forums/COM")
$=CreateXMLElement($filename1,"korg/forums/UDF")
$=CreateXMLElement($filename1,"korg/forums/UDFs")
$=CreateXMLElement($filename1,"korg/forums/Beta")
$=CreateXMLElement($filename1,"korg/forums/Examples")

?? "Listing forums"
$colXMLElements = EnumXMLElement($filename1,"korg/forums/*")
For Each $colE in $colXMLElements
? "Forum Name : " + $colE.TagName
Next

?? "Deleting error"
$ = RemoveXMLElement($filename1,"forums/UDF")


?? "Listing forums again"
$colXMLElements = EnumXMLElement($filename1,"korg/forums/*")
For Each $colE in $colXMLElements
? "Forum Name : " + $colE.TagName
Next

?? "Deleting all forums"
$ = RemoveXMLElement($filename1,"forums")

?? "Listing forums again"
$colXMLElements = EnumXMLElement($filename1,"korg/forums/*")
For Each $colE in $colXMLElements
? "Forum Name : " + $colE.TagName
Next
exit 1


; CreateXMLElement() - Creates an empty element pair
Function CreateXMLElement($source,$path)
$CreateXMLElement = WriteXMLValue($source,$path,"")
EndFunction

; EnumXMLAttr() - Enums all attributes of an element
Function EnumXMLAttr($source,$path)
Dim $xml,$

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***
Dim $colXMLElements,$colE
$colXMLElements = $xml.getElementsByTagName($path)
If $colXMLElements.item(0).Attributes.length > 0
$EnumXMLAttr = $colXMLElements.item(0).Attributes
EndIf
EndFunction

; EnumXMLElement() - Enums child elements of a given element
Function EnumXMLElement($source,$path)
Dim $xml,$

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***
;Select
; Case Right($path,1)= "/"
; $path = Left($path,Len($path)-1)
; ;? "path "$path
; $EnumXMLElement = $xml.getElementsByTagName($path).Item(0)
; $EnumXMLElement = $EnumXMLElement.ChildNodes
; Case 1
; $EnumXMLElement = $xml.getElementsByTagName($path)
;EndSelect
$EnumXMLElement = $xml.getElementsByTagName($path)
EndFunction

;FormatXML() - Formats the output style of an XMLDOM object to the standard
Function FormatXML($objXMLDOM)

;? "V" vartype($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

;LoadXML() - Load an existing .XML file, instantiate XMLDOM object for use
;regardless of file existance.
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

; ReadXMLAttr() - Read a specific attribute of an XML element
Function ReadXMLAttr($source, $Path, $Attr)
Dim $SelectionTag,$AttrName,$,$xml
; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$SelectionTag = $xml.getElementsByTagName($Path)
$AttrName = $SelectionTag.item(0)
If @ERROR = "-2147352573"
$ReadXMLAttr = ""
Exit @Error
Else
$ReadXMLAttr =$AttrName.getAttribute($Attr)
EndIf

; Begin Overloaded Var Exit Strategy
; None needed
; End Overloaded Var Exit Strategy
EndFunction

;ReadXMLElement() - Reads the value of an XML element
Function ReadXMLValue($source, $key, optional $defaultValue)
Dim $sectionNode,$xml,$

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$sectionNode = $xml.SelectSingleNode($key)
If not $sectionNode
$ReadXmlValue = $defaultValue
Else
$ReadXmlValue = $sectionNode.FirstChild.Text
EndIf
EndFunction

; RemoveXMLAttr() - Deletes an XML element attribute
Function RemoveXMLAttr($source, $Path, $attr, optional $id)
Dim $SelectionTag,$AttrTag,$erase,$,$xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$SelectionTag = $xml.getElementsByTagName($Path)
If $id=""
$id=0
EndIf
$AttrTag = $SelectionTag.item($id)
$erase = $AttrTag.removeAttribute($attr)

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$RemoveXMLAttr = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$RemoveXMLAttr = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy

EndFunction

; RemoveXMLElement() - Deletes an XML element (Recursively?)
Function RemoveXMLElement($source,$path)
Dim $SelectionTag,$AttrTag,$,$xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$SelectionTag = $xml.getElementsByTagName($path)
Dim $i

For $i=0 To $SelectionTag.Length - 1
;? "Index "$i
;? "Removing "$SelectionTag.item($i).TagName
;? "Parent " $SelectionTag.item($i).ParentNode.TagName
$=$SelectionTag.item($i).ParentNode.RemoveChild($SelectionTag.item($i))
;? @Serror
Next

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$RemoveXMLElement = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$RemoveXMLElement = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy
EndFunction

;SaveXML() - Saves an XMLDOM object to a file
; Requires: FormatXML()
Function SaveXML($xml, $filename)
$xml = FormatXML($xml)
$SaveXml = $xml.Save($filename)
EndFunction

Function WriteXMLAttr($source, $Path, $Attr, $Value)
Dim $SelectionTag,$AttrTag,$,$Write,$xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$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)

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$WriteXMLAttr = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$WriteXMLAttr = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy
EndFunction

; WriteXMLValue() - Writes a value to an XML element
Function WriteXMLValue($source, $path, $value)
dim $, $p, $rootNode, $sectionNode, $parentNode, $childNode, $node, $xml

; *** Begin Overloaded Var Sorting ***
If VarType($source) = 9
; An object was passed
$xml = $source
Else
If Instr($source,"<")=0
; A file name was passed
$xml = LoadXML($source)
Else
; An XML string was passed
$xml = CreateObject("Microsoft.XMLDOM")
$xml.async = false
$=$xml.loadXML($source)
EndIf
EndIf
; *** End Overloaded Var Sorting ***

$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

; Begin Overloaded Var Exit Strategy
If VarType($source) = 9
; An object was passed.
$WriteXMLValue = $xml
Else
If Instr($source,"<")=0

; A file name was passed, so we write it out and destory the object
SaveXML($xml, $source)
Else
; An XML string was passed.
$WriteXMLValue = $xml

EndIf
EndIf
; End Overloaded Var Exit Strategy

endfunction


; ******************************************************************************
; ApplyXSL() - Applies an XSL template. (Much potential here for an expanded
; set, see http://www.devguru.com/Technologies/xslt/quickref/xslt_index.html
Function ApplyXSL()

EndFunction

; FilterXML() - Apply a filter to an XMLDOM object. (Search the db. XSL related.)
Function FilterXML()

EndFunction


Function EnumXML($source)
dim $nodelist,$i
$NodeList = $source.getElementsByTagName("*")
For Each $i In $NodeList
? "Element " +$i.tagName
;? "attributes: " $i.attributes
;? "baseName: " $i.baseName
;? "childNodes: " $i.childNodes
;? "dataType: " $i.dataType
;? "definition: " $i.definition
;? "firstChild: " $i.firstChild.nodeName
;? "lastChild: " $i.lastChild.nodeName
;? "namespaceURI: " $i.namespaceURI
;? "nextSibling: " $i.nextSibling.nodeName
;? "nodeName: " $i.nodeName
;? "nodeType: " $i.nodeType
;? "nodeTypeString: " $i.nodeTypeString
;? "nodeValue: " $i.nodeValue
;? "ownerDocument: " $i.ownerDocument
;? "parentNode: " $i.parentNode.nodeName
;? "parsed: " $i.parsed
;? "prefix: " $i.prefix
;? "previousSibling: " $i.previousSibling.nodeName
;? "specified: " $i.specified
;? "tagName: " $i.tagName
;? "text: " $i.text
? "xml: " $i.xml
?
Next
EndFunction



Edited by jtokach (2005-03-23 01:14 AM)
_________________________
-Jim

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

Top
#135468 - 2005-03-23 08:38 PM Re: RFC: ReadXmlString/WriteXmlString
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well not exactly in the KiX coding area thought I'd pass along these links for doing, learning XML data with Excel.

Excel 2003 Add-in: XML Tools Add-in
Overview
Learn how to use the Microsoft Office Excel 2003 XML Tools Add-in Version 1.1. With it, you can provide information about a selected cell's XML properties, create XSD files for XML maps, rename the Root and Row elements, or refresh all of the XML maps in a workbook at once.
http://www.microsoft.com/downloads/detai...;displaylang=en

Using the Excel 2003 XML Tools Add-in
James Rivera and Frank Rice show how this new add-in makes working with XML maps, cells, and ranges much easier for developers. (February 2005)
http://msdn.microsoft.com/office/underst...mltooladdin.asp


Part 1: Automating the XML Data Mapping Process in Excel 2003
Learn how to use XML maps to customize Excel as a data input and display system. (March 2005)
http://msdn.microsoft.com/office/underst...ata_mapping.asp

Part 2: Mapping XML from SQL Server to a Single Cell in Excel 2003
In the second of this three-part series, learn how to map well-formed XML from a standard SQL Server query to a single cell in a worksheet. The resulting output is an XML tree with non-repeating elements. (March 2005)
http://msdn.microsoft.com/office/underst..._sql_server.asp

Create XML Mappings in Excel 2003
Walk through common XML tasks in Excel 2003 to learn more about the new XML functionality in Office 2003 Editions. Learn how to add custom schemas, work with XML maps in Excel, and create a series of mappings based on various schemas. (February 2005)
http://msdn.microsoft.com/office/underst...ngscenarios.asp

How Excel 2003 Infers XSDs when Importing XML Data
Review the rules and instances where Excel 2003 infers schema from XML data during import and export. Understand why Excel creates the schemas it does and then learn to modify your own XML data. (February 2005)
http://msdn.microsoft.com/office/underst...nferdetails.asp

Top
#135469 - 2005-03-23 10:23 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Hmmm... The rating on this post went from 5 to 3 three stars... Someone is obviously not happy with something. Please speak your mind rather than let this go down the wrong development path.
_________________________
-Jim

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

Top
#135470 - 2005-03-25 06:01 AM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Hey Jim wonderfull work man!!. Very complete piece of UDF´s.
Sorry I didnt have the time to give you some feedback but I played with it and looks very good. I see you have dealed with RemoveXMLElement() good.


So far..


    LoadXML()
    FormatXML()

    WriteXmlValue()
    CreateXMLElement()
    WriteXMLAttr()

    ReadXMLValue()
    ReadXMLAttr()

    EnumXMLElement()
    EnumXMLAttr()

    RemoveXMLAttr()
    RemoveXMLElement()

    SaveXML()




About XML I have seen resistance notisted at suggestion forum but I think this is kinda normal not specially with XML but for other languajes known as foreings to Kixtart FE Flash,java etc. But it is understandable cause kix makes you love it jealously.

What I really see positive about this thead it that lot have been done since your first suggetion.


Quote:


Not sure if this is practical, but I'll throw it out there. With .XML becoming more and more prevalent, wouldn't it be nice to have functions to manipulate .XML files like the functions we have to manipulate .INI files?





Well...acomplish has been made. A rate is deserved.
Now its time to wait for dear Shawn to speack up about posting as UDF´s. Me going to country side till monday and that makes me happy.

DOC: I dont have Excel 2003 so as to try your links.
_________________________
Life is fine.

Top
#135471 - 2005-03-25 07:25 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Thanks Jose! I was beginning to think this would never see the light of day. Thank Shawn and Chris for renewed spark of interest; add a little elbow grease and voila.

Hope you enjoy your mini-vacation! I'll be writing Use-Case scenarios this weekend for a class I'm taking. That sucks.

Shawn, I suppose we shouldv'e put together a use-case diagram before hammering these out.
_________________________
-Jim

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

Top
#135472 - 2005-03-25 07:39 PM Re: RFC: ReadXmlString/WriteXmlString
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
use-case diagram ? that anything like flow-charts and gant-charts ? ;0) (idk)

I have been incorporating these XML ideas/strategies into my GRID script (a kinda Excel work-alike) ... the raw native XML functions work very fast - much faster than using INI's ... but ... the UDF's are much slower, even slower then INI's ... has to do with the ReadXmlValue UDF me thinks ... think that one needs a bit of tweaking ...

When done, will give the UDF's a try (again), and apply any lesson's learned to them, hopefully make them much faster than INI's ...

-Shawn

Top
#135473 - 2005-03-25 07:51 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Use-Cases are part of UML somewhere. See:
Link
I'll take a look at ReadXmlValue()
_________________________
-Jim

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

Top
#135474 - 2005-03-25 08:40 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Starting a new thread here.

Edited by jtokach (2005-03-25 08:41 PM)
_________________________
-Jim

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

Top
Page 4 of 4 <1234


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

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.064 seconds in which 0.025 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