Page 1 of 1 1
Topic Options
#127263 - 2004-09-30 06:05 PM RFC: fnLoadXML() - Loads & validates XML documents
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Ok, to take the discussion regarding XML to a new level here is a function that will load an XML document from a local file, from the internet, or from a string.

It uses Microsoft.XMLDOM, which is available with all versions of IE from ver 5 and up.

To learn more about XML DOM you can review the following resources:

http://www.w3schools.com/dom/default.asp
http://www.devguru.com/Technologies/xmldom/quickref/xmldom_index.html

Currently, this function returns a string value representing the XML document. This can then be loaded into another XML parser for further manipulation. I could also just return the object itself and then that object could be further manipulated.

Anyway, I'd like to post this for further comment.

Code:

Break On
$nul=SetOption("WrapAtEOL","on")

$sFromFile = "mycomputer.xml"
$sFromHttp = "http://www.nytimes.com/services/xml/rss/userland/Books.xml"
$sFromString = '
<remoteDir>
<dir>1394</dir>
<dir>backoffice</dir>
<dir>Clients</dir>
<dir>distapps</dir>
<dir>exchange</dir>
<dir>GEN_INFO</dir>
<dir>IIS</dir>
<dir>lanman</dir>
<dir>mail</dir>
<dir>mcis</dir>
<dir>mspress</dir>
<file>
<name>readme.txt</name>
<size>1715</size>
<lastModTime m="5" d="20" y="1996" hh="0" mm="0" ss="0" />
</file>
<file>
<name>ReadMe1.txt</name>
<size>2107</size>
<lastModTime m="7" d="2" y="2002" hh="0" mm="0" ss="0" />
</file>
<dir>sitesrv</dir>
<dir>sql</dir>
<dir>Sysmgrsrv</dir>
<dir>utilities</dir>
<dir>viper</dir>
<dir>winnt</dir>
<dir>WinSock</dir>
</remoteDir>'
$sFromError = '
<remoteDir>
<dir>1394</di>
</remoteDir>'


fnLoadXML($sFromFile,1) ?
@ERROR " | " @SERROR ?
Get $

fnLoadXML($sFromHttp,2) ?
@ERROR " | " @SERROR ?
Get $

fnLoadXML($sFromString,3) ?
@ERROR " | " @SERROR ?
Get $

fnLoadXML($sFromError,3) ?
@ERROR " | " @SERROR ?
Get $

Function fnLoadXML($sXML,$sFrom)
; $sFrom: 1 = File; 2 = HTTP; 3 = String
Dim $xmlDoc,$xmlHttp,$nul

$xmlDoc = CreateObject("Microsoft.XMLDOM")
If @ERROR Exit 10 EndIf
$xmlDoc.async = 0

Select
Case $sFrom = 1
$nul = $xmlDoc.load($sXML)
Case $sFrom = 2
$xmlHttp = CreateObject("Microsoft.XMLHTTP")
If @ERROR Exit 10 EndIf
$xmlHttp.open("GET",$sXML,0)
$xmlHttp.send()
$nul = $xmlDoc.loadXML($xmlHttp.responseText)
Case $sFrom = 3
$nul = $xmlDoc.loadXML($sXML)
Case 1 Exit 87
EndSelect

If $xmlDoc.parseError.errorCode
$fnLoadXML = $xmlDoc.parseError.reason + $xmlDoc.parseError.line
Exit $xmlDoc.parseError.errorCode
EndIf

$fnLoadXML = $xmlDoc.xml

EndFunction


Top
#127264 - 2004-09-30 06:51 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Just a quick suggestion.

Using things like $nul=SetOption("WrapAtEOL","on") do you need this for your function to work? Or is this just a best coding practice?

Either way, may I suggest that the black box method of UDF's is a good idea. Basically this means any UDF should take into consideration the status of SetOptions from the parent script. For example, I just wrote a UDF a few days ago where I needed novarinstrings turned on, i was using Execute with code stings put together based off of UDF options.

Since i needed novarinstrings, when i turned that option on, i also captured the current status, and the last thing i did before exiting the UDF was return novarinstrings back to it's original value.

This way, i can code my UDF that way i wanted to with out forcing someone else to convert to my coding preferences/needs.

Everything else looks good!

Top
#127265 - 2004-09-30 07:31 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
WrapAtEol isn't a necessity, it is just there becuase some of the lines were wrapping. You'll notice that it doesn't have the regular UDF header, as it isn't finalized yet.

I'd like more comments regarding whether to return the XML as a string vs. an object. This could be the first of a mess of XML library functions.

Top
#127266 - 2004-10-01 10:48 AM Re: RFC: fnLoadXML() - Loads & validates XML documents
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

I'd like more comments regarding whether to return the XML as a string vs. an object




It should be an object. Your UDF is a "Load and Parse" udf, not a "Load, Parse and extract the text representation" UDF

More importantly, you will need the object to use things like $oXML.save("path") or getNamedItem or whatever in your other wrapper UDFs.

Top
#127267 - 2004-10-01 10:55 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Good point, Richard.

I've beefed the function up a little with some more error checking.

This demo of the function includes a simple RSS feed reader.

Code:

; ********** SimpleRSS.kix **********

Break On
$nul=SetOption("WrapAtEOL","on")
$nul=SetOption("NoVarsInStrings","on")
$nul=SetOption("Explicit","on")

Dim $sRSSFeed,$xml,$xmlNode,$y,$IE

$sRSSFeed = "http://www.nytimes.com/services/xml/rss/userland/Books.xml"

$xml = fnLoadXML($sRSSFeed,2)
If @ERROR @SERROR ? EndIf

