none
how to convert xml string to adodb.recordset RRS feed

  • คำถาม

  • มี xml string อยู่ชุดหนึ่งครับ อยากจะ convert ไปเป็น recordset ไม่ทราบว่าจะต้องเขียน asp อย่างไรครับ
    ตอนนี้มี code อยู่แล้ว แต่ยังมี error อยู่ครับ (invalid xml format)  ขอคำแนะนำ หรือตัวอย่าง program ด้วยครับ ขอบคุณครับ

    --- my asp program : convertxml2recordset.asp --- 
    <%
    Function RecordsetFromXMLString(sXML)

        'Dim oStream As ADODB.Stream
        Set oStream = Server.CreateObject("ADODB.Stream") 'New ADODB.Stream
       
        oStream.Open
        oStream.WriteText sXML   'Give the XML string to the ADO Stream

        oStream.Position = 0    'Set the stream position to the start

        'Dim oRecordset As ADODB.Recordset
        Set oRecordset = Server.CreateObject("ADODB.Recordset")
        'oRecordset.ActiveConnection = "Provider=MSDAOSP; Data Provider=MSXML2.DSOControl.2.6;"
      
        oRecordset.Open oStream ', "Provider=MSPersist"  'Open a recordset from the stream

        oStream.Close
        Set oStream = Nothing

        Set RecordsetFromXMLString = oRecordset  'Return the recordset

        Set oRecordset = Nothing

    End Function
    '======
    Dim sXML ' As String
    Dim ors 'As ADODB.Recordset
    sXML = "<?xml version=""1.0"" encoding=""UTF-16""?>" & _
        "<Values version=""2.0"">" & _
        "<array name=""FIXEDASSET"" type=""record"" depth=""1"">" & _
        " <record javaclass=""com.wm.util.Values"">" & _
        "   <value name=""COM"">TH00</value>" & _
        "   <value name=""ASSET_NO"">000000050652</value>" & _
           "   <value name=""ASSET_TY"">00000050</value>" & _
        "   <value name=""CLASS_NAME"">Computers (C)</value>" & _
        "   <value name=""ASSET_NAME1"">COMPUTER DELL OPTIPLEX 170L</value>" & _
        "         <value name=""ASSET_NAME2"">15410011 ( Computer (C:50) )</value>" & _
        "         <value name=""QTY"">1.00</value>" & _      
           " </record>" & _
        " <record javaclass=""com.wm.util.Values"">" & _
        "         <value name=""COM"">TH00</value>" & _
        "         <value name=""ASSET_NO"">000000050654</value>" & _
        "         <value name=""ASSET_TY"">00000050</value>" & _
           "         <value name=""CLASS_NAME"">Computers (C)</value>" & _
        "         <value name=""ASSET_NAME1"">computer</value>" & _
        "         <value name=""ASSET_NAME2""></value>" & _
        "         <value name=""QTY"">1.00</value>" & _   
        "       </record>" & _
           "     </array>" & _
        "   </Values>"
    ors = RecordsetFromXMLString(sXML)
    if ors.eof() then
      response.write("ors is eof")
    else
    '=== print field name
     For i = 0 To ors.Fields.Count - 1
        response.write "Field " & i & " = " & rs.Fields(i).Name
     Next
     response.write("<br/>===============================")
     do while not ors.eof()
      For i = 0 To rs.Fields.Count - 1
       response.write(ors.Fields(i).Name & " = " & rs.Fields(i).Value & ";")
      Next
      response.write("<br/>======next record=========================")
      ors.movenext
     loop
    end if
    %>

    24 ธันวาคม 2553 8:26

คำตอบ