java - android sax 解析器处理程序

标签 java android saxparser

我必须使用 sax 解析器开发一个 xml 解析应用程序。

我有以下 xml feed:

<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 创建一个类:

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)

我怎样才能解决这个错误。请给我解决这些问题...

最佳答案

该错误表明您无法将 String 实例分配给 List 实例。这是相当合理的。自从

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

采用列表作为参数。

如果您想为每个笔记本电脑实例保留一个String(文章)的List,您可以尝试以这种方式进行修复

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

关于java - android sax 解析器处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15108262/

相关文章:

java - 如何避免Spring Data Jpa并发情况?

android - Paypal 仅在美国和英国以沙盒模式测试信用卡?

android - MvxSpinner selecteditem 未在 itemssource 更改时更新

java - SAX 解析器换行符

Java 如何访问文本字段

java - 在游戏框架中保存对象列表

java - JDBC插入语句不起作用

android - styles.xml 和 theme.xml 有什么区别

JAVA SAX解析成hash map并打印出来

android - 如何保存和更新xml文件中的值?