程序员

jscript与vbscript 操作XML元素属性的代码

作者:admin 2021-04-06 我要评论

Although attributes belong to a particular element, they are not considered child nodes of element nodes. Instead, they behave more like properties of...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
Although attributes belong to a particular element, they are not considered child nodes of element nodes. Instead, they behave more like properties of IXMLDOMElement.

Most of the methods for working with attributes come from IXMLDOMElement. Attributes can be manipulated in the following ways.

Directly, through the getAttribute and setAttribute methods of IXMLDOMElement.

As named IXMLDOMAttribute nodes, with getAttributeNode and setAttributeNode.

As a set of nodes accessible through the attributes property and returned as an IXMLNamedNodeMap.

Examples
JScript
The following JScript example creates a new document containing a <memo> element, and then creates an attribute named author with a value of "Pat Coleman".
复制代码 代码如下:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo");
rootElement.setAttribute("author", "Pat Coleman");
xmlDoc.appendChild(rootElement);


VBScript
复制代码 代码如下:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
Set rootElement=xmlDoc.createElement("memo")
rootElement.setAttribute("author", "Pat Coleman")
xmlDoc.appendChild(rootElement)


If you prefer to work with attribute nodes, you can create the attribute, and then create a text node to store its value. Attribute nodes can only contain text nodes and entity reference nodes. (If you need to create an attribute containing an entity reference, you must use this approach.)

Working with attribute nodes requires using the DOMDocument object to create attribute and text (and entity reference, if necessary) nodes before assigning the nodes to the element.

JScript
The following JScript code uses this approach to perform the same work as the preceding examples, creating a <memo> element with an author attribute holding the value "Pat Coleman".

复制代码 代码如下:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo");
var memoAttribute=xmlDoc.createAttribute("author");
var memoAttributeText=xmlDoc.createTextNode("Pat Coleman");
memoAttribute.appendChild(memoAttributeText);
rootElement.setAttributeNode(memoAttribute);
xmlDoc.appendChild(rootElement);


VBScript
复制代码 代码如下:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
Set rootElement=xmlDoc.createElement("memo")
Set memoAttribute=xmlDoc.createAttribute("author")
Set memoAttributeText=xmlDoc.createTextNode("Pat Coleman")
memoAttribute.appendChild(memoAttributeText)
rootElement.setAttributeNode(memoAttribute)
xmlDoc.appendChild(rootElement)

本文转载自网络,原文链接:https://m.jb51.net/article/21374.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • Vue SPA 首屏优化方案

    Vue SPA 首屏优化方案

  • vue 动态添加的路由页面刷新时失效的原

    vue 动态添加的路由页面刷新时失效的原

  • vue项目配置 webpack-obfuscator 进行

    vue项目配置 webpack-obfuscator 进行

  • JavaScript 中的执行上下文和执行栈实

    JavaScript 中的执行上下文和执行栈实