0

sax を使用して 1 つの Android xml 解析を開発する必要があります。

これは私のxmlフィードです:

  <root>
 <Categories>
         <Category name="book">
   <Articles>
     <article articleid="170" title="java programming">
      <thumb_image>
     <image url="http://website/var/tmp/thumb_2536_design_pavitra-rajaram_1_thumb_dec2012__forfeed.jpeg"/>
      </thumb_image>
      <images>
      <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
       <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
       </images>
       </article>
     <article articleid="171" title="Android programming">
     <thumb_image>
     <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/>
     </thumb_image>
       <images>
       <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
      <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
        </images>
       </article>
       </Article>
  </Category>
  <Category name="Animals">
  <Articles>
          <article articleid="81" title="Dog">
          <thumb_image>
          <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/>
         </thumb_image> 
         <images>
         <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
           <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
         </images>
          </article>
        <article articleid="81" title="Cat">
         <thumb_image>
           <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/>
          </thumb_image>  
          <images>
          <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
          <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/>
           </images>
           </article>
          </Article>
          </Category>
          </Categories>

カテゴリ名と記事のタイトルを取得しました..しかし、画像を取得できませんでした..画像を取得するにはどうすればよいですか..plsは私を助けてくれます...

私は以下のコードを使用しました:

 public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    // reset
    tempVal = "";
    if (qName.equalsIgnoreCase("Category")) {
        // create a new instance of Laptop
        laptop = new Laptop();
        laptop.setBrand(attributes.getValue("name"));
    }
    else  if (qName.equalsIgnoreCase("article")) {
        // create a new instance of Laptop
        article = new Laptop();
        article.setModel(attributes.getValue("title"));
    } 
    else  if (qName.equalsIgnoreCase("thumb_image")) {

        // create a new instance of Laptop
        image = new Laptop();
        image.setImageURL(attributes.getValue("url"));
    } 

}

これらの解決策を教えてください... thum_imageタグだけから画像のURLを取得するにはどうすればよいですか...

4

1 に答える 1

1

startElementとendElementの両方にハンドラーを使用します。パーサーのメンバーとしてStackを使用することをお勧めします。

    private tagStack = new Stack();

    public void startElement(String uri, String localName, String qName,
    Attributes attributes) {
      ...
      else if (qName.equals("image")) {
        if ("thumb_image".equals(tagStack.peek())) {
          image = new Laptop();
          image.setImageURL(attributes.getValue("url"));
        }
      }
      // make sure it is at the end of method
      tagStack.push(qName);
    }

    public void endElement(String uri, String localName, String qName) {
      ...
      tagStack.pop();
    }

親指の画像のみが必要で、それ以上ネストされないようにする場合は、bool in_thumb_imageをstartElement()からtrueに設定し、qName == "thumb_image"の場合はendElement()からfalseに設定できます。スタックはより一般的です。より高いレベルでは、tagStack.search();を使用できます。

そして話題から外れます。XMLでは大文字と小文字が区別されるため、ignoreCaseは私の意見では使用しないでください。

于 2013-02-19T15:12:17.720 に答える