1

我在实现一个应该删除 XPath 下的所有节点的循环时遇到问题。一旦这些节点被删除,另一个循环应该从第二个文档中注入节点。所以基本上我想用新数据替换已经存在的文件中的旧数据。

我想我已经涵盖了基本框架,但是我猜 XPath 本身仍然可能存在一些问题。我也找不到任何方法来消除孩子的工作:(

如果有人能把我推向正确的方向,我将不胜感激,因为我基本上没有关于使用 VBA 解析 XML 的先验知识。

最好的问候, daZza

到目前为止,这是我的代码:

VBA:

Sub injectXML()

Dim sourceXML As DOMDocument60
Dim targetXML As DOMDocument60
Dim sourceNList As IXMLDOMNodeList 
Dim sourceNode As IXMLDOMNode
Dim targetNList As IXMLDOMNodeList
Dim targetNode As IXMLDOMNode
Dim sourceAttributes As IXMLDOMNamedNodeMap
Dim targetAttributes As IXMLDOMNamedNodeMap

Set sourceXML = New DOMDocument60
sourceXML.Load ("C:\...\source.xml")

Set targetXML = New DOMDocument60
targetXML.Load ("C:\...\target.xml")

sourceXML.async = False
sourceXML.validateOnParse = False
targetXML.async = False
targetXML.validateOnParse = False

Set sourceNList = sourceXML.SelectNodes("//test/TLRule")
Set targetNList = targetXML.SelectNodes("//RuleCollection/TLRule/TLRule")


For Each targetNode In targetNList

    [remove the node]   

Next targetNode


For Each sourceNode In sourceNList

    [append all Data from source.xml to target.xml]  

Next sourceNode


sourceXML.Save ("C:\...\target.XML")

End Sub

源 XML 结构:

<test>
    <TLRule>
        <RuleCommand>
            <RuleID Value="0004" />
        </RuleCommand>
    </TLRule>
    <TLRule>
        <RuleCommand>
            <RuleID Value="0005" />
        </RuleCommand>
    </TLRule>
</test>

目标 XML 结构:

<RuleCollection>
     <TLRule>

        [lots of other data in <TLRule> + child tags]

        <TLRule>
            <RuleCommand>
                <RuleID Value="0004" />
        <TLRule>
    <TLRule>
<RuleCollection>

编辑:

感谢 Martin,我能够完成宏的第一部分(删除所有旧数据)。

Set targetNList = targetXML.SelectNodes("//RuleCollection/TLRule/TLRule/RuleCommand")
Set targetNListSubstitution =    targetXML.SelectNodes("//RuleCollection/TLRule/TLRule/RuleSubstitution")
Set targetNListCleanUp = targetXML.SelectNodes("//RuleCollection/TLRule/TLRule")


For Each Item In targetNList
    Item.ParentNode.RemoveChild (Item)
Next


For Each Item In targetNListSubstitution
    Item.ParentNode.RemoveChild (Item)
Next


For Each Item In targetNListCleanUp
    If Item.HasChildNodes = False Then Item.ParentNode.RemoveChild (Item)
Next

现在我“只”需要从我的 sourceXML/sourceNList 中注入所有数据。不过,这似乎是任务中最难的部分,因为它需要注入到我使用上述方法删除旧数据的地方。

从我的角度来看,我需要某种锚/指针,但我只是看不出我怎么会在那里找到一个。

也许你们中的一些人可以提供帮助?将不胜感激。

最好的问候, daZza

4

1 回答 1

1

I think

Set targetNList = targetXML.SelectNodes("//RuleCollection/TLRule/TLRule")


For Each targetNode In targetNList

    [remove the node]   

Next targetNode

can be implemented as

Set targetNList = targetXML.SelectNodes("//RuleCollection/TLRule/TLRule")

For Each item In targetNList
  item.parentNode.removeChild item
Next

As your paths /RuleCollection/TLRule/TLRule/RuleCommand and //RuleCollection/TLRule/TLRule/RuleSubstitution select child nodes of //RuleCollection/TLRule/TLRule it should suffice to run the For Each loop on targetXML.SelectNodes("//RuleCollection/TLRule/TLRule").

As for inserting the nodes from the other document, I think you can use e.g.

Set target = targetXML.selectSingleNode("//RuleCollection/TLRule")
For Each sourceNode In sourceNList
  target.appendChild sourceNode
Next
于 2013-10-22T09:43:10.853 回答