|
-
Feb 24th, 2007, 07:50 PM
#1
Thread Starter
Addicted Member
** RESOLVED ** Need help with XML
Hello there,
I'm trying to parse the following XML data:
http://members.lycos.co.uk/sjrseh/xml.txt
The "game" tag is repeated throughout the file.
I have XML4 referenced and have spent some time playing around with the following code:
VB Code:
Dim xml_document As DOMDocument
Dim mame_node As IXMLDOMNode
Set mame_node = xml_document.selectSingleNode("mame")
txtName.Text = GetNodeValue(mame_node, "game", "???")
Which gives me "description", "year" and "manufacturer". I want to retrieve other details like "driver status". I also want it to loop through the whole 30MB file for all 6000+ game entries.
It is for my non profit project I started quite a few years back, that uses mame.exe. http://members.lycos.co.uk/sjrseh/
Any help much appreciated.
Last edited by Hack; Feb 26th, 2007 at 10:44 AM.
Reason: Added green "resolved" checkmark Last edited by seh : Today at 10:40 AM. Reason: Resolved
-
Feb 26th, 2007, 08:14 AM
#2
Thread Starter
Addicted Member
Re: Need help with XML
I thought I'd bump this up now the weekend is over I hope noone minds.
This is the XML that I want to parse (the URL doesn't work for some):
<mame build="0.112u2 (Feb 19 2007)">
<game name="puckman" sourcefile="pacman.c">
<description>PuckMan (Japan set 1, Probably Bootleg)</description>
<year>1980</year>
<manufacturer>Namco</manufacturer>
<rom name="namcopac.6e" size="4096" crc="fee263b3" sha1="87117ba5082cd7a615b4ec7c02dd819003fbd669" region="cpu1" offset="0"/>
<rom name="namcopac.6f" size="4096" crc="39d1fc83" sha1="326dbbf94c6fa2e96613dedb53702f8832b47d59" region="cpu1" offset="1000"/>
<rom name="namcopac.6h" size="4096" crc="02083b03" sha1="7e1945f6eb51f2e51806d0439f975f7a2889b9b8" region="cpu1" offset="2000"/>
<rom name="namcopac.6j" size="4096" crc="7a36fe55" sha1="01b4c38108d9dc4e48da4f8d685248e1e6821377" region="cpu1" offset="3000"/>
<rom name="pacman.5e" size="4096" crc="0c944964" sha1="06ef227747a440831c9a3a613b76693d52a2f0a9" region="gfx1" dispose="yes" offset="0"/>
<rom name="pacman.5f" size="4096" crc="958fedf9" sha1="4a937ac02216ea8c96477d4a15522070507fb599" region="gfx1" dispose="yes" offset="1000"/>
<rom name="82s123.7f" size="32" crc="2fc650bd" sha1="8d0268dee78e47c712202b0ec4f1f51109b1f2a5" region="proms" offset="0"/>
<rom name="82s126.4a" size="256" crc="3eb3a8e4" sha1="19097b5f60d1030f8b82d9f1d3a241f93e5c75d6" region="proms" offset="20"/>
<rom name="82s126.1m" size="256" crc="a9cc86bf" sha1="bbcec0570aeceb582ff8238a4bc8546a23430081" region="sound1" offset="0"/>
<rom name="82s126.3m" size="256" crc="77245b66" sha1="0c4d0bee858b97632411c440bea6948a74759746" region="sound1" offset="100"/>
<chip type="cpu" name="Z80" clock="3072000"/>
<chip type="audio" name="Namco" clock="96000"/>
<display type="raster" rotate="90" width="288" height="224" refresh="60.606060" />
<sound channels="1"/>
<input players="2" buttons="1" coins="2">
<control type="joy4way"/>
</input>
<dipswitch name="Rack Test (Cheat)">
<dipvalue name="Off" default="yes"/>
<dipvalue name="On"/>
</dipswitch>
<dipswitch name="Service Mode">
<dipvalue name="Off" default="yes"/>
<dipvalue name="On"/>
</dipswitch>
<dipswitch name="Cabinet">
<dipvalue name="Upright" default="yes"/>
<dipvalue name="Cocktail"/>
</dipswitch>
<dipswitch name="Coinage">
<dipvalue name="2 Coins/1 Credit"/>
<dipvalue name="1 Coin/1 Credit" default="yes"/>
<dipvalue name="1 Coin/2 Credits"/>
<dipvalue name="Free Play"/>
</dipswitch>
<dipswitch name="Lives">
<dipvalue name="1"/>
<dipvalue name="2"/>
<dipvalue name="3" default="yes"/>
<dipvalue name="5"/>
</dipswitch>
<dipswitch name="Bonus Life">
<dipvalue name="10000" default="yes"/>
<dipvalue name="15000"/>
<dipvalue name="20000"/>
<dipvalue name="None"/>
</dipswitch>
<dipswitch name="Difficulty">
<dipvalue name="Normal" default="yes"/>
<dipvalue name="Hard"/>
</dipswitch>
<dipswitch name="Ghost Names">
<dipvalue name="Normal" default="yes"/>
<dipvalue name="Alternate"/>
</dipswitch>
<driver status="good" emulation="good" color="good" sound="good" graphic="good" savestate="supported" palettesize="32"/>
</game>
</mame>
-
Feb 26th, 2007, 09:10 AM
#3
Re: Need help with XML
Inside the game node which details are you looking for?
-
Feb 26th, 2007, 09:44 AM
#4
Thread Starter
Addicted Member
Re: Need help with XML
I'm looking for the following:
'name'
'description'
'year'
'manufacturer'
'driver status'
-
Feb 26th, 2007, 09:52 AM
#5
Re: Need help with XML
See if this does it
VB Code:
Private Sub Command1_Click()
Dim objDoc As DOMDocument
Dim objNodeList As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
Dim objChildNode As IXMLDOMNode
Dim objAttrib As IXMLDOMAttribute
'Load the xml file
Set objDoc = New DOMDocument
objDoc.async = False
objDoc.Load "http://members.lycos.co.uk/sjrseh/xml.txt"
'Retrieve all the game nodes
Set objNodeList = objDoc.selectNodes("//game")
'loop through the game nodes
For Each objNode In objNodeList
'Get the attributes from the current node
'1. select the attribute
Set objAttrib = objNode.Attributes.getNamedItem("name")
'2. do something with the value
Debug.Print objAttrib.Name & " = " & objAttrib.Text
'Get the vaule of the description node
'1. select the node
Set objChildNode = objNode.selectSingleNode("description")
'2. do something with the value
Debug.Print "Description = " & objChildNode.Text
'Get the vaule of the year node
'1. select the node
Set objChildNode = objNode.selectSingleNode("year")
'2. do something with the value
Debug.Print "Year = " & objChildNode.Text
'Get the vaule of the manufacturer node
'1. select the node
Set objChildNode = objNode.selectSingleNode("manufacturer")
'2. do something with the value
Debug.Print "Manufacturer = " & objChildNode.Text
'Get the status attribute from the driver node
'1. select the node
Set objChildNode = objNode.selectSingleNode("driver")
'2. select the attribute you want
Set objAttrib = objChildNode.Attributes.getNamedItem("status")
'3. do something with the value
Debug.Print "driver status = " & objAttrib.Text
Next objNode
Set objAttrib = Nothing
Set objChildNode = Nothing
Set objNode = Nothing
Set objNodeList = Nothing
Set objDoc = Nothing
End Sub
-
Feb 26th, 2007, 10:33 AM
#6
Thread Starter
Addicted Member
Re: Need help with XML
Excellent! Thanks so much, I've been wanting to add XML to my app for a while now 
I have it populating a listview at the moment, but I'm getting an "Object variable or With block variable not set" error:
http://members.lycos.co.uk/sjrseh/error.jpg
Maybe some inconsistencies in the XML? Or maybe I need to check for NULL fields?
Thanks so much again.
</happy>
-
Feb 26th, 2007, 10:43 AM
#7
Re: ** RESOLVED ** Need help with XML
You could concatenate an empty string to the end of the return so you don’t get an error if a node has a null vaule
Litem.SubItems(2) = objChildNode.Text & ""
-
Feb 26th, 2007, 10:47 AM
#8
Re: ** RESOLVED ** Need help with XML
Or maybe this would be better
VB Code:
If Not objChildNode Is Nothing Then Litem.SubItems(2) = objChildNode.Text
-
Feb 26th, 2007, 11:00 AM
#9
Thread Starter
Addicted Member
Re: ** RESOLVED ** Need help with XML
 Originally Posted by MarkT
Or maybe this would be better
VB Code:
If Not objChildNode Is Nothing Then Litem.SubItems(2) = objChildNode.Text
Hehe, yeah that does it 
Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|