[AccessD] XML Import Sample File

MartyConnelly martyconnelly at shaw.ca
Tue Nov 28 14:31:49 CST 2006


Try using XPATH  where syntax would  be "//SUBMITTING_PARTY/REQUEST/KEY/@*"
to move attributes into a collection that can be read.
or failing that just parse xmldoc.xml  for the attributes in partial xml 
string.

Sub xpathtest()
Dim strxml As String
Dim xmldoc As MSXML2.DOMDocument40
Dim nodes As MSXML2.IXMLDOMNodeList
Dim node  As MSXML2.IXMLDOMNode
Dim xmlError    As IXMLDOMParseError
Dim lngErrCode As Long

Set xmldoc = New MSXML2.DOMDocument40
    xmldoc.setProperty "SelectionLanguage", "XPath"
    xmldoc.async = False
    xmldoc.validateOnParse = False
   'xmldoc.resolveExternals = False
xmldoc.Load "C:\Access files\xmltests\Pria Path\pria.xml"
'
               lngErrCode = xmldoc.validate
           '  Debug.Print lngErrCode
            If xmldoc.parseError.errorCode <> 0 Then
                   Debug.Print " Reason: " & xmldoc.parseError.reason
                  Set xmlError = xmldoc.parseError
                   reportParseError xmlError
            End If
'xpath entity name case sensitive
'grab attributes with fee via XPath
Set nodes = xmldoc.selectNodes("//SUBMITTING_PARTY/REQUEST/KEY/@*")

'look through nodes collection
For Each node In nodes
    Debug.Print node.Text & " - " & node.nodeName
Next node

  Debug.Print xmldoc.xml
Set nodes = Nothing
Set xmldoc = Nothing
End Sub

Function reportParseError(err As IXMLDOMParseError)
  Dim s As String
  Dim r As String
  Dim i As Long
   s = ""
  For i = 1 To err.linepos - 1
    s = s & " "
  Next
  r = "XML Error loading " & err.url & " * " & err.reason
  Debug.Print r
    'show character postion of error; tired of counting
  If (err.Line > 0) Then
    r = "at line " & err.Line & ", character " & err.linepos & vbCrLf & _
         err.srcText & vbCrLf & s & "^"
  End If
  Debug.Print r
  End Function

Greg Smith wrote:

>Hi Shamil!
>
>Well, it was a DUH on me...I'd forgotten that I'd changed the text files
>somewhat and it was looking for information that wasn't in there ... once
>I fixed that, it worked!  Thanks!
>
>Now I have another question (endless, aren't they?).  All of these samples
>work fine with the XML text files that are "two" levels deep, such as, for
>the Shippers.xml2.txt file (I truncated the phone number because they were
>wrapping around):
>
><?xml version="1.0"?>
><datarows>
>    <Shippers CompanyName="Speedy Express" Phone="(503) ...
>    <Shippers CompanyName="United Package" Phone="(704) ...
>    <Shippers CompanyName="Federal Shipping" Phone="(503) ...
></datarows>
>
>The XML text file that I am trying to extract data from has many levels to
>it, although only a few are pertinent, they are several layers up inside
>the XML file (See below):
>
><?xml version="1.0" encoding="UTF-8"?>
><REQUEST_GROUP>
>   <REQUESTING_PARTY/>
>   <RECEIVING_PARTY/>
>   <SUBMITTING_PARTY/>
>      <REQUEST>
>         <KEY _Name="SubmissionID" _Value="4281"/>
>         <KEY _Name="GroupName" _Value="JFB_Exemption20060831"/>
>         <KEY _Name="SubmissionNumber" _Value="0000000610...
>           <PRIA_REQUEST _Type="Other"_TypeOtherDescription="ES....
>	<PACKAGE>
>                      .
>                      .
>                      .
>
>When I take the existing 'working' file and add one layer to it, nothing
>is imported.  I don't get any errors, but I don't get any data either.
>
>Will these samples only work for one/two levels?  If I could use these
>(any one of them) to get the data from where I want, that would be
>perfect!  But in order to do that, I need to be able to search 'into' the
>xml file past the first level.
>
>For example, I have gotten it to work if I use the text file format as
>shown below:
>
><REQUEST>
>  <KEY _Name="..../>
>  <KEY _Name=".../>
></REQUEST>
>
>But if I add another grouping around the outside, it fails and extracts no
>data:
>
><REQUEST_GROUP>
>  <REQUEST>
>    <KEY _Name="..../>
>    <KEY _Name=".../>
>  </REQUEST>
></REQUEST_GROUP>
>
>Or is there a way to make it hit whatever level I want?  So far I've not
>been able to do that (yet).
>
>Thanks!
>Greg
>
>================
>Hello Greg,
>
>It works well here with MSXML v.6.0 - data are imported:
>
>Public Function Parse(ByVal vstrURL As String, ByVal vstrTableName As
>String, ByVal vavarFields As Variant)
>Dim reader As SAXXMLReader60 '30
>    mavarFields = vavarFields
>    mstrTableName = vstrTableName
>    mintFieldIndex = -1
>    mintFieldsCount = 0
>    Set reader = New SAXXMLReader60 '30
>    Set reader.contentHandler = Me
>    Set reader.errorHandler = Me
>    reader.parseURL vstrURL
>End Function
>
>I have no clear idea why it doesn't work there on you PC.
>Try to put breakpoints and debug/trace it...
>
>--
>Shamil
>
>
>
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada




More information about the AccessD mailing list