提问人:Scott Holtzman 提问时间:4/13/2017 最后编辑:Cindy MeisterScott Holtzman 更新时间:1/11/2020 访问量:1178
将筛选的 XML 映射到 Excel 表
Map Filtered XML to an Excel Table
问:
我有以下代码将 XML 架构映射到 Excel 表(如 ListObjects)。
Sub MapXMLFieldsToExcelCells(wb As Workbook, sLOB As String)
'*******************************************************************
'** Name: xmlFieldMap
'** Purpose: Maps fields of existing xlmMap (named "xmlData") to file
'** Dependents: TieXMLToExcel (remapping of xml file)
'** Notes:
'*******************************************************************
sProcName = "MapXMLFieldsToExcelCells"
With wb
Dim xMap As XmlMap
Set xMap = .XmlMaps(sLOB)
Dim wsXMLMain As Worksheet
Set wsXMLMain = .Worksheets("xml" & IIf(Left(.Name, 2) = "SA", "SA", "") & sLOB)
Dim wsConfig As Worksheet
Set wsConfig = .Worksheets("rConfig")
With wsConfig
Dim rNode As Range
For Each rNode In .Range("xmlNodeMapping").Columns(1).Cells
Dim sNodePath As String
sNodePath = rNode & rNode.Offset(, 1)
Dim sTable As String
sTable = rNode.Offset(, 2)
'only have this here in case node does not exist
'caveat is that it will fail also if node is just written out wrong
Application.DisplayAlerts = False
'On Error Resume Next
wsXMLMain.ListObjects(sTable).ListColumns(rNode.Offset(, 3).Value2).XPath.SetValue xMap, sNodePath
'On Error GoTo ErrorReport
Application.DisplayAlerts = True
Next
End With
.XmlMaps(sLOB).DataBinding.Refresh
End With
End Sub
(请忽略此处的项目特定行 - 没有太多)
代码效果很好。如果我传递这样的配置表,我可以映射节点和节点的属性:
Xpath Node Table Field
/root/d/p/t/ ar tForms AR
/root/d/p/t/ @n tFormsa name
如果可能的话,我想做的是在节点上应用过滤器以仅映射该节点的某些元素。
例如,基于正确的语法:
Xpath Node Table Field
/root/d/p/t[item='ar']/ type tForms type
只会从 = ar 的节点 t 中提取元素。
我无法让它工作,也无法在网上找到任何东西。我可以接受这不是一个选择,但想在放弃这种方法之前问问。
答: 暂无答案
评论