VB6 and XML


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: VB6 and XML

  1. #1
    Join Date
    Nov 2007
    Posts
    5

    VB6 and XML

    Hi all,

    Have a problem reading and searching XML with VB6.

    First of all, I have an XML file that goes roughly like this:

    Code:
    <calltypes>
    	<calltype>
    		<category>Undetermined</category>
    		<type>000 Type of incident undetermined</type>
    		<type>008 Type of action taken not reported due to industrial action</type>
    	</calltype>
    	<calltype>
    		<category>Fire and Explosions</category>
    		<type>100 Fire or Explosion; insuff info to classify</type>
    		<type>109 Fire or Explosion not classified</type>
    	</calltype>
    Now what I want to do is display the 'category' contents in one combo box (I've got that going fine), but then once I've selected a category, it populates a second combo box with the 'type' entries from that node.

    So, as an example using the XML above, in the first combo I select "Fire and Explosions". Then, the second combo would have the entries "100 Fire or Explosion; insuff info to classify" and "109 Fire or Explosion not classified" in it.

    As I mentioned, I have the first combo box reading from the XML file with no problems at all. Now I just need to get it reading the 'types' according to the selection.

    I'm using VB6 Enterprise, MSXML 5 is doing the actual dirty work reading from the XML file.

    My questions are:

    - Is the structure of the XML file suitable?
    - If so, how would I grab the 'type' fields depending on which 'category' is selected?

    Happy to post up any code or information as required. When we're done I'll be sure to post the source code for anyone else who needs help.

    Thanks for your help!

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I recommend that you change the file structure so that the <type> elements are children of the associated <category>:
    Code:
    <calltypes>
    	<calltype>
    		<category id="Undetermined">
    			<type>000 Type of incident undetermined</type>
    			<type>008 Type of action taken not reported due to industrial action</type>
    		</category>
    	</calltype>
    	<calltype>
    		<category id="Fire and Explosions">
    			<type>100 Fire or Explosion; insuff info to classify</type>
    			<type>109 Fire or Explosion not classified</type>
    		</category>
    	</calltype>
    </calltypes>
    To populate the Category combo, do this:
    Code:
        Dim doc As DOMDocument30
        Dim nodes As IXMLDOMNodeList
        Dim node As IXMLDOMNode
    
        Set doc = New DOMDocument30
        doc.Load "d:\path\calltypes.xml"
        Set nodes = doc.selectNodes("//category")
        For Each node In nodes
            cboCategory.AddItem node.Attributes.getNamedItem("id").Text
        Next
    Then, given the text of the selected category, populate the second combo like this:
    Code:
        Set node = doc.selectSingleNode("//category[@id='" & cboCategory.Text & "']")
        Set nodes = node.childNodes
        For Each node In nodes
            cboType.AddItem node.Text
        Next
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2007
    Posts
    5
    Thanks for your help Phil, it's truly appreciated.

    I altered the XML file to suit your recommendation, and used the code you posted, but unfortunately still without luck.

    Code:
        Set doc = New DOMDocument30
        doc.Load App.Path & "\calltypes.xml"
        Set nodes = doc.selectNodes("//category")
        For Each node In nodes
            cboCategory.AddItem node.Attributes.getNamedItem("id").Text
        Next
    When populating the Category combo using the above code, on line 4 above it appears not to find any nodes, because it skips the AddItem line and goes right on past Next.

    Any thoughts on that one?

    Also, looking at the XML file, would you agree that the <calltype> level is now redundant?

    Thanks again for your valuable assistance.

    Cam

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Please post your XML file. The code should work (I tested it before I posted).
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Nov 2007
    Posts
    5
    Ah ha! That's done it. I ran the XML file through a checker (should have done that before) and turned out I had a double quotation mark 50 lines down that killed it.

    Works perfectly now, thanks very much for your help!

    Cam

Similar Threads

  1. problem in opening XML with EXCEL via VB6
    By eranfox in forum VB Classic
    Replies: 5
    Last Post: 08-09-2007, 07:18 AM
  2. Replies: 0
    Last Post: 04-25-2007, 06:54 PM
  3. Reading XML in VB6
    By sowjon in forum XML
    Replies: 0
    Last Post: 12-21-2005, 08:53 PM
  4. updating xml file from VB6
    By smithg22 in forum VB Classic
    Replies: 9
    Last Post: 04-12-2005, 06:23 PM
  5. Replies: 3
    Last Post: 06-19-2000, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center