xml - 如何忽略 XML 命名空间

标签 xml vba excel

我有一个 XML 文件,并且这个 XML 文件声明了命名空间

<CrystalReport  xmlns="urn:crystal-reports:schemas:report-detail"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">

这导致我在 Excel 中的 VBA 代码出现问题。当我删除上面这一行的 namespace 时,它工作正常。

我的问题是:如何在不必打开 xml 文件并手动删除的情况下忽略此命名空间?

我正在使用的代码:
Public xmlDOM As MSXML2.DOMDocument60

Public Sub setXML(xmlFileName As String)

    'Set xmlDOM = CreateObject("MSXML2.DOMDocument")
    Set xmlDOM = New MSXML2.DOMDocument60
    xmlDOM.async = False
    xmlDOM.Load xmlFileName

End Sub

Public Function getNode(p_strNode As Variant) As Variant

    Dim objNodes As IXMLDOMNodeList
    Dim objNode As IXMLDOMNode
    Dim storage As Variant
    Dim X As Integer

    Set objNodes = xmlDOM.SelectNodes(p_strNode)

    Set getNode = objNodes

End Function

Public Sub SB_StartLoadClarityReport()

    Dim d_path As String
    Dim d_node As Variant
    Dim d_arrayFields As Variant

    d_path = F_GetPathXML()

    '@Temp
    d_path = Cells(1, 1).Value

    'Open XML File
    setXML (d_path)

    'Get the project fields
    Set d_node = getNode("CrystalReport/Details/Section")
    d_arrayFields = F_GetProjectFields(d_node)

End Sub

Private Function F_GetProjectFields(p_strNode As Variant)

    'Get the project fields
    'Ex: <Field Name="PROJECTNAME1" - Get PROJECTNAME1

    Dim d_arrayFields As Variant
    Dim p_item As IXMLDOMElement
    Dim d_count As Integer

    d_count = 1

    For Each p_item In p_strNode.Item(0).ChildNodes

        If d_count = 1 Then
            ReDim d_arrayFields(1 To d_count)
        Else
            ReDim Preserve d_arrayFields(1 To d_count)
        End If

        d_arrayFields(d_count) = p_item.Attributes.Item(0).Text
        d_count = d_count + 1

    Next p_item

    F_GetProjectFields = d_arrayFields

End Function

最佳答案

这对我有用(经过一些挠头)

Sub Tester()

    Const XML As String = "<?xml version='1.0'?>" & _
    "<CrystalReport  xmlns='urn:crystal-reports:schemas:report-detail' " & _
    " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " & _
    " xsi:schemaLocation='urn:crystal-reports:schemas:report-detail " & _
    " http://www.businessobjects.com/products/xml/CR2008Schema.xsd'>" & _
    "   <Test>Testing</Test>" & _
    "</CrystalReport>"

    Dim xmlDom As New MSXML2.DOMDocument60
    Dim nodeList As MSXML2.IXMLDOMNodeList
    Dim iNode As MSXML2.IXMLDOMNode

    With xmlDom
        .async = False
        .validateOnParse = True
        .LoadXML XML
        .setProperty "SelectionLanguage", "XPath"

        'set the default namespace and give it a prefix (e.g.) "xx"
        .setProperty "SelectionNamespaces", _
                     "xmlns:xx='urn:crystal-reports:schemas:report-detail'"

        'use the same default prefix in your XPath
        Set nodeList = .SelectNodes("//xx:Test")

    End With

    Debug.Print nodeList.Length
    For Each iNode In nodeList
        Debug.Print iNode.XML
    Next iNode

End Sub

关于xml - 如何忽略 XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27563616/

相关文章:

ruby-on-rails - Nokogiri XML 文件输出位于何处?

excel - 检索共享邮箱中电子邮件的电子邮件回复者名称

vba - 在 Excel : how to highlight cells that has a sepcific character more than once

Excel计算小数位数的公式

excel - 如何 COUNTIF 两组标准是否正确(可能很容易)

PHP 不使用 DOMDocument 保存

java - 使用 java 对输入 XML 进行 XSL 转换

xml - 选择在XSLT中逗号分隔的特定字符串

excel - 如何使用 VBS 运行 Excel 宏,但不运行 Workbook_Open()

excel - 在模块中调用 UserForm_Initialize()