有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓 sax解析器处理程序

我必须使用sax解析器开发一个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创建一个类:

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(列表)不适用于参数(字符串)

我如何解决这个错误。请给我一些解决方案


共 (0) 个答案