Page 3 of 4 <1234>
Topic Options
#135445 - 2005-03-18 01:10 AM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Regarding EnumXML(), here's where I'm at:

Code:
Function EnumXML($xml)
Dim $i,$a,$NodeList

$NodeList = $xml.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


;? "root: "$xml.documentElement.NodeName

;$a = xml_doc.getElementsByTagName("*")
;for each $i in $a
; item($i).text
; next
; for each $i in $xml.documentElement.childNodes
; Dim $k,$m
; ? "lvl1: " $xml.documentElement.NodeName + "/" + $i.nodeName
; ;GetXMLChildElements($xml,$xml.documentElement.NodeName + "/" + $i.nodeName)
; next
EndFunction


_________________________
-Jim

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

Top
#135446 - 2005-03-18 01:25 AM Re: RFC: ReadXmlString/WriteXmlString
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
We did originally perform the Load and Save in WriteXMLValue, loading and saving on every write, then just decided that that wasn't very efficient, so removed - didn't perform any bench-marking or anything, might not be too bad. Doing that load and save certainly makes the whole thing easier to use - for sure.
Top
#135447 - 2005-03-18 05:15 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
EDIT1: Renamed some stuff and added some stuff.
EDIT2: Added Remove* functions (Jose suggestion)
Renamed functions from *Attribute to *Attr

Shawn, if there's sufficient reason to, we can 'overload' these functions by allowing the source argument to be either a path\filename, an XMLDOM object, or an XML string. This raises the complexity of the functions, but also addresses performance and increases usability.

All, I'm envisioning these functions as a cross between the _profilestring functions and the registry functions. Kinda mimics real life as XML seems to be a cross between INI and a hierarchal db like the REG. Here's how I see it, please comment:

Core functions (We have a solid base on most of this already)
Code:
  

LoadXML() - Load an existing .XML file, instantiate XMLDOM object for use regardless of file existance.
SaveXML() - Saves an XMLDOM object
FormatXML() - Formats the output style of an XMLDOM object to the standard

EnumXMLElement() - Enums child elements of a given element
EnumXMLAttr() - Enums all attributes of an element

CreateXMLElement() - Creates an empty element pair
RemoveXMLElement() - Deletes an XML element (Recursively?)

WriteXMLValue() - Writes a value to an XML element
ReadXMLValue() - Reads the value of an XML element

WriteXMLAttr() - Writes an attribute to an XML element
ReadXMLAttr() - Read a specific attribute of an XML element
RemoveXMLAttr() - Deletes an XML element attribut


Secondary functions
Code:
 

