2016-08-09 75 views
1

我是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> 

欣赏你的时间和响应。

感谢&问候 拉吉

+0

也许你的XPATH不匹配的东西。无论是否这样,我们只能确定何时看到XML。请显示您的XML。 –

+0

感谢您的回复@AxelRichter。我粘贴了上面的示例xml代码。 – Raj

+0

如果你像下面这样修改你的XML,那么“text()”将不再是空的:' blabla'(只是一个愚蠢的例子) – Dominique

回答

1

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

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

如果找到dataItem元素,我们可以遍历所有这些节点。或者我们可以简单地从所有的孩子节点获取所有文本内容。我们可以用XML DOM对象做什么的描述见XML DOM Objects/Interfaces

第二个问题是在XML中定义了一个名称空间。这需要XML解析器来知道。否则,解析器将假定名称空间外的所有元素,因此它不会查找名称空间内的所有元素。

所以你的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是我自己的选择(d eveloper Ç ognos Ç OM)。 XPATH在selectNodes Method正常工作需要此前缀。由于XML中的所有元素都位于此名称空间中,因为根元素report具有此xmlns属性,XPATH需要从此名称空间中选择所有report的子元素。否则它将无法工作。 因此,dcc前缀对于位于此名称空间中的XPATH选择中的所有元素都是必需的。如果存在多个名称空间,则需要多个前缀。每个命名空间一个。

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

+0

谢谢@AxelRichter,就像一个charm.its几天,我开始学习XML和VBScript,我想了解你使用的逻辑和关键字。我正在浏览 - https://msdn.microsoft.com/en-us/library/windows/desktop/ms757878(v=vs.85).aspx并尝试使用这些属性。 另外,这是什么:dcc意味着我每次遍历节点时都必须在SelectNodes属性中使用它。 非常感谢您的时间。 – Raj

+0

@Raj:看我的补充。 –

+0

感谢@AxelRichter,您的初始代码片段已经设定了方向,并且我正在取得良好的代码进度。我正在自动化一些需要花费大量时间的世俗事物。 – Raj