For Each $xmlNode In $xml.getElementsByTagName("item")
Color n/w "Title:" Color w " " + $xmlNode.ChildNodes(0).Text ??
Color n/w "Description:" Color w " " + $xmlNode.ChildNodes(2).Text ??
Color n/w "View the article? (Y/N)" Color w
Get $y ??
If $y = "y"
$IE = CreateObject("InternetExplorer.Application")
$IE.Visible=1
$IE.Navigate($xmlNode.ChildNodes(1).Text)
EndIf
"**************************************"+
"**************************************" ??
Next
$IE = 0

Function fnLoadXML($sXML,$sFrom)
; $sFrom: 1 = File; 2 = HTTP; 3 = String
Dim $xmlDoc,$xmlHttp,$nul

$xmlDoc = CreateObject("Microsoft.XMLDOM")
If @ERROR Exit 10 EndIf
$xmlDoc.async = 0

Select
Case $sFrom = 1
If Not Exist($sXML) Exit 2 EndIf
$nul = $xmlDoc.load($sXML)
Case $sFrom = 2
$xmlHttp = CreateObject("Microsoft.XMLHTTP")
If @ERROR Exit 10 EndIf
$xmlHttp.open("GET",$sXML,0)
$xmlHttp.send()
If $xmlHttp.status = 200
$nul = $xmlDoc.loadXML($xmlHttp.responseText)
Else
$fnLoadXML = $xmlHttp.statusText
Exit 2
EndIf
Case $sFrom = 3
$nul = $xmlDoc.loadXML($sXML)
Case 1 Exit 87
EndSelect

If $xmlDoc.parseError.errorCode
$fnLoadXML = $xmlDoc.parseError.reason + $xmlDoc.parseError.line
Exit $xmlDoc.parseError.errorCode
EndIf

$fnLoadXML = $xmlDoc

EndFunction


Top
#127268 - 2005-03-11 02:52 AM Re: RFC: fnLoadXML() - Loads & validates XML documents
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Chris I was playing arround with XMLDOM and coudnt figure out other way of doing SaveXML() function. As Richard said $oXML.save("path") could be one way, in this case played with the path as he suggested.

This is just to light the candel for a couple of nice XML functions or just trigger new ideas for it. I think they are missing. Variables $root, $node and $child are just to point out levels, they might have another name.

Code:
 

SaveXML("c:\temp\Albums.xml", "contact", "name", "first" , "Leonard")


Code:
  

Function SaveXML($file, $root, $node, $child, $newvalue)
$MyXMLDOM = CreateObject("Microsoft.XMLDOM")
$MyXMLDOM.async = False
$MyXMLDOM.Load ($file)
$MyXpath = "/$root/$node/$child"
$MyNode = $MyXMLDOM.selectSingleNode('$MyXpath')
$MyNode.Text = $newvalue
$Node=$MyNode.Text
$MyXMLDOM.save ('$file')
EndFunction


This is the file right click save as.
HTML is on at KORG, how do you post without it?
_________________________
Life is fine.

Top
#127269 - 2005-03-11 03:23 AM Re: RFC: fnLoadXML() - Loads & validates XML documents
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Anybody know what the "minimum requirements" for xmldom is ? Is it something that has to be installed specially, or does it get installed by IE or Office or something else ?
Top
#127270 - 2005-03-11 04:44 AM Re: RFC: fnLoadXML() - Loads & validates XML documents
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
IE Shawn.

Chris wrote.
Quote:


It uses Microsoft.XMLDOM, which is available with all versions of IE from ver 5 and up.





Intresting format the XML, could expand Kixforms posibilities.
_________________________
Life is fine.

Top
#127271 - 2005-03-11 09:56 AM Re: RFC: fnLoadXML() - Loads & validates XML documents
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Chris:
I was playing arround (again) with getNamedItem() and getElementsByTagName() and as far as I can see there´s plenty to do with this.

Myself, until I dont get the hand on all those objects(methods, events and properties) I cannot figure how to build an UDF without leaving something aside.

The question is:
Should every XMLDOM method respond to a unike UDF or you have in mind big UDF´s with plenty of XMLDOM methods?
If so there are lots to do.

Me want to colaborate on this.

Ahh BTW......translations are easy.


Edited by Jose (2005-03-11 10:08 AM)
_________________________
Life is fine.

Top
#127272 - 2005-03-11 02:34 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Was thinking about using it to save my KF GridView files, instead of using INI files, which are deadly slow on the writes !
Top
#127273 - 2005-03-11 07:53 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
XML can be considered the successor to INI. Also, XML supports a better relational model of data to be stored.
_________________________
There are two types of vessels, submarines and targets.

Top
#127274 - 2005-03-11 09:58 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
I talked to Jose in MSN today about this topic, but for everyone's edification...

Because there is no one way to store data in an XML file I don't think you can adequately write a UDF to service the needs of everyone. The needs are generally too specific. Basically, my thought was to use fnLoadXML() to open the XML document as an object which would then be manipulated by the coder as they saw fit for their specific needs.

That said, perhaps the only generic functions that would be useful to a general audience would be LoadXML, Save, and possibly ParseError.

Once again, additional comments, thoughts, and opinions would be helpful.

Top
#127275 - 2005-03-12 01:51 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The same is true for .INI formats. However, you can develop a UDF that uses XML to read/write data as specified by the user.
_________________________
There are two types of vessels, submarines and targets.

Top
#127276 - 2005-03-13 02:14 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Offshoot discussion here

-Shawn


Top
#127277 - 2005-03-14 11:24 PM Re: RFC: fnLoadXML() - Loads & validates XML documents
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Test post ...







Top
Page 1 of 1 1


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

Who's Online
0 registered and 369 anonymous users online.
Newest Members
rrosell, PatrickPinto, Raoul, Timothy, Jojo67
17877 Registered Users

Generated in 0.071 seconds in which 0.024 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