jscript與vbscript 操作XML元素屬性的代碼
來源:易賢網(wǎng) 閱讀:863 次 日期:2014-08-10 17:15:17
溫馨提示:易賢網(wǎng)小編為您整理了“jscript與vbscript 操作XML元素屬性的代碼”,方便廣大網(wǎng)友查閱!

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)

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:jscript與vbscript 操作XML元素屬性的代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)