在导出到XML时嵌套多个表/查询

时间:2014-11-21 17:49:58

标签: xml ms-access access-vba export ms-access-2010

有没有办法使用Application.ExportXML方法不仅可以合并多个查询,还可以将查询嵌套在一起?

例如,我们说我有三张桌子。我已经能够使用Application.ExportXML方法将三者合并在一起了。我不想重复的主要问题不会重复,而所有客户信息的主要查询都会重复。好。

但它是这样出现的(出于各种偏执的原因,我使用虚假数据):

<DogCare>
  <Facility>Twin Peaks</Facility>
  <Year>1992</Year>
  <Month>1</Month>
</DogCare>
<DogTeam>
  <Team>DogWalker</Team>
  <DogsWalked>24</DogsWalked>
  <TreatsGiven>900</TreatsGiven>
</DogTeam>
<PetWalkTime>
  <Name>Palmer</Name>
  <Age>7</Age>
  <FavoriteSnack>Cheese</FavoriteSnack>
  <FavThingtoDo>Chew Shoes</FavThingtoDo>
  <Day>1</Day>
  <DidBusiness>Yes</DidBusiness>
  <Barked>Constantly</Barked>
  <PulledonLeash>Whole Time</PulledonLeash>
  <DeservesTreat>Gave it Anyway</DeservesTreat>
</PetWalkTime>
<PetWalkTime>
  <Name>Laura</Name>
  <Age>5</Age>
  <FavoriteSnack>Pie</FavoriteSnack>
  <FavThingtoDo>Fetch</FavThingtoDo>
  <Day>1</Day>
  <DidBusiness>Yes</DidBusiness>
  <Barked>No</Barked>
  <PulledonLeash>No</PulledonLeash>
  <DeservesTreat>Gave Several</DeservesTreat>
</PetWalkTime>

现在。 &#34; DogCare&#34;,&#34; DogTeam&#34;,&#34; PetWalkTime&#34;是单独的查询。然而,它们都是相关的,并且属于彼此:

<DogCare>
  <Facility>Twin Peaks</Facility>
  <Year>1992</Year>
  <Month>2</Month>
  <DogTeam>
    <Team>DogWalker</Team>
    <DogsWalked>24</DogsWalked>
    <TreatsGiven>900</TreatsGiven>
    <PetWalkTime>
        <Name>Palmer</Name>
        <FavoriteSnack>Cheese</FavoriteSnack>
        <FavThingtoDo>Chew Shoes</FavThingtoDo>
        <Day>1</Day>
        <DidBusiness>Yes</DidBusiness>
        <Barked>Constantly</Barked>
        <PulledonLeash>Whole Time</PulledonLeash>
        <DeservesTreat>Gave it Anyway</DeservesTreat>
    </PetWalkTime>
    <PetWalkTime>
         <Name>Laura</Name>
         <FavoriteSnack>Pie</FavoriteSnack>
         <FavThingtoDo>Fetch</FavThingtoDo>
         <Day>1</Day>
         <DidBusiness>Yes</DidBusiness>
         <Barked>No</Barked>
         <PulledonLeash>No</PulledonLeash>
         <DeservesTreat>Gave Several</DeservesTreat>
    </PetWalkTime>
  </DogTeam>
</DogCare>

有没有办法在出口时使最后一个成为可能?

1 个答案:

答案 0 :(得分:1)

是的,Application.ExportXML可以导出正确的嵌套XML,前提是

  1. 您要导出表格(不是查询)和
  2. 您已在表格之间设置了Access中的关系(例如外键约束)
  3. Relationships.png

    如果不满足其中任何一个条件,那么即使您使用MSDN文章here中列出的方法,您仍然可以获得&#34;顺序&#34; XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-11-21T13:43:50">
        <DogCare>
            <ID>1</ID>
            <Facility>Twin Peaks</Facility>
        </DogCare>
        <DogTeam>
            <TeamID>1</TeamID>
            <ParentID>1</ParentID>
            <Team>DogWalker</Team>
        </DogTeam>
        <PetWalkTime>
            <WalkID>1</WalkID>
            <TeamID>1</TeamID>
            <Name>Palmer</Name>
            <Age>7</Age>
        </PetWalkTime>
        <PetWalkTime>
            <WalkID>2</WalkID>
            <TeamID>1</TeamID>
            <Name>Laura</Name>
            <Age>5</Age>
        </PetWalkTime>
    </dataroot>
    

    然而,如果两个条件都满足,那么我们可以得到&#34;嵌套&#34;像这样的XML

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-11-21T13:52:50">
        <DogCare>
            <ID>1</ID>
            <Facility>Twin Peaks</Facility>
            <DogTeam>
                <TeamID>1</TeamID>
                <ParentID>1</ParentID>
                <Team>DogWalker</Team>
                <PetWalkTime>
                    <WalkID>1</WalkID>
                    <TeamID>1</TeamID>
                    <Name>Palmer</Name>
                    <Age>7</Age>
                </PetWalkTime>
                <PetWalkTime>
                    <WalkID>2</WalkID>
                    <TeamID>1</TeamID>
                    <Name>Laura</Name>
                    <Age>5</Age>
                </PetWalkTime>
            </DogTeam>
        </DogCare>
    </dataroot>
    

    使用这样的代码

    Option Compare Database
    Option Explicit
    
    Sub XmlExportTest()
        Dim objTeam As AdditionalData, objWalk As AdditionalData
        Set objTeam = Application.CreateAdditionalData
        Set objWalk = objTeam.Add("DogTeam")
        objWalk.Add "PetWalkTime"
        Application.ExportXML _
                ObjectType:=acExportTable, _
                DataSource:="DogCare", _
                DataTarget:="C:\Users\Gord\Desktop\DogData.xml", _
                AdditionalData:=objTeam
    End Sub