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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse values out of large XML file

Status
Not open for further replies.

tmcneil

Technical User
Nov 17, 2000
294
US
I have an extremely large xml file and would like to search for a value and pull out certain values based on that search. My XML looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<pti xmlns="[URL unfurl="true"]http://www.arinc.com/pti"[/URL]
     name="pti"
     msgtime="2006-09-06T00:00:00-06:00">
<TrainMovement>
 <msgtime>
  2006-09-06T00:00:00-06:00 </msgtime>
 <track id="UIA-1-1073"/>
 <train id="S136" direction="West" revenue="1" dest="">
  <station track="1" IsStation="Y">
   STA  </station>
 </train>
</TrainMovement>
<TrainMovement>
 <msgtime>
  2006-09-06T00:00:04-06:00 </msgtime>
 <track id="JCR-1-1037"/>
 <train id="105" direction="West" revenue="1" dest="">
  <station track="1" IsStation="Y">
   WPK  </station>
 </train>
</TrainMovement>
<TrainMovement>
 <msgtime>
  2006-09-06T00:00:11-06:00 </msgtime>
 <track id="SNA-1-3702"/>
 <train id="S122" direction="West" revenue="1" dest="">
  <station track="1" IsStation="Y">
   SUN  </station>
 </train>
</TrainMovement>
<TrainMovement>
 <msgtime>
  2006-09-06T23:59:58-06:00 </msgtime>
 <track id="JCR-1-1063"/>
 <train id="105" direction="West" revenue="1" dest="">
  <station track="0" IsStation="Y">
   WPK  </station>
 </train>
</TrainMovement>
</pti>

It's obviously much larger than this, say several thousand lines. What I would like to do is search for track="0" in the station tag and then pull out track id and train direction. I would like to store this information in a text file with a format that could be imported into excel or something like that. Track ID and Direction would have their own columns. Is this possible using Visual Studio .Net macors?

Todd
 
To:eek:p

This shows you all the ingredient to get it done in the vb... only I use vbs and you can translate with table of correspondance to vb.net. In particular it makes good use of xpath, selectNodes and selectionNameSpace. The selectNodes documentation is here.
[tt]
[green]xmlfile="d:\test\abc.xml" 'your file here, url http: is supported[/green]

set oparser=createobject("msxml2.domdocument")
with oparser
.async=false
.validateOnParse=false
.resolveExternals=false
.load(xmlfile)
end with

if oparser.parseerror.errorcode<>0 then
wscript.echo "Operation aborted: file not found or the document mal-formed."
set oparser=nothing
wscript.quit
end if

snsuri="xmlns:x='with oparser
.setproperty "SelectionLanguage","XPath"
.setproperty "SelectionNamespaces",snsuri
end with

set onodelist=oparser.documentelement.selectNodes("//x:train[child::x:station/@track='0']")

for each onode in onodelist
wscript.echo onode.getAttribute("id") & vbcrlf & onode.getAttribute("direction")
next

set oparser=nothing
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top