ApplyXSL() - Applies an XSL template. (Much potential here for an expanded set, see
http://www.devguru.com/Technologies/xslt/quickref/xslt_index.html
or NTDOC's post above.
FilterXML() - Apply a filter to an XMLDOM object. (Search the db. XSL related.)



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

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

Top
#135448 - 2005-03-18 05:28 PM Re: RFC: ReadXmlString/WriteXmlString
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I'm cool with overloading, good idea, so the write overloads would look like:

WriteXmlValue(string, string, variant)

-and-

WriteXmlValue(object, string, variant)

Where if the first parm is a string, it means its a filename, and if its an object, means its a xml object. ja ?

Top
#135449 - 2005-03-18 05:40 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Bingo. Even more than that though.

WriteXmlValue(string, string, variant)
WriteXmlValue(object, string, variant)

and

WriteXmlValue(xmlstring, string, variant)

Where if instr(xmlstring,"<")
Not a filename, but an XML string of elements. So you could build your XML file in a string and then do a $=$objXML.loadXML($strXML) (note the loadXML method instead of .load)

I really haven't thought out what this would buy us as brainstorming doesn't lend well to rational thought. But, I think later it may handy when dealing with creating XSL on the fly or creating new .XML files from portions of existing XML.

I forgot to add WriteXmlValue() the previous list... Done.
_________________________
-Jim

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

Top
#135450 - 2005-03-18 05:48 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
And one more request... (don't crucify me) but can we possibly not use Optional args? I plan on rewriting these in .VBS and it just makes life easier.
_________________________
-Jim

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

Top
#135451 - 2005-03-18 06:55 PM Re: RFC: ReadXmlString/WriteXmlString - division of labor
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I'm going to start working on EnumXMLElement() and
EnumXMLAttribute(). If someone is writing code on this, please let us know what you're doing.

Both of these will return an array on success, or a null string otherwise (with errorlevel for further definition)
_________________________
-Jim

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

Top
#135452 - 2005-03-18 07:02 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Not me Jim I think we hould also add these two.

RemoveXMLElement()
RemoveXMLattr()

I have tryed the second one. Coudnt afford using optional arg Jim .


Code:
  

Function RemoveXMLattr($xml, $Path, $attr, optional $id)

Dim $SelectionTag,$AttrTag,$erase

$SelectionTag = $xml.getElementsByTagName($Path)

If $id=""
$id=0
EndIf

$AttrTag = $SelectionTag.item($id)

$erase = $AttrTag.removeAttribute($attr)

EndFunction



Posting sample.
_________________________
Life is fine.

Top
#135453 - 2005-03-18 07:14 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Good call Jose, I'll add them to that list.

Yeah, sometimes you can't get around using optionals. In that case, with .VBS, I force sending all arguments where optionals contain NULL.
_________________________
-Jim

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

Top
#135454 - 2005-03-18 07:24 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Te optional comes cause you could have a sample like this:
Code:

<?xml version="1.0" encoding="UTF-8" ?>
<Albums>
<Album ref="CD142" category="Folk">
<title>Boil The Breakfast Early</title>
<artist>The Chieftains</artist>
</Album>
<Album ref="CD720" category="Pop">
<title>Come On Over</title>
<artist>Shania Twain</artist>
</Album>
<Album ref="CD024" category="Country">
<title>Red Dirt Girl</title>
<artist>Emmylou Harris</artist>
</Album>
</Albums>



Code:
  
$=RemoveXMLattr($xml, "Albums/Album", "category", 0)


In wich case you have to indicate the att id to erase it. 0 should remove category="Folk" in this case.
Maybe would be better to search for the attr, get the id and then arease it if you dont now it.
_________________________
Life is fine.

Top
#135455 - 2005-03-18 07:39 PM Re: RFC: ReadXmlString/WriteXmlString
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Code:
 
<Album ref="CD142" category="Folk"> <title>Boil The Breakfast Early</title> <artist>The Chieftains</artist> </Album>



ahh just what I was wondering about earlier... Shawn, Jose's code above is exactly where I was going with my earlier question about the XML format you are expecting. In this example there are values defined within the Album (ref="CD142"), and then child attributes like title and artitist.

I'll be the first one to admit, I don't know a whole lot about XML, but would it be easiest to say the format of the XML is expected to be only one way or another, or are the UDFs already taking in consideration the altering data formats?

Top
#135456 - 2005-03-18 08:00 PM Re: RFC: ReadXmlString/WriteXmlString
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Pardon me for jumping in this late in the game. I thought I read early on that the objective was to "emulate" the INI file handling like WriteProfileString() does. Now I see it is more like WriteLine() whereby the file needs to be opened, read, written, closed, etc., and now something to remove elements? I know I did not read every word in this thread but I don't recall a change of objective stated anywhere, just creep.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#135457 - 2005-03-18 08:00 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Allen:
The thing about XML is that you are free to format it as you wish. There is not defined structure to use it as an mdb database.
You can have either different child names like this, used in a straight way as ini.
Code:
  

- <korg>
- <shawn>
<number>119</number>
</shawn>
- <jose>
<number>1772</number>
</jose>
- <jooel>
<number>2087</number>
</jooel>
</korg>



Or you can have same childs widh are defined by different attributes. Like this.
Code:
  

- <Albums>
- <Album ref="CD142" category="Folk">
<title>Boil The Breakfast Early</title>
<artist>The Chieftains</artist>
</Album>
- <Album ref="CD720" category="Pop">
<title>Come On Over</title>
<artist>Shania Twain</artist>
</Album>
- <Album ref="CD024" category="Country">
<title>Red Dirt Girl</title>
<artist>Emmylou Harris</artist>
</Album>
</Albums>



In wich case ref="CD142" is an attribute for that particular album.

This is what I have seen by now about XLM I think none of us is expert we are just getting into knowing it.
_________________________
Life is fine.

Top
#135458 - 2005-03-18 08:01 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Allen,

This is why I'm modeling after INI+REG. You can combine both styles, and most programmer do. So like the REG functions: EnumKey, EnumValues, do something. Given the current setup: EnumXMLElements, ReadXMLValue, EnumXMLAttr.

The extra step is required b/c XML is basically a combo of INI and hierarcal REG.
_________________________
-Jim

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

Top
#135459 - 2005-03-18 08:18 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Les, the *ProfileString do the same thing, although Kix does the work of open and close behind the scenes. Since we can't operate at that level, we have no choice but to Open, Read/Write, Close somewhere, either internal to the function or external to it.

I asked Shawn, in a previous post, why LoadXML and SaveXML were seperated, and he stated for effeciency. Our collective agreement has been to overload the functions so that both methods (writeline type and *profilestring) are available.
_________________________
-Jim

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

Top
#135460 - 2005-03-18 08:31 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
And.....I dont think that any other languajes that invoke XMLDOM are using it other that we are trying to use now, its XMLDOM way.
_________________________
Life is fine.

Top
#135461 - 2005-03-18 08:39 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Jim?
Quote:


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




What do you mean by Recursively? sorry if I dont get it.
_________________________
Life is fine.

Top
#135462 - 2005-03-18 08:47 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Jose, not sure if the delete methods of XMLDOM will allow you to delete an element that has children. Haven't done any research into it yet. I think it does which would make it recursive, in a sense.
_________________________
-Jim

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

Top
#135463 - 2005-03-18 08:52 PM Re: RFC: ReadXmlString/WriteXmlString
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Here's all of what we have so far. Some functions have been renamed from their original.

LoadXML() - Load an existing .XML file, instantiate XMLDOM object for use regardless of file existance.
SaveXML() - Saves an XMLDOM object
FormatXML() - Formats the output style of an XMLDOM object to the standard

EnumXMLElement() - Enums child elements of a given element
EnumXMLAttr() - Enums all attributes of an element

CreateXMLElement() - Creates an empty element pair
RemoveXMLElement() - Deletes an XML element (Recursively?)

WriteXMLValue() - Writes a value to an XML element
ReadXMLValue() - Reads the value of an XML element

WriteXMLAttr() - Writes an attribute to an XML element
ReadXMLAttr() - Read a specific attribute of an XML element
RemoveXMLAttr() - Deletes an XML element attribut


Secondary functions

ApplyXSL() - Applies an XSL template. (Much potential here for an expanded set, see
http://www.devguru.com/Technologies/xslt/quickref/xslt_index.html
or NTDOC's post above.
FilterXML() - Apply a filter to an XMLDOM object. (Search the db. XSL related.)


Code:
; 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)
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($xml, $Path, $Attr)
Dim $SelectionTag,$AttrName
$SelectionTag = $xml.getElementsByTagName($Path)
$AttrName = $SelectionTag.item(0)
If @Serror = "Member not found."
$ReadXMLAttr = ""
Exit @Error
Else
$ReadXMLAttr =$AttrName.getAttribute($Attr)
EndIf
EndFunction

;ReadXMLElement() - Reads the value of an XML element
Function ReadXMLValue($xml, $key, optional $defaultValue)
Dim $sectionNode
$sectionNode = $xml.SelectSingleNode($key)
If not $sectionNode
$ReadXmlValue = $defaultValue
Else
$ReadXmlValue = $sectionNode.FirstChild.Text
EndIf
EndFunction

; RemoveXMLAttr() - Deletes an XML element attribute
Function RemoveXMLAttr($xml, $Path, $attr, optional $id)
Dim $SelectionTag,$AttrTag,$erase
$SelectionTag = $xml.getElementsByTagName($Path)
If $id=""
$id=0
EndIf
$AttrTag = $SelectionTag.item($id)
$erase = $AttrTag.removeAttribute($attr)
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

; WriteXMLAttr() - Writes an attribute to an XML element
Function WriteXMLAttr($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

; WriteXMLValue() - Writes a value to an XML element
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


; ******************************************************************************
; 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



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

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

Top
#135464 - 2005-03-18 09:06 PM Re: RFC: ReadXmlString/WriteXmlString
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
This is quite an achievement man!!
From nothing we have plenty of stuff now.



About the delete method I think it should delete both element and children I will se if I can get along with it, if not I shout it out.

Ahh...remember to update Write-ReadXMLAttr() for the languaje issue. Remember "Member not found."?



Edited by Jose (2005-03-18 09:15 PM)
_________________________
Life is fine.

Top
Page 3 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.108 seconds in which 0.038 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