帮助将XML String传递给RecordSet

时间:2010-10-26 18:14:01

标签: javascript asp-classic vbscript ado

我想要做的是将XML String转储到RecordSet中。我遇到的问题是,如果我首先将XML String保存到文件然后从我认为冗余的文件中读取,代码似乎工作正常。但是,当我想从字符串中读取时,我收到错误

无法创建RecordSet。源XML不完整或无效。 80004005

我的XML字符串格式为

<portfolio>
   <stock>
      <shares>100</shares>
      <symbol>MSFT</symbol>
      <price>$70.00</price>
      <info>
         <companyname>Microsoft Corporation</companyname>
         <website>http://www.microsoft.com</website>
      </info>
   </stock>
   <stock>
      <shares>100</shares>
      <symbol>AAPL</symbol>
      <price>$107.00</price>
      <info>
         <companyname>Apple Computer, Inc.</companyname>
         <website>http://www.apple.com</website>
      </info>
   </stock>
   <stock>
      <shares>100</shares>
      <symbol>DELL</symbol>
      <price>$50.00</price>
      <info>
         <companyname>Dell Corporation</companyname>
         <website>http://www.dell.com</website>
      </info>
    </stock>
    <stock>
       <shares>100</shares>
       <symbol>INTC</symbol>
       <price>$115.00</price>
       <info>
          <companyname>Intel Corporation</companyname>
          <website>http://www.intel.com</website>
       </info>
   </stock>
</portfolio>

我用来转换XML字符串的代码(我遇到问题)是

Public Function RecordsetFromXMLString(sXML As String) As Recordset

    Dim oStream As ADODB.Stream
    Set oStream = 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 = New ADODB.Recordset

    oRecordset.Open oStream    'Open a recordset from the stream

    oStream.Close
    Set oStream = Nothing

    Set RecordsetFromXMLString = oRecordset  'Return the recordset

    Set oRecordset = Nothing

End Function

请您的帮助将不胜感激。

http://msdn.microsoft.com/en-us/library/ms810621

http://support.microsoft.com/kb/263247

我已经尝试过使用以下内容

Public Function RecordsetFromXMLDocument(XMLDOMDocument) 
    Dim oRecordset 
    Set oRecordset = CreateObject("ADODB.Recordset.6.0")
    oRecordset.Open XMLDOMDocument 'pass the DOM Document instance as the Source argument

    Set RecordsetFromXMLDocument = oRecordset  'return the recordset

    Set oRecordset = Nothing
End Function

但仍遇到同样的问题。


我用过的代码将我的DomDocumentData格式化为我需要的ADO XML持久格式

'*******************************************************************************************
' SCHEMA GENERATOR
'*******************************************************************************************
'parentnodepath -- XPath to the Main Node/Table/RowCollection
'parentnodepath -- Name of the Main Node/Table/RowCollection
Function CreateSchemafromNode(XMLDocument,parentnodepath, parentnodeName)
        Dim schema, stemp, MyArray,nodename, childnodelist,counter, n, x, tempnode
        schema = TextWriterSchemaNameSpaceHeader()
        schema=schema & TextWriterSchemaHeader(parentnodeName)
        'LOOP HERE 
          counter = 0

            For Each stemp In XMLDocument.SelectSingleNode(parentnodePath).ChildNodes(0).ChildNodes
                    counter = counter + 1
                    schema = schema & TextWriterSchemaRowAttributeElement(stemp.NodeName, counter, "")
            Next
        'END LOOOP HERE
            schema=schema & TextWriterSchemaSchemaEnd
            schema =schema &  TextWriterSchemaRowHeader
           'BEGIN FIRST LOOP HERE -- FOR EACH TOP NODE --ROW
                For Each n In XMLDocument.SelectSingleNode(parentnodePath).ChildNodes
                        schema =schema & TextWriterSchemaAddRowBegin()
                    'BEGIN SECOND LOOP HERE -- FOR EACH CHILD OF TOP NODE -- NODE VALUE IN CURRENT ROW -- FOR EACH CHILDNODELIST NAME
                    For Each x In n.ChildNodes
                            schema = schema & TextWriterSchemaAddRowFieldNameValue(x.NodeName, x.text) 
                    'END SECOND LOOP HERE 
                    Next
                    schema =schema & TextWriterSchemaAddRowEnd()
           Next 
        'END FIRST LOOP HERE 
        schema =schema & TextWriterSchemaRowEnd()
        schema =schema &TextWriterSchemaNameSpaceEnd()

        CreateSchemafromNode=schema
End Function 


