2016-08-09 7 views
1

저는 VBScript 및 XML 코딩의 초보자입니다. 그러나 W3 학교 및 기타 온라인 포럼의 개념을 이해하려고 노력하고 있습니다.XML 콘텐츠를 Excel로 가져 오기

VBScript를 사용하여 XML 파일을 읽거나 파싱하려면 XML 파일이 데이터가 아니라 응용 프로그램의 XML 소스 코드입니다. 나는 VB 코드를 통해 단계 때 objNode.Length 값은 항상 0 확실하지 평가

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 

그것의 길이를 계산하지 왜 -

다음은 내가 사용하고 코드 조각입니다.

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

당신의 시간 및 응답을 감사합니다 - 여기

내가 구문 분석하는 데 노력하고있어 XML이다.

감사 & 감사 라지

+0

XPATH가 뭔가 일치하지 않을 수도 있습니다. 이것이 사실이든 아니든간에 우리는 XML을 언제 볼 수 있는지를 결정할 수 있습니다. XML을 보여주십시오. –

+0

회신 해 주셔서 감사합니다. @AxelRichter. 위의 샘플 XML 코드를 붙여 넣었습니다. – Raj

+0

XML을 다음과 같이 수정하면 "text()"가 더 이상 비어 있지 않습니다. blabla' (그냥 바보 같은 예) – Dominique

답변

1

첫 번째 문제는 dataItem 요소가 직접 자식 노드로 더 텍스트 노드가없는 것입니다. 따라서 ...dataItem/text()은 null을 반환합니다.

요소는 expressionXMLAttributes 요소 노드를 포함합니다. expression에는 텍스트 노드가 있습니다. 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/"""

으로 내가 네임 스페이스

http://developer.cognos.com/schemas/report/10.0/에 대한 접두사 dcc을 정의합니다.

dcc 내 자신의 선택 (D eveloper C ognos C OM). 이 접두어는 selectNodes Method에있는 XPATH가 제대로 작동하는 데 필요합니다. 루트 요소 report에이 xmlns 특성이 있기 때문에 XML의 모든 요소가이 네임 스페이스에 있으므로 XPATH는이 네임 스페이스에서 report의 모든 하위 요소를 선택해야합니다. 그렇지 않으면 작동하지 않습니다. 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

관련 문제