我有这个 XML 文件
我需要按名称搜索<deviceset>
元素(例如 name="DB_")并<technologies>
用更新的数据替换它的子子树。
到目前为止,我制作了返回MSXML2.IXMLDOMElement
<technologies>
结构正确的函数,但我不知道如何在主文档中搜索和替换。
我正在尝试这种方法
'Select everything from table Interlink - This table contains element's names
Dim RS As Recordset
Set RS = CurrentDb.OpenRecordset("SELECT * FROM Interlink")
'Create new document and load the file
Dim oDoc As DOMDocument60
Set oDoc = New DOMDocument60
oDoc.async = False
oDoc.Load CurrentProject.Path & "\JLC_pattern.xml"
Dim Tech As IXMLDOMElement 'I can set this to contain updated <technologies> subtree
'is it better to use IXMLDOMNode? or IXMLDOMDocumentFragment?
Dim devSets As IXMLDOMNodeList 'Collection ?
Dim devSet As IXMLDOMNode 'Node?
'Loop through the recordset, search for elements and replace the subtree <technologies>
Do Until RS.EOF
'Recordset now contains the deviceset name attribute
Debug.Print RS.Fields("lbrDeviceSetName") ' first record contains "DB_"
'I can't find the right method to find the node or collection
'I have tried:
Set devSets = oDoc.getElementsByTagName("deviceset") 'and
Set devSets = oDoc.selectNodes("//eagle/drawing/library/devicesets/deviceset")
'but devSets collection is always empty
For Each devSet In devSets
Debug.Print devSet.baseName ' this does not loop
Next devSet
'I made a function that returns IXMLDOMNode with needed data structure
'Once I find the node I need to replace the subtree
'and move to the next deviceset name
RS.MoveNext
Loop
'Save the modified XML document to disk
oDoc.Save CurrentProject.Path & "\SynthetizedDoc.xml"
RS.Close
'Cleanup...
循环遍历节点集合并搜索记录集可能比循环遍历记录集并搜索节点更容易。
任何人都可以给我一个线索吗?
编辑:我已经扩展了 VBA 代码for each loop
模式 XML 在这里JLC_Pattern.xml
编辑 2:子<technologies>
树可能非常大。我不想用代码淹没这篇文章。我有一个getTechnology(tech as string) as IXMLDOMElement
从数据库中提取数据的函数。函数输出内容可以在这里下载:IXMLDOMElement.xml问题不是这个函数,我只是不知道怎么把这个输出插入到正确的地方oDoc