2013-03-07 44 views
0

我有以下问题:XML的SAXParser格式化,如果其他的Java/Android的

我使用的是XML的SAXParser解析XML文件,并动态地创建类并设置其属性。

我写过的代码现在可以编写4个类并设置类的特性,但问题在于代码是一个大的条件案例(if/else if/else)并且很难阅读。

我想解析xml,所以我可以创建15个不同的类,所以代码变得非常大。

现在确切的问题是如何重构if/elseif/else以获得更好的可读代码?我已经搜索了一段时间,发现了一些方法,如使用地图或命令模式,但我不明白如何使用它?

这是我目前使用的代码和工作:

public class XmlParserSax extends DefaultHandler { 

List<Fragment> fragments = null; 
String atType = null; 
String typeObject; 
String currentelement = null; 
String atColor = null; 
RouteFragment route = null; 
ChapterFragment chapter = null; 
FirstFragment first = null; 
ExecuteFragment execute = null; 
StringBuilder textBuilder; 

public XmlParserSax() { 
    fragments = new ArrayList<Fragment>(); 
    try { 
     /** 
     * Create a new instance of the SAX parser 
     **/ 
     SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
     SAXParser sp = saxPF.newSAXParser(); 
     XMLReader xr = sp.getXMLReader(); 

     /** 
     * Create the Handler to handle each of the XML tags. 
     **/ 

     String file = "assets/test.xml"; 
     InputStream in = this.getClass().getClassLoader() 
       .getResourceAsStream(file); 

     xr.setContentHandler(this); 
     xr.parse(new InputSource(in)); 

    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 

    atColor = attributes.getValue("color"); 
    atType = attributes.getValue("type"); 
    currentelement = localName; 
    textBuilder = new StringBuilder(); 

    if (localName.equalsIgnoreCase("template")) { 

     if (atType.equalsIgnoreCase("route")) { 

      route = new RouteFragment(); 
      typeObject = "route"; 
     } else if (atType.equalsIgnoreCase("chapter")) { 

      chapter = new ChapterFragment(); 
      typeObject = "chapter"; 
     } else if (atType.equalsIgnoreCase("first")) { 
      first = new FirstFragment(); 
      typeObject = "first"; 
     } else if (atType.equalsIgnoreCase("execute")) { 
      execute = new ExecuteFragment(); 
      typeObject = "execute"; 
     } 
    } else if (localName.equalsIgnoreCase("number")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setNumberTextcolor("#" + atColor); 
     } 
    } else if (localName.equalsIgnoreCase("maxnumber")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setMaxNumberColor("#" + atColor); 
     } 

    } else if (localName.equalsIgnoreCase("title")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setTitleColor("#" + atColor); 
     } else if (typeObject.equalsIgnoreCase("first")) { 
      first.setTitleColor("#" + atColor); 
     } 
    } else if (localName.equalsIgnoreCase("subtitle")) { 
     if (typeObject.equalsIgnoreCase("first")) { 
      first.setSubtitleColor("#" + atColor); 
     } 
    } else if (localName.equalsIgnoreCase("text")) { 
     if (typeObject.equalsIgnoreCase("execute")) { 
      execute.setTextColor("#" + atColor); 
     } 
    } 

} 

@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 

    String text = textBuilder.toString(); 
    if (localName.equalsIgnoreCase("template")) { 
     if (typeObject.equalsIgnoreCase("route")) { 
      fragments.add(route); // nieuw routefragment 
            // toevoegen aan de lijst 

     } else if (typeObject.equalsIgnoreCase("chapter")) { 
      fragments.add(chapter); // nieuw chapterfragment 
            // toevoegen aan de lijst 

     } else if (typeObject.equalsIgnoreCase("first")) { 
      fragments.add(first); 
     } else if (typeObject.equalsIgnoreCase("execute")) { 
      fragments.add(execute); 
     } 
    } else if (localName.equalsIgnoreCase("text")) { 
     if (typeObject.equalsIgnoreCase("route")) { 
      // route.setOmschrijving(text); 
     } else if (typeObject.equalsIgnoreCase("execute")) { 
      execute.setText(text); 
     } 
    } else if (localName.equalsIgnoreCase("background")) { 
     if (typeObject.equalsIgnoreCase("route")) { 
      // route.setKleur("#" + text); 
     } else if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setBackgroundColor("#" + text); 
     } else if (typeObject.equalsIgnoreCase("first")) { 
      first.setBackgroundColor("#" + text); 
     } else if (typeObject.equalsIgnoreCase("execute")) { 
      execute.setBackgroundColor("#" + text); 

     } 
    } else if (localName.equalsIgnoreCase("number")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setNumber(text); 
     } 
    } else if (localName.equalsIgnoreCase("maxnumber")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setMaxNumber(text); 
     } 
    } else if (localName.equalsIgnoreCase("title")) { 
     if (typeObject.equalsIgnoreCase("chapter")) { 
      chapter.setTitle(text); 
     } else if (typeObject.equalsIgnoreCase("first")) { 
      first.setTitle(text); 
     } 
    } else if (localName.equalsIgnoreCase("subtitle")) { 
     if (typeObject.equalsIgnoreCase("first")) { 
      first.setSubtitle(text); 
     } 
    } else if (localName.equalsIgnoreCase("square")) { 
     if (typeObject.equalsIgnoreCase("execute")) { 
      execute.setBorderColor("#" + text); 

     } 
    } 
} 

public List<Fragment> getList() { 
    return fragments; 

} 

@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 

    textBuilder.append(ch, start, length); 

} 

}

+0

我曾回复过类似的回复,请检查:http://stackoverflow.com/questions/13417363/parsing-an-xml-file-in-android/13417932#13417932 – Atrix1987 2013-03-07 11:49:40

+0

@ Atrix1987我看不到为什么你的版本少了代码?你仍然使用很多其他的东西? – 2013-03-07 13:14:47

+0

还有另一种方法,使用开始元素和结束元素侦听器,我会发布一些代码 – Atrix1987 2013-03-07 13:29:29

回答

0

有这样做的另一种方式;使用startElementListener和EndTextElementListeners

首先定义你的根元素:

RootElement root = new RootElement("root"); 

定义你的子元素

Element nodeA = root.getChild("nodeA"); 
    Element nodeB = root.getChild("nodeB"); 
    Element nodeC = root.getChild("nodeC"); 

现在设置听众

root.setStartElementListener(new StartElementListener() { 
      public void start(Attributes attributes) { 
       foundElement = true;// tells you that you are parsing the intended xml 
      } 
     }); 

    nodeA.setEndTextElementListener(new EndTextElementListener() { 
      public void end(String body) { 
       //populate your pojo 
      } 
     }); 

这样你就可以用做掉所有那些if-else语句和布尔值,但是你必须活着w ñ数量的听众。

+0

嗯,我会考虑到这一点,现在我试图使用枚举和代码看起来更可读:) – 2013-03-07 13:43:55