2016-12-12 19 views
-2

誰かが私にどのようにforループを書くことができますこれらすべてのゾーンのノードを繰り返し、クラスリードのテキストを取得できますか?forループxmlの構文解析java

<zones count="13"> 
        <zone type="RECT" flags="4099" class="Headline" num="1"> 
         <zrect unit="pix">0,1097,2173,1303</zrect> 
         <ztext type="XML" textformat="XML"> 
          <REGION> 
           <PARAGRAPH> 
            <LINE> 
             <WORD Rect="27,933,272,1067">ma</WORD> 
             <BLANK/> 
             <WORD Rect="325,933,820,1096">ekdum</WORD> 
             <BLANK/> 
             <WORD Rect="877,933,982,1065">gyani</WORD> 
             <BLANK/> 
             <WORD Rect="1040,933,1829,1096">chu</WORD> 
             <BLANK/> 
            </LINE> 
           </PARAGRAPH> 
          </REGION> 
         </ztext> 
         <source/> 
        </zone> 
        <zone type="RECT" flags="4099" class="Author" num="2"> 
         <zrect unit="pix">0,1326,324,1372</zrect> 
         <ztext type="XML" textformat="XML"> 
          <REGION> 
           <PARAGRAPH> 
            <LINE> 
             <WORD Rect="4,1126,44,1158">By</WORD> 
             <BLANK/> 
             <WORD Rect="54,1126,131,1151">Sano</WORD> 
             <BLANK/> 
             <WORD Rect="145,1126,272,1151">shrest</WORD> 
             <BLANK/> 
            </LINE> 
           </PARAGRAPH> 
          </REGION> 
         </ztext> 
         <source/> 
        </zone> 
        <zone type="RECT" flags="4099" class="Lead" num="3"> 
         <zrect unit="pix">0,1384,475,1584</zrect> 
         <ztext type="XML" textformat="XML"> 
          <REGION> 
           <PARAGRAPH> 
            <LINE> 
             <WORD Rect="5,1174,42,1192">Dherai</WORD> 
             <BLANK/> 
             <WORD Rect="55,1178,118,1198">years</WORD> 
             <BLANK/> 
             <WORD Rect="130,1178,166,1192">dekhin</WORD> 
             <BLANK/> 
             <WORD Rect="179,1174,263,1192">gadi</WORD> 
             <BLANK/> 
             <WORD Rect="277,1174,331,1192">banaune</WORD> 
             <BLANK/> 
             <WORD Rect="344,1174,399,1192">manche</WORD> 
             <BLANK/> 
            </LINE> 
            <LINE> 
             <WORD Rect="4,1203,91,1226">haru</WORD> 
             <BLANK/> 
             <WORD Rect="115,1203,147,1221">mehanat</WORD> 
             <BLANK/> 
             <WORD Rect="172,1207,218,1221">gardai</WORD> 
             <BLANK/> 
             <WORD Rect="241,1203,399,1226">chan</WORD> 
             <BLANK/> 
            </LINE> 
            <LINE> 
             <WORD Rect="3,1236,63,1255">ramro</WORD> 
             <BLANK/> 
             <WORD Rect="80,1233,102,1250">gadi</WORD> 
             <BLANK/> 
             <WORD Rect="119,1231,214,1255">nirman</WORD> 
             <BLANK/> 
             <WORD Rect="232,1231,323,1254">garna</WORD> 
             <BLANK/> 
             <WORD Rect="341,1236,400,1250">lai</WORD> 
             <BLANK/> 
            </LINE> 
           </PARAGRAPH> 
          </REGION> 
         </ztext> 
         <source/> 
        </zone> 
        <zone type="RECT" flags="4099" class="Paragraph" num="4"> 
         <zrect unit="pix">0,1596,478,2249</zrect> 
         <ztext type="XML" textformat="XML"> 
          <REGION> 
           <PARAGRAPH> 
            <LINE> 
             <WORD Rect="28,1352,74,1366">Ramro</WORD> 
             <BLANK/> 
             <WORD Rect="82,1356,114,1366">gadi</WORD> 
             <BLANK/> 
             <WORD Rect="122,1356,151,1369">are,</WORD> 
             <BLANK/> 
             <WORD Rect="158,1352,179,1366">for</WORD> 
             <BLANK/> 
             <WORD Rect="186,1356,196,1366">a</WORD> 
             <BLANK/> 
             <WORD Rect="202,1352,254,1369">variety</WORD> 
             <BLANK/> 
             <WORD Rect="262,1352,274,1366">of</WORD> 
             <BLANK/> 
             <WORD Rect="283,1356,348,1368">reasons,</WORD> 
             <BLANK/> 
             <WORD Rect="356,1352,400,1369">ramro</WORD> 
             <BLANK/> 
            </LINE> 
           </PARAGRAPH> 
          </REGION> 
         </ztext> 
         <source/> 
        </zone> 

私は、すべてのゾーンのテキストを取得することができましたが、特に属性クラスで=はW3学校から

+0

あなたはXMLを解析するためには何を使用していますか? – rafaelbattesti

+0

私はxpathを使用しています。私はノードの位置を与えることはできませんが、XMLファイルごとに構造が変わります。つまりここではクラスLeadはnum = 3にありますが、他のxmlではLeadクラスはnum = 1にあります。 – Light

+0

w3schoolsによると、 '// zone [@ class = 'Lead']'を使用すると、Leadクラスのすべてのゾーンが取得されます。その後、それらをループして必要なテキストを取得することができます。 – rafaelbattesti

答えて

0

例を「リード」ではありません。 ソース:

<bookstore> 
    <book category="cooking"> 
     <title lang="en">Everyday Italian</title> 
     <author>Giada De Laurentiis</author> 
     <year>2005</year> 
     <price>30.00</price> 
    </book> 
    <book category="children"> 
     <title lang="en">Harry Potter</title> 
     <author>J K. Rowling</author> 
     <year>2005</year> 
     <price>29.99</price> 
    </book> 
    <book category="web"> 
     <title lang="en">XQuery Kick Start</title> 
     <author>James McGovern</author> 
     <author>Per Bothner</author> 
     <author>Kurt Cagle</author> 
     <author>James Linn</author> 
     <author>Vaidyanathan Nagarajan</author> 
     <year>2003</year> 
     <price>49.99</price> 
    </book> 
    <book category="web" cover="paperback"> 
     <title lang="en">Learning XML</title> 
     <author>Erik T. Ray</author> 
     <year>2003</year> 
     <price>39.95</price> 
    </book> 
</bookstore> 

そして、あなたが希望するノードを抽出するJavaScript機能:http://www.w3schools.com/xml/xpath_syntax.asp

これはxmlです。 //book[@category='web']を使用して、属性=値のペアを持つすべてのノードを取得していることに注目してください。同様に、//zone[@class='Lead']もできます。

<html> 
<body> 

<p id="demo"></p> 

<script> 
function showResult(xml) { 
    var txt = ""; 
    path = "//book[@category='web']/title"; 
    if (xml.evaluate) { 
     var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null); 
     var result = nodes.iterateNext(); 
     while (result) { 
      txt += result.childNodes[0].nodeValue + "<br>"; 
      result = nodes.iterateNext(); 
     } 
    document.getElementById("demo").innerHTML = txt; 
} 
</script> 

</body> 
</html>