On Nov 30, 2008, at 4:36 PM, Tom Russell wrote:
I have an xml file that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<config>
<node id="lastDownload" text="Pj48PkE8Pz09RQ" />
<node id="applicationVersion" text="QDtEOnd6" />
<node id="applicationBuild" text="QDtEOnd6" />
<node id="localFilePath" text="" />
<node id="sitePath" text="dYGBfUc8PHtygT+BfXo7cHx6PA" />
<node id="timeToLive" text="P0I" />
<node id="retries" text="QA" />
</config>
While I have done some xml before with RB this one is giving me some
grief.
I have some code written:
Dim FI As FolderItem
Dim TIS as TextInputStream
FI = GetFolderItem("config.xml")
Dim FName,loginfo,oneCell as String
Dim root_count,i,count_po As Integer
Dim node as XMLNode
If FI.Exists() Then
TIS = FI.OpenAsTextFile()
loginfo=TIS.ReadAll
dim xd As new XMLDocument
xd.LoadXML(loginfo)
root_count = xd.DocumentElement.childCount
For i = 0 to root_count - 1
node = xd.DocumentElement.Child(i)
Next
But I really am not sure how to grab the things I need. I set some
properties in my app for the nodes listed in the xml and would like
to assign these by the text or values listed.
Could someone point me in the right direction, I would appreciate it.
If you are willing to trade some pain for code, I'd suggest using
XMLNode.XQL to do XPath queries. Here is some sample code.
dim xml as new XMLDocument
xml.LoadXml testXML //your XML from above
dim nodeset as XMLNodeList = xml.XQL("config/
node[@id='lastDownload']/@text")
if nodeset.Length > 0 then
dim node as XMLNode = nodeset.Item(0)
MsgBox nodeset.Item(0).Value
end if
The XPath expression
config/node[@id='lastDownload']/@text
says, more or less, to find all nodes following the path config/node
whose id attribute equals 'lastDownload', and get the 'text' attribute.
You need to be prepared to catch various XMLExceptions, and know that
I found a bug in exception-handling while working out this example :)
Also note that you can load XML directly from a file; you don't need
to use a TextInputStream.
Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|