我是VBScript和XML编码方面的新手。但是,我的头脑正在努力理解W3学校和其他在线论坛的概念。将XML内容导入Excel
我想使用VBScript读取/解析xml文件,我的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。不知道为什么它不计算长度。
这里是我试图解析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>
欣赏你的时间和响应。
感谢&问候 拉吉
也许你的XPATH不匹配的东西。无论是否这样,我们只能确定何时看到XML。请显示您的XML。 –
感谢您的回复@AxelRichter。我粘贴了上面的示例xml代码。 – Raj
如果你像下面这样修改你的XML,那么“text()”将不再是空的:' blabla'(只是一个愚蠢的例子) –
Dominique