我一直在解析下面的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>
我需要将“Identifier”标签“Type”和“Value”替换为其他各自的值。并且还会更改“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
答案 0 :(得分:1)
Microsoft.XMLDOM
已过时,不应再使用了。请改用Msxml2.DOMDocument
。
Set xml = CreateObject("Msxml2.DOMDocument")
选择一个XPath expression的单个节点,如下所示:
Set node = xml.SelectSingleNode("//node_name")
和几个具有相同名称的节点如下:
Set nodes = xml.SelectNodes("//node_name")
节点(<node attribute="value">
)的属性可以像这样改变:
node.SetAttribute("attribute_name") = "new value"
和节点文本(<node>text</node>
)如下:
node.Text = "new text"
请注意,XML节点和属性的名称区分大小写。