并非所有 XML 数据都导入 ACCESS

问题描述

我正在尝试将此 XML 文件导入 MS Access,但它忽略了“TICKET”数据,只是拉入了票证详细信息、客户等的“SUB”表......对 XML 了解不多,只是试用和错误

这是我要导入的文件

<SMS_TICKETS_EXPORT>
    <Ticket
        Number="32271"
        TicketDate="2001-06-22T16:01:08"
        TruckID="DWY15          "
        Trailer="               "
        CustomerID="BRE20          "
        OrderNumber="200101         "
        Material="360.77"
        Delivery="0.00"
        Misc="0.00"
        Tax="0.00"
        Paid=" "
        CheckNumber="0"
        InvoiceNumber="0"
        OperatorID="SEAN    "
        Miles="0"
        OverCap="N"
        OverCust="N"
        JobNo="0"
        Received="0.00"
        Pass="1"
        Status="0"
        Void="N"
        Replacement="N"
        ReplacedTicket="0"
        Remarks="Thank you for your business"
        DateTimeUpdated="2011-09-02T10:42:13.513"
        SiteNumber="1"
        ShipTo="0"
        CloseTimeStamp="2001-06-22T16:01:08">
        <Truck>
            <TruckID>DWY15          </TruckID>
            <Description>DWY #15                       </Description>
            <Tare>35020.00</Tare>
            <ContainerSize>0.00</ContainerSize>
            <EntryDate>1998-12-17T00:00:00</EntryDate>
            <TrailerAttached>N</TrailerAttached>
            <MaxWeight>60.00</MaxWeight>
            <Registration></Registration>
            <Comment></Comment>
            <SplitWeightCode>0</SplitWeightCode>
            <CustomerID>THA10          </CustomerID>
            <HaulerID></HaulerID>
            <UDFData1 />
            <UDFData2 />
            <VehicleType>TRUCK</VehicleType>
            <Created>2020-03-17T15:26:39.7130000</Created>
            <Modified>2020-03-17T15:26:39.7130000</Modified>
            <Sites>1</Sites>
        </Truck>
        <Customer>
            <CustomerID>BRE20          </CustomerID>
            <Name>BREWSTER TRANSIT              </Name>
            <Address1>P.O. Box 410                  </Address1>
            <Address2></Address2>
            <City>BREWSTER                      </City>
            <State>NY                  </State>
            <Zip>10509     </Zip>
            <Country>U.S.A.                        </Country>
            <Contact>JEFF CHATTEN                  </Contact>
            <Phone>8452793738</Phone>
            <CreditLim>0.00</CreditLim>
            <CreditStatus>OK  </CreditStatus>
            <TaxExempt>Y</TaxExempt>
            <CurrentBalance>0.00</CurrentBalance>
            <Miles>0</Miles>
            <GLNumber></GLNumber>
            <discountLevel>0</discountLevel>
            <InvoiceTerms>0</InvoiceTerms>
            <BalanceForward>0.00</BalanceForward>
            <InvoiceByOrder>N</InvoiceByOrder>
            <Deposit>0.00</Deposit>
            <Notes>FirsT DAY W/ GOOD 3/4 3/19/01</Notes>
            <Fax></Fax>
            <Email />
            <UDFData1 />
            <UDFData2 />
            <CreditWarning>0</CreditWarning>
            <Sites>1</Sites>
            <AccountType>CUSTOMER</AccountType>
            <Created>2020-03-17T15:26:39.7530000</Created>
            <Modified>2020-03-17T15:26:39.7530000</Modified>
            <AllowCCTrans>Y</AllowCCTrans>
            <AllowCheckTrans>Y</AllowCheckTrans>
            <AllowCashTrans>Y</AllowCashTrans>
        </Customer>
        <Orders>
            <CustomerID>BRE20          </CustomerID>
            <OrderNum>200101         </OrderNum>
            <Description>BREWSTER PLANT F.O.B.         </Description>
            <Edate>2001-06-06T00:00:00</Edate>
            <Address1>P.O. Box 410                  </Address1>
            <Address2></Address2>
            <City>BREWSTER                      </City>
            <State>NEW YORK            </State>
            <Zip>10509     </Zip>
            <Status>C</Status>
            <TaxRatePct>0.000</TaxRatePct>
            <TaxLocale></TaxLocale>
            <TaxExempt>N</TaxExempt>
            <TnLimit>0.000</TnLimit>
            <UnitLimit>0.000</UnitLimit>
            <Pricing>N</Pricing>
            <Miles>0</Miles>
            <LocationID></LocationID>
            <LimitPeriod>A</LimitPeriod>
            <Remarks />
            <AnyMaterial>Y</AnyMaterial>
            <Metric>N</Metric>
            <CostCode>0</CostCode>
            <AmountRcvd>0.00</AmountRcvd>
            <EscrowFwd>0.00</EscrowFwd>
            <EscrowMinimum>0.00</EscrowMinimum>
            <EscrowOrderSw>N</EscrowOrderSw>
            <EscrowWarning>0.00</EscrowWarning>
            <TonsWarning>0</TonsWarning>
            <UnitsWarning>0</UnitsWarning>
            <UDFData1 />
            <UDFData2 />
            <ShipTo>0</ShipTo>
            <InvoiceTerms>0</InvoiceTerms>
            <Created>2020-03-17T15:26:39.8230000</Created>
            <Modified>2020-03-17T15:26:39.8230000</Modified>
        </Orders>
        <TKDetail>
            <NUMBER>32271</NUMBER>
            <LINENUMBER>1</LINENUMBER>
            <TRUCKID>DWY15          </TRUCKID>
            <CUSTOMERID>BRE20          </CUSTOMERID>
            <ORDERNUMBER>200101         </ORDERNUMBER>
            <MATERIALID>SS10           </MATERIALID>
            <LOCATIONID></LOCATIONID>
            <MATERIALPRICE>8.6000</MATERIALPRICE>
            <MATERIALLABEL>tn </MATERIALLABEL>
            <DELIVERYCHARGE>0.0000</DELIVERYCHARGE>
            <DELIVERYCODE>Q</DELIVERYCODE>
            <MISCCHARGES>0.0000</MISCCHARGES>
            <QUANTITY>0.000</QUANTITY>
            <QUANTITYLABEL>tn </QUANTITYLABEL>
            <NET>83900.00</NET>
            <MATERIALTOTAL>360.77</MATERIALTOTAL>
            <DELIVERYTOTAL>0.00</DELIVERYTOTAL>
            <MISCTOTAL>0.00</MISCTOTAL>
            <TAXTOTAL>0.00</TAXTOTAL>
            <SYstemTAX>0.00</SYstemTAX>
            <ORDERTAX>0.00</ORDERTAX>
            <LOCATIONTAX>0.00</LOCATIONTAX>
            <MINIMUM>0.00</MINIMUM>
            <RESTRICTED>N</RESTRICTED>
            <TAX>Y</TAX>
            <Gross>120940.00</Gross>
            <GrossScale>-1</GrossScale>
            <GrossDateTime>2001-06-22T16:01:08</GrossDateTime>
            <Tare>37040.00</Tare>
            <TareScale>0</TareScale>
            <TareDateTime>2001-06-22T00:00:00</TareDateTime>
            <ContainerID></ContainerID>
            <Notes></Notes>
            <Direction>O</Direction>
            <DeliveryCost>0.0000</DeliveryCost>
            <MaterialCost>0.0000</MaterialCost>
            <Material>
                <MaterialID>SS10           </MaterialID>
                <Description>NYS SPEC. CONCRETE SAND       </Description>
                <MaterialPrice>22.5000</MaterialPrice>
                <MaterialLabel>tn </MaterialLabel>
                <MaterialCost>0.0000</MaterialCost>
                <MaterialGL></MaterialGL>
                <ConversionFactor>0.0000000</ConversionFactor>
                <MinimumCharge>0.00</MinimumCharge>
                <DeliveryCharge>0.0000</DeliveryCharge>
                <DeliveryCost>0.0000</DeliveryCost>
                <DeliveryGL></DeliveryGL>
                <DeliveryCode>Q</DeliveryCode>
                <MiscCharges>0.0000</MiscCharges>
                <Taxable>Y</Taxable>
                <Direction>O</Direction>
                <Restricted>N</Restricted>
                <MTCategory></MTCategory>
                <UDFData1 />
                <UDFData2 />
                <Inactive></Inactive>
                <LoadAdjustment>0.0000000</LoadAdjustment>
                <Created>2020-03-17T15:26:39.7770000</Created>
                <Modified>2020-03-17T15:26:39.7770000</Modified>
                <DefaultQuantity>0.000</DefaultQuantity>
                <Sites>1</Sites>
            </Material>
            <Rate>
                <CustomerID>BRE20          </CustomerID>
                <OrderNum>200101         </OrderNum>
                <MaterialID>SS10           </MaterialID>
                <MaterialPrice>8.6000</MaterialPrice>
                <MaterialCost>0.0000</MaterialCost>
                <MinimumCharge>0.00</MinimumCharge>
                <DeliveryCharge>0.0000</DeliveryCharge>
                <DeliveryCost>0.0000</DeliveryCost>
                <DeliveryCode>Q</DeliveryCode>
                <QtyOrdered>0.000</QtyOrdered>
                <discountBase>0.00</discountBase>
                <discountInc>0.00</discountInc>
                <discountAmount>0.0000</discountAmount>
                <DefaultMaterial>N</DefaultMaterial>
                <Created>2020-03-17T15:26:39.8800000</Created>
                <Modified>2020-03-17T15:26:39.8800000</Modified>
            </Rate>
        </TKDetail>
    </Ticket>
