XML SAXParser重新格式化if else Java/Android

XML SAXParser重新格式化if else Java/Android,java,android,xml-parsing,sax,Java,Android,Xml Parsing,Sax,我有以下问题: 我使用XML解析器解析XML文件,创建动态类并设置它们的属性 我已经编写了代码,现在可以生成4个类并设置类的属性,但问题是代码是一个大的条件情况(if/else if/else),很难阅读 我想解析xml,这样我可以创建15个不同的类,所以代码变得非常大 现在,确切的问题是如何将if/elseif/else重构为可读性更好的代码?我已经搜索了一段时间,找到了一些方法,比如使用map或命令模式,但我不知道如何使用它 这是我目前正在使用的代码,正在运行: public class X

我有以下问题:

我使用XML解析器解析XML文件,创建动态类并设置它们的属性

我已经编写了代码,现在可以生成4个类并设置类的属性,但问题是代码是一个大的条件情况(if/else if/else),很难阅读

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

现在,确切的问题是如何将if/elseif/else重构为可读性更好的代码?我已经搜索了一段时间,找到了一些方法,比如使用map或命令模式,但我不知道如何使用它

这是我目前正在使用的代码,正在运行:

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);

}
公共类XmlParserSax扩展了DefaultHandler{
列表片段=null;
字符串atType=null;
字符串类型对象;
字符串currentelement=null;
字符串atColor=null;
RouteFragment路由=空;
ChapterFragment chapter=null;
FirstFragment first=null;
ExecuteFragment execute=null;
StringBuilder文本生成器;
公共XmlParserSax(){
片段=新的ArrayList();
试一试{
/**
*创建SAX解析器的新实例
**/
SAXParserFactory saxPF=SAXParserFactory.newInstance();
SAXParser sp=saxPF.newSAXParser();
XMLReader xr=sp.getXMLReader();
/**
*创建处理程序来处理每个XML标记。
**/
String file=“assets/test.xml”;
InputStream in=this.getClass().getClassLoader()
.getResourceAsStream(文件);
xr.setContentHandler(此);
xr.parse(新输入源(in));
}捕获(例外e){
系统输出打印ln(e);
}
}
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
atColor=attributes.getValue(“颜色”);
atType=attributes.getValue(“类型”);
currentelement=localName;
textBuilder=新的StringBuilder();
if(localName.equalsIgnoreCase(“模板”)){
if(类型为equalsIgnoreCase(“路由”)){
路由=新路由标记();
typeObject=“路由”;
}else if(类型:equalsIgnoreCase(“章节”)){
chapter=新ChapterFragment();
typeObject=“章”;
}else if(atType.equalsIgnoreCase(“第一”)){
first=新的FirstFragment();
typeObject=“first”;
}else if(atType.equalsIgnoreCase(“执行”)){
execute=新的EXECUTEFRAGGENT();
typeObject=“执行”;
}
}else if(localName.equalsIgnoreCase(“编号”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第章.setNumberTextcolor(“#”+atColor);
}
}else if(localName.equalsIgnoreCase(“maxnumber”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第章.设置MaxNumberColor(“#”+atColor);
}
}else if(localName.equalsIgnoreCase(“title”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第二章设置颜色(“#”+atColor);
}else if(typeObject.equalsIgnoreCase(“第一”)){
首先,设置标题颜色(“#”+atColor);
}
}else if(localName.equalsIgnoreCase(“副标题”)){
if(typeObject.equalsIgnoreCase(“第一”)){
首先,设置子标题颜色(“#”+atColor);
}
}else if(localName.equalsIgnoreCase(“文本”)){
if(typeObject.equalsIgnoreCase(“执行”)){
execute.setTextColor(“#”+atColor);
}
}
}
@凌驾
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
String text=textBuilder.toString();
if(localName.equalsIgnoreCase(“模板”)){
if(typeObject.equalsIgnoreCase(“路由”)){
fragments.add(route);//nieuw routefragment
//托沃根酒店
}else if(typeObject.equalsIgnoreCase(“章节”)){
fragments.add(chapter);//nieuw chapterframent
//托沃根酒店
}else if(typeObject.equalsIgnoreCase(“第一”)){
添加(第一);
}else if(typeObject.equalsIgnoreCase(“执行”)){
片段。添加(执行);
}
}else if(localName.equalsIgnoreCase(“文本”)){
if(typeObject.equalsIgnoreCase(“路由”)){
//route.setOmschrijving(文本);
}else if(typeObject.equalsIgnoreCase(“执行”)){
execute.setText(text);
}
}else if(localName.equalsIgnoreCase(“背景”)){
if(typeObject.equalsIgnoreCase(“路由”)){
//route.setKleur(“#”+文本);
}else if(typeObject.equalsIgnoreCase(“章节”)){
章节.背景色(“#”+文本);
}else if(typeObject.equalsIgnoreCase(“第一”)){
首先,setBackgroundColor(“#”+文本);
}else if(typeObject.equalsIgnoreCase(“执行”)){
execute.setBackgroundColor(“#”+文本);
}
}else if(localName.equalsIgnoreCase(“编号”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第章设置编号(文本);
}
}else if(localName.equalsIgnoreCase(“maxnumber”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第章setMaxNumber(文本);
}
}else if(localName.equalsIgnoreCase(“title”)){
if(typeObject.equalsIgnoreCase(“章节”)){
第章.标题(正文);
}else if(typeObject.equalsIgnoreCase(“第一”)){
第一,设置标题(文本);
}
}else if(localName.equalsIgnoreCase(“副标题”)){
if(typeObject.equalsIgnoreCase(“第一”)){
第一,设置子标题(文本);
}
}else if(localName.equalsIgnoreCase(“square”)){
if(typeObject.equalsIgnoreCase(“执行”)){
    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
            }
        });