-2

我有来自 IPC1752 模式的 XML,我必须根据我的 excel 数据表填充值。所以我必须做这些事情

1.将 XML 加载到我的 VBA 2.将 XML 中的所需值更改为我存储在 excel 单元格中的值 3.将修改后的 XML 保存到文件夹

'Sub UpdateXML() 调用 fnUpdateXMLByTags End Sub

函数 fnUpdateXMLByTags() 将 mainWorkBook 调暗为工作簿 将 wrsht 调暗为工作表

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(attributes) 时,它的工作 [example:oXMLFile.SelectNodes("//@itemName")]

但是当我使用 selectnodes(elements) [example:oXMLFile.SelectNodes("//Substance")] 时不起作用

请帮忙。

这是 XML https://www.jiocloud.com/s/?t=SzqFJhEABfsTQfZW&s=a2

4

2 回答 2

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")

于 2018-05-10T15:13:46.930 回答
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. https://www.w3schools.com/xml/dom_nodes_access.asp
  2. https://www.w3schools.com/jsref/prop_node_nodetype.asp
  3. XPath 不适用于在 XML 中进行选择
于 2018-05-08T15:57:57.053 回答