【问题标题】:Removing Nodes from XML - VBA从 XML 中删除节点 - VBA
【发布时间】:2014-09-13 12:50:59
【问题描述】:

每当我使用 Access 在 VBA 上创建 XML 时,它都会创建一个在我正在使用的系统上不可读的 xml 文件..

这是我的 XML 代码

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-07-   22T15:53:26">
<Customers>
<Idx>1</Idx>
<FirstName>David</FirstName>
<LastName>McCollum</LastName>
<IconIdx>0</IconIdx>
<PhoneNumber>02870 354244</PhoneNumber>
<Email></Email>
<Street></Street>
<City></City>
<State></State>
<ZipCode></ZipCode>
<Available>1</Available>
<SPIndex>0</SPIndex>

所以基本上我创建了我的表,然后使用按钮命令将其导出

Private Sub Export_Click()
Dim objOtherTbls As AdditionalData

Set objOtherTbls = Application.CreateAdditionalData

'Identify the tables or querys to export
objOtherTbls.Add "Customers"

'Here is where the export takes place
Application.ExportXML ObjectType:=acExportTable, _
DataSource:="Customers", _
DataTarget:="C:\Users\David PC\Desktop\CustomersTest.xml", _
AdditionalData:=objOtherTbls

MsgBox "Export operation completed successfully."
End Sub

基本上我有三件事需要做:)

  1. Root 元素需要更改为“DatabaseData”而不是“dataroot”

  2. 我需要先删除子节点'xlmns:od'才能保存

  3. 我需要在保存之前删除子节点'generated'

这些节点是“dataroot”的子节点吗?

任何帮助将不胜感激......

如果这没有意义我道歉..这是我第一次这样做:)

【问题讨论】:

    标签: xml vba nodes


    【解决方案1】:

    当您使用 ExportXml 函数时,请确保为其提供 schematarget,然后忽略该文件。本质上,默认情况下您将架构添加到 xml 文档中,并且由于我在Official Documentation 中没有看到将其关闭的选项,因此您最好的选择是创建然后删除(杀死) .xsd 如果您不这样做不需要额外的信息。

    Application.ExportXML ObjectType:=acExportTable, _
    DataSource:="Customers", _
    DataTarget:="C:\Users\David PC\Desktop\CustomersTest.xml", _
    SchemaTarget:="C:\Users\David PC\Desktop\DelMe.xsd", _
    AdditionalData:=objOtherTbls
    
    'may need a pause here  > do events loop to make sure it's finished
    'DateTarget = DateAdd("s", 5, Now) '5 second pause here
    'Do Until Now > DateTarget
    '    DoEvents
    'Loop    
    Kill "C:\Users\David PC\Desktop\DelMe.xsd"
    

    我们已经确定在导出过程中很难排除您不需要的标签。导出后更好的解决方案可能是将客户节点放入其自己的单独 XML 文件中。

    Call WriteNodesToNewFile("//Customers", "C:\Users\David PC\Desktop\CustomersTest.xml", "C:\Users\David PC\Desktop\CustomersFINAL.xml")
    

    将此添加到模块中,

    Sub WriteNodesToNewFile(XPath As String, XmlFilePath As String, FinalXmlFilePath As String)
        'If you want intellisense on these Objects, go to Tools > Reference > Add Microsoft XML v6.0
        'This will output ONLY the collection of nodes you specify
    
        Set doc = CreateObject("MSXML2.DOMDocument")
        doc.validateOnParse = False
        doc.Load (XmlFilePath)
        Set GetNodes = doc.SelectNodes(XPath)
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.OpenTextFile(FinalXmlFilePath, 2, True)
    
        For Each Item In GetNodes
            f.write Item.XML
        Next
        f.Close
        Set f = Nothing    
    
    End Sub
    

    【讨论】:

    • 非常感谢您,
    • 没问题。以前像这样的一些过程自动生成的东西对我来说一直很痛苦,所以你弯曲 XSD 以适应你的实际需求。
    • 好的...所以创建模式是我正在尝试处理的事情...但它很复杂,我不知道如何制作它..哈哈..对不起..当它搜索时对于导出的 SchemaTarget,它是否有效地寻找信息模板?还有在哪里/什么是创建模式以满足我的需求的最佳方式?
    • 啊,我才意识到发生了什么。给它 schematarget,然后忽略该文件。本质上,默认情况下您将架构添加到 xml。
    • 我应该忽略哪个文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2019-04-12
    • 2016-11-05
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多