jscript与vbscript 操作XML元素属性的代码实用指南
平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“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 <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)
您可能感兴趣的文章:
- juqery 学习之三 选择器 可见性 元素属性
- 需做特殊处理的DOM元素属性的访问
- javascript 对象属性property与元素属性attribute的浏览器兼容
- jQuery第三课 修改元素属性及内容的代码
- jQuery学习3:操作元素属性和特性
- 用js拿到元素属性的代码
- javascript 三种方法实现获得和设置以及移除元素属性
-
09.05
SunoAI局部改词有字数限制吗
-
09.05
解决依赖冲突
-
09.05
快对AI拍照搜题夜间光线差识别失败怎么办
-
09.05
Qoder可以完成Spring_Boot项目重构吗
-
09.05
PavoAI手机端提示词输入限制怎么突破
-
09.05
DeepSeek本地部署模型加载失败怎么办
-
- jstl 字符串处理函数实用指南
- 09.06
-
- 江南古典人像摄影
- 09.01
-
- 超级简历一键纠错忽略排版问题如何设置
- 09.01
-
- Seedance生成视频人物变形问题解决办法
- 09.01
-
-
- 火山引擎豆包API缺少参数报错修复做法
- 09.01
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