0

saxパーサーを使用して1つのxml解析アプリを開発する必要があります。

私は以下のxmlフィードを持っています:

<root>
  <Categories>
        <Category name="Photos">
             <article articleid="4537" title="Sonam-Steals-the-Mai-Show"/>
             <article articleid="4560" title="Big-B-Croons-For-World-Peace"/>
              <article articleid="4585" title="Yami-Paints-The-Town-Red"/>
         </Category>
       <Category name="Style">
             <article articleid="266" title="Dita wows us in a sari"/>
             <article articleid="268" title="Frieda is a natural"/>
             <article articleid="269" title="Demi finds love in Jodhpur"/>
       </Category>
    </Categories>
    </root>

ここでは、getterとsetter用に1つのクラスを作成する必要があります。

public class Laptop {
            private String brand;
            private List<String> model;
          public String getBrand() {
           return brand;
             }

       public void setBrand(String brand) {
               this.brand = brand;
                }
         public List<String> getModel() {
          return model;
           }
        public void setModel(List<String> string) {
          this.model = string;
         } 

私のxmlハンドラーは次のコードのようになります:

public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    current = true;
        if (localName.equals("Category")) {
        laptop = new Laptop();
        laptop.setBrand(attributes.getValue("name"));

    }

     else if (localName.equals("article")) {
           laptop.setModel(attributes.getValue("title"));
       } 
        }

public void characters(char[] ch, int start, int length)
        throws SAXException {
    if(current)
    {
        currentValue = new String(ch, start, length);
        current=false;
    }
         }

     public void endElement(String uri, String localName, String qName)
        throws SAXException {
          current = false;
      if (localName.equals("Category")) {
          // add it to the list
          laptops.add(laptop);

      } 
    if (localName.equals("article")) {
        laptop.getModel().add(currentValue);
        } 

これらの行で以下のエラーが発生しています:

           laptop.setModel(attributes.getValue("title"));

タイプLaptopのメソッドsetModel(List)は、引数(String)には適用できません。

どうすればそのエラーを解決できますか。これらの解決策を教えてください...

4

3 に答える 3

2

エラーは、Stringインスタンスをインスタンスに割り当てることができないことを示していListます。そして、それはかなり合理的です。以来

public void setModel(List<String> string) {
          this.model = string;
} 

List をパラメーターとして受け取ります。

可能な修正として、ラップトップ インスタンスごとListに(記事)を保持したい場合は、この方法で試すことができます。String

public void setModel(String string) {
      if (this.model == null)
           this.model = new List<String>();    
      this.model.add(string);
}
于 2013-02-27T09:23:55.917 に答える
0

@blackbelt が説明しようとしたように、メソッドによって受け入れられる値のみを割り当てることができます。TextView.setText(CharSequence)を受け入れるCharSequenceので、文字列を指定する必要がありますが、getModel() は文字列のリストを返します。List<String>

于 2013-02-27T09:38:04.047 に答える
0

このようにコーディングするように勧めているわけではありませんが、モデル クラスを避けて、次のように Handler クラス内でデータ型をキャストすることができます。

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        db = new DatabaseHelper(thecontext);
        if (qName.equals("Asa.Amms.Data.Entity.User")) {
            int length = attributes.getLength();
            for (int i = 0; i < length; i++) {
                String name = attributes.getQName(i);
                if (name.equals("Id")) {
                    id = Integer.parseInt(attributes.getValue(i));
                }
                if (name.equals("Login")) {
                    LoginID = attributes.getValue(i).toString();
                }
                if (name.equals("Name")) {
                    Name = attributes.getValue(i).toString();
                }
                if (name.equals("Password")) {
                    Password = attributes.getValue(i).toString();
                }
                if (name.equals("ProgramOfficerId")) {
                    user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
                }
            }    
            db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
        }
}
于 2015-06-14T10:27:37.667 に答える