gpt4 book ai didi

xml - 子元素通过VBA继承父属性

转载 作者:数据小太阳 更新时间:2023-10-29 02:57:16 25 4
gpt4 key购买 nike

我正在处理 excel 2013 的 xml 输出,我已经设置了对 XML v6 库的引用,但我没有得到我期望的结果。这是我正在使用的代码:

Sub testXML()

Dim dom, node, attr, PCMS, SendPCMS, header

Set dom = CreateDom
Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
dom.appendChild node
Set node = Nothing

Set PCMS = dom.createElement("PCMS")
Set attr = dom.createAttribute("xmlns")
attr.Value = "MyNamespace"
PCMS.setAttributeNode attr
Set attr = Nothing
dom.appendChild PCMS

Set SendPCMS = dom.createElement("SendPCMSManifest")
PCMS.appendChild SendPCMS
Set header = dom.createElement("header")
PCMS.appendChild header

dom.Save "C:\Temp\DomTest.xml"

End Sub
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

出于某种原因,属性“xmlns”也被应用于子元素(但只是属性名称,而不是值),输出如下:

?xml version="1.0" encoding="UTF-8"?>
PCMS xmlns="MyNamespace">
SendPCMSManifest xmlns=""/>
header xmlns=""/>
/PCMS>

谁能告诉我哪里出错了?元素“SendPCMSManifest”和“header”不应在节点名称中包含“xmlns=”

已编辑:尝试获取示例输出 xml 显示

最佳答案

Sub testXML()

Dim dom, node, PCMS

Set dom = CreateDom
Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
dom.appendChild node
Set node = Nothing

Set PCMS = dom.createNode(1, "PCMS", "MyNamespace")
dom.appendChild PCMS

PCMS.appendChild dom.createNode(1, "SendPCMSManifest", "MyNamespace")
PCMS.appendChild dom.createNode(1, "header", "MyNamespace")

Debug.Print dom.XML

End Sub

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

输出:

<?xml version="1.0"?>
<PCMS xmlns="MyNamespace"><SendPCMSManifest/><header/></PCMS>

关于xml - 子元素通过VBA继承父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20651298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com