Function TextWriterSchemaNameSpaceHeader()
  Dim schemaString 

    schemaString= "<xml xmlns:s='"
    schemaString= schemaString & "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' " & vbCrLf & vbTab
    schemaString= schemaString & ("xmlns:dt='")
    schemaString= schemaString & "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' " & vbCrLf & vbTab
  ' schemaString= schemaString & ("xmlns:dt='")
  ' schemaString= schemaString & "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' " & vbCrLf & vbTab
    schemaString= schemaString & "xmlns:rs='urn:schemas-microsoft-com:rowset' "  & vbCrLf & vbTab
    schemaString=schemaString & " xmlns:z='#RowsetSchema'> " & vbCrLf 
    TextWriterSchemaNameSpaceHeader = schemaString 
End Function


Function TextWriterSchemaHeader(recordname)
     Dim schemaString 
            schemaString= "<s:Schema id='RowsetSchema'>"& vbCrLf & vbTab
            schemaString= schemaString &"<s:ElementType name='" & recordname & "' content='eltOnly'>" & vbCrLf 
            TextWriterSchemaHeader = schemaString 
End Function

Function TextWriterSchemaRowAttributeElement(rowname, rowordernumber, rowtype)
    Dim schemaString 
        schemaString=vbTab & vbTab & "<s:AttributeType  name='" & rowname & "'  rs:number='" & rowordernumber & "'  />" & vbCrLf 
        TextWriterSchemaRowAttributeElement = schemaString
End Function

Function TextWriterSchemaSchemaEnd()
      Dim schemaString 
        schemaString=vbTab & vbTab & "<s:extends type='rs:rowbase'/>" & vbCrLf 
        schemaString= schemaString &vbTab &  "</s:ElementType>" & vbCrLf 
        schemaString= schemaString & "</s:Schema>"  & vbCrLf 
        TextWriterSchemaSchemaEnd =schemaString
End Function

Function TextWriterSchemaRowHeader()
        Dim schemaString 
            schemaString= vbTab & "<rs:data>" & vbCrLf 
            TextWriterSchemaRowHeader = schemaString
End function

Function TextWriterSchemaAddRowBegin()
      Dim schemaString 
        schemaString=vbTab & "<z:row "
        TextWriterSchemaAddRowBegin=schemaString
End function

Function TextWriterSchemaAddRowFieldNameValue(FieldName, FieldValue)
    Dim schemaString 
            schemaString= FieldName & "='" & FieldValue & "' "
            TextWriterSchemaAddRowFieldNameValue=schemaString
End function

Function TextWriterSchemaAddRowEnd()
        Dim schemaString
           schemaString="/>"  & vbCrLf 
           TextWriterSchemaAddRowEnd=schemaString
End function


Function TextWriterSchemaRowEnd()
         Dim schemaString
              schemaString=vbTab & "</rs:data>" & vbCrLf 
               TextWriterSchemaRowEnd=schemaString
End function

Function TextWriterSchemaNameSpaceEnd()
         Dim schemaString
               schemaString="</xml>"  & vbCrLf 
               TextWriterSchemaNameSpaceEnd=schemaString
End Function

1 个答案:

答案 0 :(得分:1)

您收到此错误,因为您提供的XML不是ADODB.Recordset可以理解的格式。格式需要类似于以下内容,它基于XML-Data Reduced Schema。有关详细信息,请参阅ADO XML Persistence Format Protocol上的文档。

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' 
    xmlns:rs='urn:schemas-microsoft-com:rowset' 
    xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly'>
        <s:AttributeType name='shares' rs:number='1' />
        <s:AttributeType name='symbol' rs:number='2' />
        <s:AttributeType name='price' rs:number='3' />
        <s:extends type='rs:rowbase'/>
    </s:ElementType>
</s:Schema>
<rs:data>
    <z:row shares='100' symbol='MSFT' price='$70.00'  />
    <z:row shares='100' symbol='AAPL' price='$107.00'  />
    <z:row shares='100' symbol='DELL' price='$50.00'  />
</rs:data>
</xml>

过去使用ADO和XML时,我使用XSLT将我的XML格式转换为ADO XML格式。您也可以通过编程方式执行此操作。这里有几个例子(它们不是VB6,但应该让你知道需要什么)。

http://support.microsoft.com/kb/316337

http://msdn.microsoft.com/en-us/magazine/cc301468.aspx

http://etutorials.org/Programming/Web+Solutions+based+on+ASP.NET+and+ADO.NET/Part+III+Interoperability/Interoperable+Web+Applications/From+DataSet+Objects+to+ADO+Recordset+Objects/