使用vbscript创建XML元素时如何避免xmlns属性? [英] How to avoid xmlns attribute when creating XML element using vbscript?
问题描述
添加新元素时,我看到xmlns
属性被添加为空字符串.如何避免这种情况?我看到的答案很少,但它们都是Java或.Net.仍然尝试过这些,但是它们不起作用.我需要VBScript的解决方案.
When adding a new element I see the xmlns
attribute getting added with empty string. How can I avoid this? I have seen few answers but they are either in Java or .Net. Still tried those but they don't work. I need a solution for VBScript.
'load the xml file
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.load(strFilePath)
'get all <MainError> nodes in the xml
Set mainNode = objXMLDoc.documentElement.SelectNodes("//MainError")
'get child nodes for the first <MainError> node
Set childNodes = mainNode(0).ChildNodes
Set objErrorNode = objXMLDoc.createElement("ChildError")
objErrorNode.text = "somevalue"
mainNode(0).appendChild(objErrorNode)
输出:
<MainError><ChildError xmlns="">somevalue</ChildError></MainError>
推荐答案
如此答案中所述,针对类似问题您可能会获得空的xmlns
属性,因为其中一个父元素是使用命名空间定义的,但是您创建的新子元素没有命名空间.使用createNode
而不是createElement
创建具有与祖先节点相同的名称空间的子元素.
As explained in this answer to a similar question you probably get the empty xmlns
attribute because one of the parent elements is defined with a namespace, but you create the new child element without a namespace. Use createNode
instead of createElement
to create the child element with the same namespace as the ancestor node.
ns = "..." '<-- define namespace string here according to whatever
' namespace is defined in your XML
Set objErrorNode = objXMLDoc.createNode(1, "ChildError", ns)
objErrorNode.text = "somevalue"
mainNode(0).appendChild(objErrorNode)
这篇关于使用vbscript创建XML元素时如何避免xmlns属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!