更改 XML 文件中的节点和属性文本 [英] Change node and attribute text in XML file

查看:25
本文介绍了更改 XML 文件中的节点和属性文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在解析下面的 XML.

I am stuck in parsing the below XML.

<?xml version="1.0" encoding="UTF-8"?>
<Provisioning xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Request>
    <Header>
      <Command>Create</Command>
      <EntityIdentifiers>
        <Identifier Type="CosName" Value="Super_Super"/>
      </EntityIdentifiers>
      <EntityName>COS</EntityName>
    </Header>
    <Data>
      <COS>
        <ServiceLevels>
          <ServiceLevel>
            <ServiceName>MMS</ServiceName>
            <ServiceLevelName>Super user</ServiceLevelName>
          </ServiceLevel>
          <ServiceLevel>
            <ServiceName>General</ServiceName>
            <ServiceLevelName>Super user</ServiceLevelName>
          </ServiceLevel>
          <ServiceLevel>
            <ServiceName>MMBOX</ServiceName>
            <ServiceLevelName>Super user</ServiceLevelName>
          </ServiceLevel>
        </ServiceLevels>
        <CosName>Super_Super</CosName>
      </COS>
    </Data>
  </Request>
</Provisioning>

我需要将标识符"标签类型"和值"替换为其他相应的值.并更改ServiceLevel"下节点的所有值.

I need to replace the "Identifier" tags "Type" and "Value" to other respective values. And also change all values of nodes under "ServiceLevel".

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "false"
xmlDoc.Load("C:\1.xml")
Set nodeXML = xmlDoc.getElementsByTagName("Identifier")
Set node = nodeXML.item(0)
MsgBox node.Text

推荐答案

Microsoft.XMLDOM 已过时,不应再使用.改用 Msxml2.DOMDocument.

Microsoft.XMLDOM is outdated and shouldn't be used anymore. Use Msxml2.DOMDocument instead.

Set xml = CreateObject("Msxml2.DOMDocument")

选择具有 XPath 表达式 的单个节点,如下所示:

Select a single node with an XPath expression like this:

Set node = xml.SelectSingleNode("//node_name")

和几个具有相同名称的节点,如下所示:

and several nodes with the same name like this:

Set nodes = xml.SelectNodes("//node_name")

节点的属性()可以像这样改变:

Attributes of a node (<node attribute="value">) can be changed like this:

node.SetAttribute("attribute_name") = "new value"

和节点文本(text)像这样:

and node text (<node>text</node>) like this:

node.Text = "new text"

注意 XML 节点和属性的名称区分大小写.

Beware that the names of XML nodes and attributes are case-sensitive.

这篇关于更改 XML 文件中的节点和属性文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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