Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOR loop with xml

Status
Not open for further replies.

buddyel

MIS
Mar 3, 2002
279
US

Does anyone know if it is possible to do a for loop with an xml file. We have an old basic program that does a for loop to find a certain line in a text file (it is based up a calculation that determines how far to search into the file)? Thanks in advance...
 
Do you mean using an XSL or a language specific parsing package?
XSL has the
Code:
<xsl:for-each>
tag that will loop through a file performing whatever actions based ona valid match. Most XML parsers generally allow a similar functionality (in VB you can use element.SelectNodes(&quot;nodename&quot;), than loop through returned list of objects). It should be possible with any xml parser.
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
The old code looks roughly like this:
Open &quot;wiredata.txt&quot; for Input as #1
For NN% = 0 to N%
Input #1,WireGauge,WireTurns
Next NN%
txtOutput.Text = &quot;Wire Gage : &quot; & WireGauge & &quot; Turns: &quot; & WireTurns
Close #1

Wiredata.txt looks like:
&quot;3/16x63&quot;,1.25
&quot;3/16x63&quot;,2.00
&quot;3/16x5&quot;,3.00
...

wiredata.xml looks like:
<wires>
<wire>
<guage>3/16x63</gauge>
<turns>1.25</turns>
</wire>
...
</wires>

I was just wondering if there was a way to do code similar to the FOR statement listed above having the code scan the <wire> elements instead of each new line , is it possible ???
 
You need an XML-Parser that you can call from the Basic environment (MSXML is fine with VB)
/JOlesen
 
i'm building a project using the MSXML parser on IIS for ASP pages. this is a small example in VBScript of how you may be able to do what you're asking:
Code:
dim rootNode
dim baseNode
dim wireNode
dim listNode
dim nodeList
dim strGauge
dim strTurns
dim strOut  

set rootNode = objXML.documentElement
set baseNode = rootNode.selectSingleNode(&quot;//wires&quot;)

set nodeList = baseNode.getElementsByTagName(&quot;//wire&quot;)
for each wireNode in nodeList
  for each listNode in targetNode.childNodes
    if listNode.nodeName = &quot;gauge&quot; then
      strGauge = &quot;Wire Gauge: &quot; & listNode.text
    elseif listNode.nodeName = &quot;turns&quot; then
      strTurns = &quot; Turns: &quot; & listNode.text
    end if
  next
next
there's more info about this parser on microsoft's msdn website:

hope i was a help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top