【问题标题】:What is the optimum way to create XML using MSXML使用 MSXML 创建 XML 的最佳方式是什么
【发布时间】:2015-05-18 09:38:20
【问题描述】:

目前我正在创建这样的 XML - 而且效果很好......

Private Function CreateDom()
    Dim dom
    Set dom = New DOMDocument
    dom.async = False
    dom.validateOnParse = False
    dom.resolveExternals = False
    dom.preserveWhiteSpace = True
    Set CreateDom = dom
End Function

Public Function generateXML(sourceFileLocation)

'I make an instance of the dom

Set dom = CreateDom

'This is how I setup a root node

Set rootXML= dom.createElement("root")
    dom.appendChild rootXML

'This is how I set attributes
questestinterop.setAttribute "attributeName", "attributeValue"

'setup a child node
Set childOfRoot = dom.createElement("childOfRoot")
rootXML.appendChild childOfRoot 

'This is how I set the text of the element

childOfRoot.Text = "Text Value"
End Function

这在我上面的基本示例中没问题,但是可以说我要创建更多的 XML - 我最终得到了 LOADS 的 appendchilds 和很多对象 - 这接缝效率低且容易出错 - 但有优势我可以随时将一个对象添加到以前创建的对象中。

对于 MSXML,我没有可用的 InnerXML,因此代码很冗长。我正在寻找一种使用 MSXML 和 VBA/VB 来创建 XML 的更有效的方法——或者这种工作的最佳实践——我不禁觉得有更好的方法。

更新 我在上面提到没有 InnerXML - 但是有一种方法可以将 XML sn-p 加载到 DOM 中

Sub MergeXML()

   'Define
   Dim oXml As New MSXML2.DOMDocument
   Dim oXml2 As New MSXML2.DOMDocument

   'Assign
   oXml.loadXML ("<SomeRootElement><Input></Input></SomeRootElement>")
   oXml2.loadXML ("<Output><SomeElement></SomeElement></Output>")
   'or assign via file
   'oXml.Load("c:\Xml.xml")
   'oXml2.Load("c:\Xml2.xml")

   'Process
   oXml.FirstChild.appendChild oXml2.selectSingleNode("//Output")

   'Destroy
   oXml.Save ("c:\NewXml.xml")
   Set oXml2 = Nothing
   Set oXml = Nothing

End Sub

来源:http://p2p.wrox.com/beginning-vb-6/28319-xml-using-msxml2-domdocument-object.html

【问题讨论】:

  • 此代码来自 VBA,但仍可在 VB.net 中使用 - MSXML 方法保持不变
  • 使用如下网页的序列化类:stackoverflow.com/questions/30274147/…
  • Set 在 VB.Net 中仍然有效吗?另外,将Option Strict On 添加到代码文件的顶部或在项目的属性中设置它。
  • 有许多 VB.NET 解决方案在 VBA 中不起作用。对一个人来说是最佳的,对另一个人来说可能不是这样。如果您不追求 .NET 解决方案,请删除标签。

标签: xml vba msxml


【解决方案1】:

XML 通常是存储在文件中的对象的表示。 .Net 有许多流行的包,可以很容易地进行序列化和反序列化,使您能够从对象生成 xml,从 xml 生成对象。

VBA 无法使用那些不错的包,但我使用了一个基本上可以做到这一点的模块。 http://www.kudinov.ru/?p=21

这使您可以专注于构建类和操作数据。该模块将为您处理 XML 创建。

更新:

首先创建你的父类

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ParentClassContainer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Option Explicit
Public Persons() As ChildClassWithEveryXmlAttributes

第二次创建你的子类

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ChildClassWithEveryXmlAttributes"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Option Explicit
Public FirstName As String
Public LastName as String
Public Birdthday as date

第三,确保包含序列化模块

最后,你可以使用你的对象并在最后序列化它

Sub testSerialize()
    Dim myObject As New ParentClassContainer
    Redim myObject.Persons(20)
    myObject.Persons(0).FirstName = "John"
    myObject.Persons(0).LastName = "Doe"
    myObject.Persons(0).Birdthday = #2015-05-21#

    Serialize myObject, "C:\test.xml", False
End Sub

所以我们创建了一个 xml 文件,而没有使用 msxml 中的 createElement 和 appendChild 函数。这不太容易出错,因为您玩的是对象。

XML 输出结果

<?xml version="1.0"?>
<Object class="ParentClassContainer">
    <PropertyGet name="Persons" type="VT_EMPTY">
        <Object class="ChildClassWithEveryXml">
            <PropertyGet name="FirstName" type="VT_BSTR">
                <![CDATA[John]]>
            </PropertyGet>
            <PropertyPut name="FirstName" type="VT_BSTR"/>
            <PropertyGet name="LastName" type="VT_BSTR">
                <![CDATA[Doe]]>
            </PropertyGet>
            <PropertyPut name="LastName" type="VT_BSTR"/>
            <PropertyGet name="Birdthday" type="VT_DATE">
                <![CDATA[2015-05-21]]>
            </PropertyGet>
            <PropertyPut name="Birdthday" type="VT_DATE"/>
        </Object>
    </PropertyGet>
    <PropertyPut name="Persons" type="VT_VARIANT"/>
    <PropertyPutRef name="Persons" type="VT_EMPTY"/>
</Object>

我为此创建了一个excel文件,我不知道如何在这里上传或者是否可以......

Sample excel file with Vba as requested

【讨论】:

  • 这很有趣——你能提供一个例子来说明该方法与我在我的例子中所做的不同吗?是不是代码少了
  • 这比以前好,但仍然不清楚这是如何创建 XML 或父子关系如何工作的。您可以通过显示示例生成的 XML 来改进您的答案吗?
  • 在纯 VBA 中序列化是不可能的。此外,为用户注册 COM 服务器对于 Excel-VBA 解决方案来说有点牵强,因为 Excel 工具的主要魅力之一是它们没有任何部署开销。在这个例子中,序列化是通过一些包装器完成的,可能是一个 C# 包装器,所以它需要部署。
猜你喜欢
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2011-01-04
  • 2017-08-06
  • 2022-01-19
相关资源
最近更新 更多