#127263 - 2004-09-30 06:05 PM
RFC: fnLoadXML() - Loads & validates XML documents
|
Chris S.
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
|
|
|
|
#127266 - 2004-10-01 10:48 AM
Re: RFC: fnLoadXML() - Loads & validates XML documents
|
Richard H.
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.
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
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
|
|
|
|
#127276 - 2005-03-13 02:14 PM
Re: RFC: fnLoadXML() - Loads & validates XML documents
|
Shawn
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
Registered: 1999-08-13
Posts: 8611
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 369 anonymous users online.
|
|
|