2014-11-04 82 views
0

使用dom概念進行XML讀取的Iam,但我無法正確檢索值。使用dom cencepts讀取xml

我想用for循環語句

<area id="508" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0"> 
     <pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename> 
     <pagenumber>1</pagenumber> 
     <left>603</left> 
     <top>868</top> 
     <right>764</right> 
     <bottom>1132</bottom> 
     <polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon> 
    </area> 

以下是完整的示例XML我想要的檢索使用FOR循環BELOW部分來重新獲得。

<?xml version="1.0" encoding="UTF-8"?> 
    <articles> 
<template istemplate="true" twidth="0" theight="0" uwidth="0" uheight="0" ubottom="0">  </template> 
    <article name="11187_2014_06_14_000002_0004_NFHEZ_00004" id="4"> 
     <articlename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ_00004]]></articlename> 
    <input_pages> 
     <page name="11187_2014_06_14_000002_0004_NFHEZ" number="3"> 
      <pagepdfheight>1125</pagepdfheight> 
     <pagepdfwidth>786</pagepdfwidth> 
     <pagejpgheight>1125</pagejpgheight> 
     <pagejpgwidth>786</pagejpgwidth> 
     <pagejpgresolution>72</pagejpgresolution> 
     <name><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></name> 
    <Article_Area>0</Article_Area> 
    <Article_percentage>0</Article_percentage> 
    </page> 
    </input_pages> 
    <NoOfPage>1</NoOfPage> 
    <output_pages> 
    <page number="1" height="0" width="0"/> 
    </output_pages> 
    <read> 
    <area id="508" Type="paragraph" visible="1" source_ap="True" invert="False"   rotation="0" ignoretext="0"> 
     <pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename> 
     <pagenumber>1</pagenumber> 
     <left>603</left> 
     <top>868</top> 
     <right>764</right> 
     <bottom>1132</bottom> 
     <polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon> 
    </area> 
    <area id="507" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0"> 
     <pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename> 
      <pagenumber>1</pagenumber> 
      <left>462</left> 
      <top>868</top> 
      <right>601</right> 
      <bottom>1131</bottom> 
     <polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon> 
    </area> 
    </read> 

我的代碼是:

Dim doc As New XmlDocument() 
    Dim sValue, sPgName As String 
    Dim sLeft, sTops, sRight, sBottom As String 
    Dim ObjZoneInfo As CZoneInfo 
    Try 
     doc.Load(sFile) 
      If doc.ChildNodes.Item(0).Name = "xml" Then 
      If doc.ChildNodes.Item(1).Name = "articles" Then 
       For i As Integer = 0 To doc.ChildNodes.Item(1).ChildNodes.Count - 1 
        If doc.ChildNodes.Item(2).ChildNodes.Item(i).Name = "article" Then 
         ObjZoneInfo = New CZoneInfo 
         For j As Integer = 0 To doc.ChildNodes.Item(2).ChildNodes.Item(i).ChildNodes.Count - 1 
          sValue = doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).InnerText 


          If doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).Name = "page_name" Then 
           If sProFiles.Contains(sValue) = False Then 
            sProFiles.Add(sValue) 
           End If 
           ObjZoneInfo.PageName = sValue : sPgName = sValue 

回答

0

我明白你所用 「區域」 標籤節點只搜索,那麼試試這個:

Dim tagAreas as XmlNodeList = doc.GetElementsByTagName("area") 

你可以然後使用For Each循環:

For Each tagArea as XmlElement in tagAreas Then 

'______your code here. If you wanted to retrieve the "area" tag I don't understand the code you posted 

Next 

現在,訪問區域節點的屬性,你必須使用以下方法:如果你要訪問的子節點

Dim area_id as Integer = Integer.parse(tagArea.GetAttribute("id")) 'if you want to obtain id data as integer 
Dim area_type as String = tagArea.GetAttribute("type") 
'so etc___________________ 

Dim area_pagenum as Integer = Integer.parse(tagArea("pagenumber")) 
+0

從上面的代碼,我可以接受一切標籤,但如何獲得低於標籤作爲單獨的價值。 我需要將每個分別設置爲「area id,type,可見「 – user2944173 2014-11-04 09:26:48

+0

現在檢查,希望它有幫助。 – 2014-11-04 10:43:16