新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     >>W3CHINA.ORG讨论区<<     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> Oracle, SQL Server与XML,XML在数据挖掘中的应用, PMML.
    [返回] W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWLXML.ORG.CN讨论区 - 高级XML应用『 XML 与 数据库 』 → [求助]请教大虾如果将Access数据导成XML 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4375 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]请教大虾如果将Access数据导成XML 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     km321 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:2
      积分:59
      门派:XML.ORG.CN
      注册:2005/11/1

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给km321发送一个短消息 把km321加入好友 查看km321的个人资料 搜索km321在『 XML 与 数据库 』的所有贴子 引用回复这个贴子 回复这个贴子 查看km321的博客楼主
    发贴心情 [求助]请教大虾如果将Access数据导成XML

    请教大虾如果将Access数据导成XML?我的QQ:68274110,邮箱:km_321@163.com最好有原码,不胜感谢。

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/11/1 19:04:00
     
     98900969r 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      等级:大四寒假(收到MIT的Offer啦)
      文章:235
      积分:1555
      门派:XML.ORG.CN
      注册:2005/11/2

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给98900969r发送一个短消息 把98900969r加入好友 查看98900969r的个人资料 搜索98900969r在『 XML 与 数据库 』的所有贴子 引用回复这个贴子 回复这个贴子 查看98900969r的博客2
    发贴心情 
    source ------  http://www.microsoft.com/technet/scriptcenter/resources/officetips/default.mspx
    Importing and Exporting XML Data Using Microsoft Access
    Although the Office Space column has been in business for several months now there are at least two topics we haven’t even touched on: Microsoft Access and XML. There’s no real reason for that; we just haven’t gotten around to them yet. And then we had a brainstorm when it came time to write this week’s column: let’s kill two birds with one stone.

    OK, so then after all the animal rights activists finally stopped picketing our offices we had a better idea: let’s not kill any birds. Instead, let’s write a column that deals with both Access and XML.

    And so we did. Today we’re going to talk about importing XML data into and exporting XML data out of Microsoft Access. Of course, that immediately leads to some questions: after all, if you’ve decided to use Access, why would you care about XML data? Conversely, if you’ve decided to use XML, then why bother with Access? Wouldn’t it be better to pick one format and stick with it?

    To be honest, we don’t have the definitive answer to those questions. We do know, however, that we live in an XML world: many applications – especially Web-based applications – rely on XML data. In that respect, XML is hard to ignore.

    On the other hand, Access has some really nice features for working with and analyzing data; the graphical query builder alone makes it a worthwhile application. Thus a quandary: XML is everywhere, but there aren’t a lot of good tools for analyzing data stored in XML files. (That’s changing, but for now ….) Access, by contrast, has excellent tools for analyzing data, but XML is becoming the de facto standard for data storage. What’s a poor script writer to do?

    Well, here’s one suggestion: why not use XML and Access? Do you have data in an XML file that you’d like to work with? Then import that data into an Access database. Have data in an Access database that someone needs in XML format? Then just export that data to an XML file. This is better than having your cake and eating it, too; it’s like having your cake and someone else’s cake, and then eating both. Plus, you get an XML file out of the deal as well!

    Note. Before running these scripts, set your Access macro security level to Low. Otherwise you’ll get a warning message each time you run one of the scripts.

    Let’s start off by showing you how to import an existing XML file into an Access database:

    Const acAppendData = 2

    Set objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase "C:\Scripts\Test.mdb"

    objAccess.ImportXML "c:\scripts\test.xml", acAppendData

    In this script, we’re assuming the XML file has an outer element tag (say, Inventory) that references a table in the database C:\Scripts\Test.mdb. (In other words, there’s a table in the database named Inventory.) That means your XML file might start off looking something like this:

    <?xml version="1.0" encoding="UTF-8"?>
        <Inventory>
            <ComputerName>atl-fs-01</ComputerName>

    Because we want to append the XML data to that table we start the script by defining a constant named acAppendData and setting the value to 2. We’ll use that constant later on to tell the script to import the data and append it to the existing table.

    We knew you were going to ask that: yes, there are other options for importing data. For example, we could define a constant named acStructureOnly and set the value to 0. In that case, Access would examine the XML file and create a table mimicking the structure of that file. However, no data would be imported; we’d simply have a blank table in the database.

    Alternatively, we could define a constant named acStructureAndData and set the value to 1. If we did that, Access will not only create a new table, it will import data into that table as well. This works quite nicely depending on the nature of the data. For example, Access will create a table and, by default, set the data type of each field to Text. That could be a problem if you have data that needs to be stored in a Memo or Date field. But that’s something you’ll have to determine on a file-by-file basis.

    Tip. To be on the safe side, we recommend constructing the database table first, then appending data. One way to do that is to import the structure from the XML file, then open the new table in Access and change field types as needed. Once that’s done you can append the data without any problem.

    After defining the constant we create an instance of the Access.Application object, then call the OpenCurrentDatabase method in order to open the file C:\Scripts\Test.mdb:

    Set objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase "C:\Scripts\Test.mdb"

    All we have to do now is call the ImportXML method and pass it two parameters: the path to the XML file we want to import (C:\Scripts\Test.xml) and the type of import (as indicated by the constant acAppendData):

    objAccess.ImportXML "c:\scripts\test.xml", acAppendData

    That’s it. Give the thing a few seconds (depending on the size of the XML file), and the data will be imported into Microsoft Access. From there you can do anything you want with it. Including – we might add – exporting it back out as XML.

    Do you need an XML version of one of your Access tables? Hey, why not:

    Const acExportTable = 0

    Set objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase "C:\Scripts\Test.mdb"

    objAccess.ExportXML acExportTable,"Inventory","c:\scripts\test.xml"

    Once again we start out by defining a constant – in this case, that’s a constant named acExportTable – and we set the value to 0. Why do we use this constant? That’s easy: because we want to export all the data stored in a table. Alternatively, we could choose to export data from a query, a report, a function, or some other Access element. In fact, you can use any of the constants (and their corresponding values) shown in the following table:

    Constant
    Value

    acExportForm
    2

    acExportFunction
    10

    acExportQuery
    1

    acExportReport
    3

    acExportServerView
    7

    acExportStoredProcedure
    9

    acExportTable
    0

    After defining the constant we create an instance of the Access.Application object, open the database, and then call the ExportXML method:

    objAccess.ExportXML acExportTable,"Inventory","c:\scripts\test.xml"

    In this sample script, we include three parameters in our method call:

    • acExportTable. The constant that indicates we’re exporting data from a table.

    • Inventory. The name of the table being exported.

    • C:\Scripts\Test.xml. The path to the XML file we’re creating. By default, Access will overwrite this file should it already exist.

    That’s all we have to do. There are some additional options available to us (for example, you can simultaneously export just the schema information to a separate file), but we won’t worry about that today. For more information, see the Microsoft Access VBA Language Reference on MSDN.

    One thing we did want to mention (seeing as how we haven’t talked much about Access in this column) is the fact that Access operates slightly different from Word and Excel. You’ll notice, for example, that we didn’t set the Visible property to True. That was done primarily because our test XML files were so small that they were imported before Access could fully show up on screen. However, it’s easy enough to make Access visible and thus allow you to watch the fun; just add this line of code after you create the Access.Application object:

    objAccess.Visible = True

    You might also have noticed that we never call the Quit method. Does that mean we have invisible copies of Access running amok on our computer? No. Like we said, Access operates a bit different from Word and Excel. When you start Access from a script, the application runs, does whatever you ask it to do, and then automatically terminates at the same time your script terminates. In other words, there’s no need to call the Quit method; Access will automatically quit without any prompting. Something one of the Scripting Guys does every day at 4:00 PM sharp.

    Well, OK, sometimes a little before 4:00 PM. But only during baseball season.

    OK, and sometimes during the baseball off-season.

    At any rate, there might be times when you want Access to open up, do something (like import data from an XML file), and then remain on screen. In that case, you’ll need to add a second line of code, one that sets the UserControl property to True:

    objAccess.UserControl = True

    That line of code “tricks” Access into thinking it was started by a user (e.g., from the Start menu) rather than from a script. In turn, that causes it to remain onscreen when the script finishes.

    And no, script or no script, you’ll never be able to trick our Scripting Guy into staying past 4:00 PM.

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/11/2 6:20:00
     
     bluewing 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:68
      门派:XML.ORG.CN
      注册:2005/11/2

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给bluewing发送一个短消息 把bluewing加入好友 查看bluewing的个人资料 搜索bluewing在『 XML 与 数据库 』的所有贴子 引用回复这个贴子 回复这个贴子 查看bluewing的博客3
    发贴心情 
    大哥 看不懂啊 有中文的吗?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/11/2 19:01:00
     
     bluewing 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:68
      门派:XML.ORG.CN
      注册:2005/11/2

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给bluewing发送一个短消息 把bluewing加入好友 查看bluewing的个人资料 搜索bluewing在『 XML 与 数据库 』的所有贴子 引用回复这个贴子 回复这个贴子 查看bluewing的博客4
    发贴心情 
    大哥 我看完了 眼睛好累 只有点点懂。。就是可以导出来。。。。。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/11/2 19:39:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XML 与 数据库 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/27 11:33:18

    本主题贴数4,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms