使用 MSXML 创建 XML 的最佳方法是什么
What is the optimum way to create XML using MSXML
目前我正在像这样创建 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 片段加载到 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
XML 通常表示存储在文件中的 object。 .Net 有许多流行的包可以使序列化和反序列化变得非常容易,使您能够从 object 生成 xml 和从 xml.
生成 object
VBA 无法使用那些不错的包,但我使用了一个基本上做同样事情的模块。 http://www.kudinov.ru/?p=21
这使您可以专注于构建 class 和处理数据。该模块将为您处理 XML 创建。
更新:
首先创建你的Parentclass
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
其次创建你的childclass
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
第三,确保包含序列化模块
最后,你可以使用你的 objects 并在最后序列化它
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 函数。这不太容易出错,因为你玩的是 objects.
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 文件,我不知道如何上传到这里或者是否可以...
目前我正在像这样创建 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 片段加载到 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
XML 通常表示存储在文件中的 object。 .Net 有许多流行的包可以使序列化和反序列化变得非常容易,使您能够从 object 生成 xml 和从 xml.
生成 objectVBA 无法使用那些不错的包,但我使用了一个基本上做同样事情的模块。 http://www.kudinov.ru/?p=21
这使您可以专注于构建 class 和处理数据。该模块将为您处理 XML 创建。
更新:
首先创建你的Parentclass
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
其次创建你的childclass
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
第三,确保包含序列化模块
最后,你可以使用你的 objects 并在最后序列化它
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 函数。这不太容易出错,因为你玩的是 objects.
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 文件,我不知道如何上传到这里或者是否可以...