Results 1 to 4 of 4

Thread: Search an XML

  1. #1

    Thread Starter
    Addicted Member MrPresident2k's Avatar
    Join Date
    May 2002
    Location
    INDIA
    Posts
    167

    Search an XML

    Hi,

    I have an XML string which is this format
    Code:
     <Parts>
    
     <Part>
      <index>1</index> 
      <PART_NBR>AS15</PART_NBR> 
      <SECTION>A</SECTION> 
      <MAX_PITCH_DIA>1.4</MAX_PITCH_DIA> 
      <MIN_PITCH_DIA>0</MIN_PITCH_DIA> 
      <ALLOW_FHP_BELT>1</ALLOW_FHP_BELT> 
      <COMPOUND_GROOVE>0</COMPOUND_GROOVE> 
      <TURNS_OF_ADJUSTMENT_100>0</TURNS_OF_ADJUSTMENT_100> 
      <SUB_PARTS_PER_TURN>0</SUB_PARTS_PER_TURN> 
      <INIT_TURNS_OPEN_MAX_PD>0</INIT_TURNS_OPEN_MAX_PD> 
      </Part>
    
     <Part>
      <index>2</index> 
      <PART_NBR>AS15</PART_NBR> 
      <SECTION>B</SECTION> 
      <MAX_PITCH_DIA>1.6</MAX_PITCH_DIA> 
      <MIN_PITCH_DIA>0</MIN_PITCH_DIA> 
      <ALLOW_FHP_BELT>1</ALLOW_FHP_BELT> 
      <COMPOUND_GROOVE>0</COMPOUND_GROOVE> 
    <TURNS_OF_ADJUSTMENT_100>0</TURNS_OF_ADJUSTMENT_100> 
      <SUB_PARTS_PER_TURN>0</SUB_PARTS_PER_TURN> 
      <INIT_TURNS_OPEN_MAX_PD>0</INIT_TURNS_OPEN_MAX_PD> 
      </Part>
    
     <Part>
      <index>3</index> 
      <PART_NBR>AK17</PART_NBR> 
      <SECTION>A</SECTION> 
      <MAX_PITCH_DIA>1.7</MAX_PITCH_DIA> 
      <MIN_PITCH_DIA>0</MIN_PITCH_DIA> 
      <ALLOW_FHP_BELT>1</ALLOW_FHP_BELT> 
      <COMPOUND_GROOVE>0</COMPOUND_GROOVE> 
    <TURNS_OF_ADJUSTMENT_100>0</TURNS_OF_ADJUSTMENT_100> 
      <SUB_PARTS_PER_TURN>0</SUB_PARTS_PER_TURN> 
      <INIT_TURNS_OPEN_MAX_PD>0</INIT_TURNS_OPEN_MAX_PD> 
      </Part>
    
     <Part>
      <index>4</index> 
      <PART_NBR>AS20</PART_NBR> 
      <SECTION>A</SECTION> 
      <MAX_PITCH_DIA>1.9</MAX_PITCH_DIA> 
      <MIN_PITCH_DIA>0</MIN_PITCH_DIA> 
      <ALLOW_FHP_BELT>1</ALLOW_FHP_BELT> 
      <COMPOUND_GROOVE>0</COMPOUND_GROOVE> 
    <TURNS_OF_ADJUSTMENT_100>0</TURNS_OF_ADJUSTMENT_100> 
      <SUB_PARTS_PER_TURN>0</SUB_PARTS_PER_TURN> 
      <INIT_TURNS_OPEN_MAX_PD>0</INIT_TURNS_OPEN_MAX_PD> 
      </Part>
    
     <Part>
      <index>4</index> 
      <PART_NBR>AS20</PART_NBR> 
      <SECTION>B</SECTION> 
      <MAX_PITCH_DIA>1.9</MAX_PITCH_DIA> 
      <MIN_PITCH_DIA>0</MIN_PITCH_DIA> 
      <ALLOW_FHP_BELT>1</ALLOW_FHP_BELT> 
      <COMPOUND_GROOVE>0</COMPOUND_GROOVE> 
    <TURNS_OF_ADJUSTMENT_100>0</TURNS_OF_ADJUSTMENT_100> 
      <SUB_PARTS_PER_TURN>0</SUB_PARTS_PER_TURN> 
      <INIT_TURNS_OPEN_MAX_PD>0</INIT_TURNS_OPEN_MAX_PD> 
      </Part>
    </Parts>
    I have 4000 records from a database in this format. The Part_Nbr (say "AS15") is the primary key which I have. I have these values in an array. Now I have to get the records for each primary key from this XML. I tried doing a search, but it is taking a long time.
    VB Code:
    1. For i = 0 To XMLDoc.getElementsByTagName("PART_NBR").length - 1
    2.     If XMLDoc.childNodes(0).childNodes(i).childNodes(1).Text = "AS20" Then
    3.       MsgBox "Found"
    4.     End If
    5. Next i

    This is taking a long time, since there are 4000 records. Is there any method available, so that I can query the XML string and get the result?

    Thanks,
    Pres.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The following will return all the PART_NBRs

    VB Code:
    1. Dim oxmlNodeList As IXMLDOMNodeList
    2.     Dim intIndex As Integer
    3.  
    4.     Set oxmlNodeList = XMLdoc.documentElement _
    5.                                      .selectNodes("//PART_NBR")
    6.  
    7.     For intIndex = 0 to oxmlNodeList.Length - 1
    8.        ' Do what you want to with the returned nodes
    9.     End If

    And this will return the "AS20" nodes
    VB Code:
    1. Dim oxmlNodeList As IXMLDOMNodeList
    2.     Dim intIndex As Integer
    3.  
    4.     Set oxmlNodeList = XMLdoc.documentElement _
    5.                                      .selectNodes("//[PART_NBR = AS20]")
    6.  
    7.     For intIndex = 0 to oxmlNodeList.Length - 1
    8.        ' Do what you want to with the returned nodes
    9.     End If

    I haven't tested either one, so if there are problems you can't figure out let me know.

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or..

    Code:
    Private Sub Command1_Click()
        MsgBox GetRecordXml("AS15").length
    End Sub
    
    Private Function GetRecordXml(ByVal strKey As String) As IXMLDOMNodeList
        Dim strXPath As String
        Dim xmlRecords As IXMLDOMNodeList
        Dim xmlRecordSet As New DOMDocument
        
        xmlRecordSet.async = False
        xmlRecordSet.Load App.Path & "\test.xml"
        
        strXPath = "Part[PART_NBR='" & strKey & "']"
        Set xmlRecords = xmlRecordSet.documentElement.selectNodes(strXPath)
        Set GetRecordXml = xmlRecords
    End Function

  4. #4

    Thread Starter
    Addicted Member MrPresident2k's Avatar
    Join Date
    May 2002
    Location
    INDIA
    Posts
    167

    Thanks a Lot

    Thanks Guys,

    It worked well and it is working fast.

    Thanks,
    Pres

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