将XML内容导入Excel

我是VBScript和XML编程的新手。但是,我的头脑,试图理解W3学校和其他在线论坛的概念。

我想用VBScript读取/parsingXML文件,我的XML文件不是数据,而是来自应用程序的xml源代码。

以下是我正在使用的代码片段 –

Sub LoadXMLFile() Dim objXML 'for xml document Dim objNode 'for xml node item Dim i As Integer i = 0 Set objXML = CreateObject("Microsoft.XMLDOM") objXML.Load ("C:\path\test.xml") objXML.setProperty "SelectionLanguage", "XPath" Set objNode = objXML.SelectNodes("/report/queries/query/selection/dataItem/text()") 'MsgBox objNode.Text For i = 0 To (objNode.Length - 1) NodeVal = objNode(i).NodeValue MsgBox NodeVal Next End Sub 

当我通过VB代码时,objNode.Length值始终为0.不知道为什么它不计算长度。

这里是我试图parsing的XML –

 <report xmlns="http://developer.cognos.com/schemas/report/10.0/" useStyleVersion="10" expressionLocale="en-us"> <modelPath> /content/package[@name='GO Sales (query)']/model[@name='model'] </modelPath> <drillBehavior/> <queries> <query name="Query1"> <source> <model/> </source> <selection> <dataItem aggregate="none" rollupAggregate="none" name="Product line"> <expression>[Sales (query)].[Products].[Product line]</expression> <XMLAttributes> <XMLAttribute output="no" name="RS_dataType" value="3"/> <XMLAttribute output="no" name="RS_dataUsage" value="attribute"/> </XMLAttributes> </dataItem> <dataItem aggregate="none" rollupAggregate="none" name="Product type"> <expression>[Sales (query)].[Products].[Product type]</expression> <XMLAttributes> <XMLAttribute output="no" name="RS_dataType" value="3"/> <XMLAttribute output="no" name="RS_dataUsage" value="attribute"/> </XMLAttributes> </dataItem> </selection> </query> </queries> </report> 

感谢您的时间和回应。

感谢和问候拉吉

第一个问题是dataItem元素没有文本节点作为直接子节点。 所以...dataItem/text()将返回null。

dataItem元素包含元素节点expressionXMLAttributesexpression包含一个文本节点。 XMLAttributes包含更多的子节点。

如果finddataItem元素,我们可以遍历所有这些节点。 或者我们可以简单地从所有的孩子节点获取所有的文本内容。 在XML DOM对象/接口中描述了XML DOM对象的function

第二个问题是在XML中定义了一个名称空间。 这需要XMLparsing器来知道。 否则,parsing器将假定名称空间之外的所有元素,所以它不会find名称空间内的所有元素。

所以用你的XML你可以做到以下几点:

 Sub LoadXMLFile() Dim objXML 'for xml document Dim objNodeList 'for xml node lists Dim objNode 'for xml node Dim oAttribute 'for xml attribute Dim oChildNode 'for xml node Dim oSubChildNode 'for xml node Set objXML = CreateObject("Microsoft.XMLDOM") objXML.Load ("C:\Users\Axel Richter\Desktop\test.xml") objXML.setProperty "SelectionLanguage", "XPath" objXML.setProperty "SelectionNamespaces", "xmlns:dcc=""http://developer.cognos.com/schemas/report/10.0/""" Set objNodeList = objXML.SelectNodes("/dcc:report/dcc:queries/dcc:query/dcc:selection/dcc:dataItem") For Each objNode In objNodeList MsgBox objNode.Text 'the text content in this element and its child elements 'go through all attributes For Each oAttribute In objNode.Attributes MsgBox oAttribute.Name & ": " & oAttribute.Text Next 'go through all child nodes For Each oChildNode In objNode.ChildNodes 'if the child node has child b´nodes of its own, go through them too If oChildNode.HasChildNodes Then For Each oSubChildNode In oChildNode.ChildNodes MsgBox oSubChildNode.nodeName & ": " & oSubChildNode.XML Next Else MsgBox oChildNode.nodeName & ": " & oChildNode.Text End If Next Next End Sub 

objXML.setProperty "SelectionNamespaces", "xmlns:dcc=""http://developer.cognos.com/schemas/report/10.0/"""

我为命名空间定义了一个前缀dcc

http://developer.cognos.com/schemas/report/10.0/

dcc是我自己的select( d eveloper cognos c om)。 selectNodes方法中的XPATH需要此前缀才能正常工作。 由于XML中的所有元素均位于此名称空间中,因为根元素report具有此xmlns属性,所以XPATH需要从此名称空间中select所有report子元素。 否则,它将无法正常工作。 所以在这个命名空间中的XPATHselect中的所有元素都需要dcc前缀。 如果存在多个名称空间,则需要多个前缀。 每个命名空间一个。

这是XPATH的准确工作条件之一。 getElement...方法将对这个命名空间更加宽容。 但实际上名字空间在那里,所以它也应该受到尊重。