我有来自IPC1752架构的XML,我必须根据我的Excel数据表填写值。所以我必须做这些事情
1.将XML加载到我的VBA中 2.使用存储在excel单元格中的值更改XML中的所需值 3.将修改后的XML保存到文件夹
' Sub UpdateXML() 调用fnUpdateXMLByTags 结束子
函数fnUpdateXMLByTags() 昏暗的mainWorkBook作为工作簿 Dim wrsht As Worksheet
Set mainWorkBook = ActiveWorkbook
Set wrsht = mainWorkBook.Sheets("Sheet1")
wrsht.Activate
Dim oXMLFile As MSXML2.DOMDocument60
Set oXMLFile = New MSXML2.DOMDocument60
oXMLFile.async = False
oXMLFile.validateOnParse = False
XMLFileName = "Z:\IPC\IPC1752A_WK-200264-000 - Copy.xml"
For i = 3 To 5
If Not IsEmpty(mainWorkBook.Sheets("Sheet1").Range("A" & i)) Then
PartID = mainWorkBook.Sheets("Sheet1").Range("A" & i).Value
PartName = mainWorkBook.Sheets("Sheet1").Range("B" & i).Value
MaterialName = mainWorkBook.Sheets("Sheet1").Range("D" & i).Value
MassAmount = mainWorkBook.Sheets("Sheet1").Range("F" & i).Value
MassUnit = mainWorkBook.Sheets("Sheet1").Range("G" & i).Value
Path = "D:\New folder\" & PartID & ".xml"
If oXMLFile.Load(XMLFileName) Then
Set PartIDNodes = oXMLFile.SelectNodes("//@itemNumber")
Set PartNameNodes = oXMLFile.SelectNodes("//@itemName")
Set MaterialNameNodes = oXMLFile.SelectNodes("//@name")
Set MassAmountNodes = oXMLFile.SelectNodes("//@value")
Set MassUnitNodes = oXMLFile.SelectNodes("//@UOM")
PartIDNodes(0).NodeValue = Part_ID
PartNameNodes(0).NodeValue = PartName
MaterialNameNodes(5).NodeValue = MaterialName
MassAmountNodes(1).NodeValue = MassAmount
MassUnitNodes(1).NodeValue = MassUnit
Set ParentNodes = oXMLFile.SelectNodes("//Substance")
MsgBox ParentNodes.Length '->is showing zero
End If
End If
Next i
End Function
这里当我使用selectnodes(属性)时,它正在工作[例如:oXMLFile.SelectNodes(" // @ itemName")]
但在使用selectnodes(元素)时无法正常工作[例如:oXMLFile.SelectNodes(" //物质")]
请帮忙。
答案 0 :(得分:1)
这让我的便盆开了一段时间,直到我意识到它与名称空间有关。您需要删除:
xmlns="http://webstds.ipc.org/175x/2.0"
来自xml。
然后你可以用Xpath导航,例如:
Public Sub test()
'Remove namespace info: ===> xmlns="http://webstds.ipc.org/175x/2.0"
Dim xml As String, doc As MSXML2.DOMDocument60
xml = [A1].Text
Set doc = New MSXML2.DOMDocument60
If Not doc.LoadXML(xml) Then
Err.Raise doc.parseError.ErrorCode, , doc.parseError.reason
Exit Sub
End If
Dim nodeList As Object
Set nodeList = doc.SelectNodes("//Substance")
Debug.Print nodeList.Length
End Sub
其他方法:
这将基于NodeType选择元素节点。我把你的xml放在单元格A1中。
Option Explicit
Public Sub test()
Dim xml As String, doc As MSXML2.DOMDocument60
xml = [A1].Text
Set doc = New MSXML2.DOMDocument60
If Not doc.LoadXML(xml) Then
Err.Raise doc.parseError.ErrorCode, , doc.parseError.reason
Exit Sub
End If
''Use the nodeType property to only process element nodes
Dim node As IXMLDOMElement
For Each node In doc.DocumentElement.ChildNodes
If node.NodeType = 1 Then
Debug.Print node.nodeName
End If
Next node
End Sub
更脆弱,但使用XPath降序树:
/MainDeclaration[@xmlns="http://webstds.ipc.org/175x/2.0"]/Product[@unitType="Each"]/MaterialInfo/HomogeneousMaterialList/HomogeneousMaterial[@name="UNS C36000"]/SubstanceCategoryList
作为指南是:
Dim node As IXMLDOMElement
Set node = doc.DocumentElement.LastChild.ChildNodes(1).FirstChild.FirstChild.ChildNodes(1).ChildNodes(1)
'^^ Above is: MainDeclaration> Product > MaterialInfo > HomogeneousMaterialList> HomogeneousMaterial > SubstanceCategoryList > SubstanceCategory
Dim i As Long
For i = 0 To node.ChildNodes.Length - 1
Debug.Print node.ChildNodes(i).BaseName '<== Substance
Next i
参考:
答案 1 :(得分:1)
我通过使用命名空间管理器解决了它。感谢@Qharr 这是
......代码的一部分......
XmlNamespaces = "xmlns:d='http://webstds.ipc.org/175x/2.0'"
oXMLFile.SetProperty "SelectionNamespaces", XmlNamespaces
If IsEmpty(mainWorkBook.Sheets("Sheet1").Range("D" & i)) Then
Substancename = mainWorkBook.Sheets("Sheet1").Range("H" & i).Value
CASNumber = mainWorkBook.Sheets("Sheet1").Range("I" & i).Value
SubAmount = mainWorkBook.Sheets("Sheet1").Range("J" & i).Value
Set SubstanceCategoryNode = oXMLFile.SelectNodes("//d:SubstanceCategory")
设置Substancenode = oXMLFile.createElement(“d:Substance”)