</SMS_TICKETS_EXPORT>

我得到的只是这些表格,但票的数据不在这里Tables in MS ACCESS

解决方法

MS Access 的 ImportXML(或 Ribbon 功能)仅导入以元素为中心的内容,而不是以属性为中心的,因为 Ticket 节点包含与其他节点不同的内容。考虑在导入之前转换您的 XML 以使用 XSLT(旨在转换 XML 文件的专用语言)将属性移动到元素:

XSLT (另存为 .xsl,一个特殊的 .xml 文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Ticket/@*">
    <xsl:element name="{name(.)}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
    
</xsl:stylesheet>

Online Demo

VBA (Access 对象库和 VBA MSXML 库的两种方法)

Public Sub XMLHandle1() 
   ' RUN XSLT TRANSFORMATION
    Application.TransformXML "C:\Path\To\Input.xml",_
                             "C:\Path\To\Transform.xsl",_
                             "C:\Path\To\Output.xml"

    ' IMPORT TRANSFORMED VERSION
    Application.ImportXML "C:\Path\To\Output.xml"
End Sub
Public Sub XMLHandle2()
    Dim xmlDoc As Object,xslDoc As Object,newDoc As Object

    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    Set xslDoc = CreateObject("MSXML2.DOMDocument")
    Set newDoc = CreateObject("MSXML2.DOMDocument")

    ' LOAD XML AND XSL FILES
    xmlDoc.Load "C:\Path\To\Input.xml"
    xmlDoc.async = False

    xslDoc.Load "C:\Path\To\Transform.xsl"
    xslDoc.async = False

    ' RUN XSLT TRANSFORMATION
    xmlDoc.transformNodeToObject xslDoc,newDoc
    newDoc.Save "C:\Path\To\Output.xml"

    ' IMPORT TRANSFORMED VERSION
    Application.ImportXML "C:\Path\To\Output.xml"

    Set newDoc = Nothing: Set xslDoc = Nothing: Set xmlDoc = Nothing
End Sub