使用vbscript创建XML元素时如何避免xmlns属性? [英] How to avoid xmlns attribute when creating XML element using vbscript?

查看:136
本文介绍了使用vbscript创建XML元素时如何避免xmlns属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加新元素时,我看到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