How to avoid xmlns attribute when creating XML element using vbscript?

你。 提交于 2020-01-04 13:37:26

问题


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)

Output:

<MainError><ChildError xmlns="">somevalue</ChildError></MainError>

回答1:


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)


来源:https://stackoverflow.com/questions/26753606/how-to-avoid-xmlns-attribute-when-creating-xml-element-using-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!