如何忽略XML命名空间 [英] How to ignore a XML namespace
本文介绍了如何忽略XML命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML文件,这个XML文件有命名空间声明
I have an XML file and this XML file has namespaces declared
<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代码中引起问题。当我删除上面这一行的命名空间,它的工作正常。
This is causing problems in my VBA code in Excel. When I remove the namespaces of this line above, it works fine.
我的问题是:如何忽略这个命名空间,而不必打开xml文件,手动删除
My question is: How can I ignore this namespace without have to open the xml file and remove manually?
我使用的代码:
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
推荐答案
这对我有用(经过一些头痛) p>
This worked for me (after some amount of head-scratching)
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命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文