2013-12-18 76 views
1

我正在處理來自excel 2013的xml輸出,我已經設置了對XML v6庫的引用,但是我沒有得到我期望的結果。 這是我使用的代碼:通過VBA繼承父屬性的子元素

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」 & 「報頭」 不應該有 「的xmlns =」 中的節點名

編輯:試圖獲得表示

+0

類似問題:http://stackoverflow.com/questions/2079209/need-help-stopping-msxml-from-adding-namespaces –

回答

4
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:

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

高蒂姆 - 正是醫生的命令。非常感謝你 – Zabman