jscript与vbscript 操作XML元素属性的代码(jscripthome)快来看

随心笔谈4个月前发布 admin
205 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

这篇文章讨论了在XMLDOMElement中处理属性的方法,强调了属性作为节点而非子节点的特性。文章详细介绍了三种属性操作方式:直接通过`getAttribute`和`setAttribute`方法,通过属性节点的`getAttributeNode`和`setAttributeNode`方法,以及以属性集合形式访问和赋值的方法。文章还提供了JScript和VBScript示例代码,展示了如何在不同语言中实现属性的创建和赋值。此外,文章还提到,当需要处理包含实体引用的属性时,必须使用属性节点和文本节点的方法。通过这些方法,可以在不同编程语言和环境中有效地处理XML文档中的属性。


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 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 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)

您可能感兴趣的文章:juqery 学习之三 选择器 可见性 元素属性需要做特殊处理的DOM元素属性的访问javascript 对象属性property与元素属性attribute的浏览器支持jQuery第三课 修改元素属性及内容的代码jQuery学习3:操作元素属性和特性用js获取元素属性的代码javascript 三种方法实现获得和设置以及移除元素属性

© 版权声明

相关文章