2014-12-19 69 views
0

我有一個XML文件,這個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代碼的問題。當我刪除上面這一行的命名空間時,它工作正常。

我的問題是:如何忽略這個命名空間,而無需打開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 
+2

我們不知道你的VBA代碼,所以我們無法幫幫我。 – reporter 2014-12-19 10:10:21

+0

嗨記者。我剛剛編輯我的帖子並添加了我正在使用的代碼。請記住,正如我所說的,如果沒有XML中的名稱空間,代碼將毫無問題地運行。 – Braulio 2014-12-19 17:08:18

+1

也許嘗試http://stackoverflow.com/questions/16490839/how-to-query-default-namespace-with-msxml – 2014-12-19 17:56:08

回答

2

這爲我工作(頭刮的一定量之後)

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 
+0

蒂姆,我改變了你的寫作代碼,它的工作完美!我正在研究你顯示的現在更好理解的這些命令。非常感謝蒂姆! – Braulio 2014-12-20 00:32:18