2013-02-02 18 views
0

我已經能夠在VBA中構建函數來解析XML並根據需要公開值。但是,有一個值得我忽略。在VBA中解析XML從我的研究中揭示數據

<osgb:topographicMember> 
    <osgb:TopographicArea fid='osgb1000000347753568'> 
    <osgb:featureCode>10053</osgb:featureCode> 
    <osgb:version>4</osgb:version> 
    <osgb:versionDate>2006-03-15</osgb:versionDate> 
    <osgb:theme>Land</osgb:theme> 
    <osgb:calculatedAreaValue>46.099150</osgb:calculatedAreaValue> 
    <osgb:changeHistory> 
     <osgb:changeDate>2001-03-09</osgb:changeDate> 
     <osgb:reasonForChange>New</osgb:reasonForChange> 
    </osgb:changeHistory> 
    <osgb:descriptiveGroup>General Surface</osgb:descriptiveGroup> 
    <osgb:descriptiveTerm>Multi Surface</osgb:descriptiveTerm> 
    <osgb:make>Multiple</osgb:make> 
    <osgb:physicalLevel>50</osgb:physicalLevel> 
    <osgb:polygon><gml:Polygon srsName='osgb:BNG'> 
    <gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>290996.130,92420.290 290998.010,92415.010 291000.000,92415.720 291005.770,92417.770 291003.890,92423.040 291000.000,92421.660 290996.130,92420.290 </gml:coordinates> 
    </gml:LinearRing> 
    </gml:outerBoundaryIs> 
    </gml:Polygon> 
    </osgb:polygon></osgb:TopographicArea> 
    </osgb:topographicMember> 

,我想獲得的值是FID = 'osgb10000000347753568'

我能得到和返回TopographicArea(使用xChild.baseName),但不裂。

任何想法歡迎!

回答

0

如果我理解正確的輸入,你需要打開TopographicArea節點並獲取其Fid屬性.text.NodeValue特性(無論適合最好根據節點的數據類型),而不是節點.baseName,即你應該使用這樣的事情:

Set XMLDOC = CreateObject("MSXML2.DOMDocument") 
..... 
Set SubNode = XMLDOC.SelectNodes("//[list of parent nodes]/topographicMember/TopographicArea/") 
fid = SubNode.Attributes.Item(0).NodeValue 

也許我是錯的符號,但關鍵的問題在這裏,你應該使用.text.NodeValue性能。添加XMLDOC的VBA觀察窗口將非常有幫助。祝你好運!

+0

嗨,明白了,謝謝。然而,有沒有什麼方法可以在試圖暴露item(0)之前測試屬性是否存在? –

+0

@AdamSherratt我想你應該檢查'Attributes.Length'屬性,它返回節點屬性的數量。如果它等於0 - 沒有屬性。還考慮使用'getNamedItem'方法來定位屬性而不是Item(#):http://msdn.microsoft.com/en-us/library/ms767592%28v=vs.85%29.aspx祝你好運!) –

+0

也看看這個:http://stackoverflow.com/q/4198381/1953175 –