Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML内容导入Excel_Xml_Excel_Vbscript - Fatal编程技术网

将XML内容导入Excel

将XML内容导入Excel,xml,excel,vbscript,Xml,Excel,Vbscript,我是VBScript和XML编码方面的新手。不过,我还是想了解一下W3学校和其他在线论坛的概念 我想使用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(

我是VBScript和XML编码方面的新手。不过,我还是想了解一下W3学校和其他在线论坛的概念

我想使用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>

/内容/软件包[@name='GO Sales(query)]/model[@name='model']
[销售(查询)][产品][产品线]
[销售(查询)][产品][产品类型]
感谢您的时间和回复

谢谢和问候
Raj

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

dataItem
元素包含元素节点
expression
xmltattributes
表达式
包含一个文本节点。
xmltattributes
包含更多的子节点

如果找到
dataItem
元素,我们可以遍历所有这些节点。或者我们可以简单地从所有子节点获取所有文本内容。中描述了我们可以对XML DOM对象执行的操作

第二个问题是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
是我自己的选择(developercognoscom)。中的XPATH需要此前缀才能正常工作。因为XML中的所有元素都在这个名称空间中,因为根元素
report
具有这个
xmlns
属性,所以XPATH需要从这个名称空间中选择
report
的所有子元素。否则它将无法工作。 因此,XPATH选择中位于该名称空间中的所有元素都需要
dcc
前缀。如果存在多个名称空间,则需要多个前缀。每个名称空间一个


这是XPATH的精确工作条件之一。
getElement…
方法将更加容忍此名称空间。但实际上名称空间就在那里,因此也应该尊重它。

可能您的XPATH与某些内容不匹配。不管是不是这种情况,我们只能在看到XML时确定。请出示您的XML。谢谢您的回复@AxelRichter。我已经粘贴了上面的示例xml代码。如果您像下面这样修改xml,“text()”将不再是空的:
blabla
(只是一个愚蠢的例子)谢谢@AxelRichter,它的工作非常有魅力。我刚开始学习xml和vbscript几天,我正在尝试理解您使用的逻辑和关键字。我正在经历,并试图使用这些属性。另外,这意味着什么:dcc,每次遍历节点时,我都必须在SelectNodes属性中使用它。非常感谢您的时间。@Raj:请参阅我的补充。谢谢@AxelRichter,您最初的代码片段已经确定了方向,我在代码方面取得了良好的进展。我正在自动化一些需要花费大量时间的普通东西。