Results 1 to 9 of 9

Thread: ** RESOLVED ** Need help with XML

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Resolved ** 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:
    1. Dim xml_document As DOMDocument
    2. Dim mame_node As IXMLDOMNode
    3.  
    4. Set mame_node = xml_document.selectSingleNode("mame")
    5. 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

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    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>

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Need help with XML

    Inside the game node which details are you looking for?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Re: Need help with XML

    I'm looking for the following:

    'name'
    'description'
    'year'
    'manufacturer'
    'driver status'

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Need help with XML

    See if this does it
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim objDoc As DOMDocument
    3. Dim objNodeList As IXMLDOMNodeList
    4. Dim objNode As IXMLDOMNode
    5. Dim objChildNode As IXMLDOMNode
    6. Dim objAttrib As IXMLDOMAttribute
    7.  
    8.     'Load the xml file
    9.     Set objDoc = New DOMDocument
    10.     objDoc.async = False
    11.     objDoc.Load "http://members.lycos.co.uk/sjrseh/xml.txt"
    12.    
    13.     'Retrieve all the game nodes
    14.     Set objNodeList = objDoc.selectNodes("//game")
    15.    
    16.     'loop through the game nodes
    17.     For Each objNode In objNodeList
    18.         'Get the attributes from the current node
    19.         '1. select the attribute
    20.         Set objAttrib = objNode.Attributes.getNamedItem("name")
    21.         '2. do something with the value
    22.         Debug.Print objAttrib.Name & " = " & objAttrib.Text
    23.  
    24.         'Get the vaule of the description node
    25.         '1. select the node
    26.         Set objChildNode = objNode.selectSingleNode("description")
    27.         '2. do something with the value
    28.         Debug.Print "Description = " & objChildNode.Text
    29.        
    30.         'Get the vaule of the year node
    31.         '1. select the node
    32.         Set objChildNode = objNode.selectSingleNode("year")
    33.         '2. do something with the value
    34.         Debug.Print "Year = " & objChildNode.Text
    35.        
    36.         'Get the vaule of the manufacturer node
    37.         '1. select the node
    38.         Set objChildNode = objNode.selectSingleNode("manufacturer")
    39.         '2. do something with the value
    40.         Debug.Print "Manufacturer = " & objChildNode.Text
    41.        
    42.         'Get the status attribute from the driver node
    43.         '1. select the node
    44.         Set objChildNode = objNode.selectSingleNode("driver")
    45.         '2. select the attribute you want
    46.         Set objAttrib = objChildNode.Attributes.getNamedItem("status")
    47.         '3. do something with the value
    48.         Debug.Print "driver status = " & objAttrib.Text
    49.     Next objNode
    50.    
    51.     Set objAttrib = Nothing
    52.     Set objChildNode = Nothing
    53.     Set objNode = Nothing
    54.     Set objNodeList = Nothing
    55.     Set objDoc = Nothing
    56. End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    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>

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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 & ""

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: ** RESOLVED ** Need help with XML

    Or maybe this would be better
    VB Code:
    1. If Not objChildNode Is Nothing Then Litem.SubItems(2) = objChildNode.Text

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Re: ** RESOLVED ** Need help with XML

    Quote Originally Posted by MarkT
    Or maybe this would be better
    VB Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width