Я работаю над проектом, в котором я хочу сделать приложение динамически.XmlPullParser Android проще?
Я хочу сделать это при работе с XML-файлом, где все шаблоны определены, поэтому я могу их разобрать и применить к шаблонам.
Но у меня есть вопрос о te xmlPullParser, когда есть много тегов и атрибутов, синтаксический код кажется сложным и большим и уродливым.
Есть ли лучший способ сделать это?
Это XML, что я в настоящее время используют в качестве примера:
<?xml version="1.0" encoding="UTF-8"?>
<app>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">01</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">Het Verhaal</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">02</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">De Bruul</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">03</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">Grote markt</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">04</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">De Sint-Romboutstoren</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">05</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">De Vismarkt</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">06</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">Hanswijk</title>
</template>
<template type="chapter">
<background>#10c1c5</background>
<number color="#ffffff">07</number>
<maxnumber color="#ffffff">07</maxnumber>
<title color="#ffffff">De Vest</title>
</template>
<template type="route">
<text>Ga 150 meter verder</text>
<background>#003e79</background>
</template>
<template type="route">
<text>Ga 550 meter verder</text>
<background>#0000FF</background>
</template>
<template type="route">
<text>Ga 850 meter verder</text>
<background>#00FF00</background>
</template>
</app>
и это некрасиво код разобрать его, сделать правильные объекты и установить цвета и текст:
public List<Fragment> parse() {
// parse de xml plaats de objecten in de list en return de list
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false); // use namespace ?
xpp = factory.newPullParser();
// xmluit file laden
String file = "assets/test.xml";
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream(file);
xpp.setInput(in, null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName(); // tag naam
String atType = xpp.getAttributeValue(null, "type"); // atrribuut
// type
String atColor = xpp.getAttributeValue(null, "color");
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("template")) {
if (atType.equalsIgnoreCase("route")) {
// fragments.add(new RouteFragment()); //nieuw
// routefragment toevoegen aan de lijst
route = new RouteFragment();
typeObject = "route";
} else if (atType.equalsIgnoreCase("chapter")) {
// fragments.add(new chapterFragment()); //nieuw
// chapterfragment toevoegen aan de lijst
chapter = new ChapterFragment();
typeObject = "chapter";
}
} else if (tagname.equalsIgnoreCase("number")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setNumberTextcolor(atColor);
}
} else if (tagname.equalsIgnoreCase("maxnumber")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setMaxNumberColor(atColor);
}
} else if (tagname.equalsIgnoreCase("title")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setTitleColor(atColor);
}
}
break;
case XmlPullParser.TEXT:
text = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("template")) {
if (typeObject.equalsIgnoreCase("route")) {
fragments.add(route); // nieuw routefragment
// toevoegen aan de lijst
}
if (typeObject.equalsIgnoreCase("chapter")) {
fragments.add(chapter); // nieuw chapterfragment
// toevoegen aan de lijst
}
} else if (tagname.equalsIgnoreCase("text")) {
if (typeObject.equalsIgnoreCase("route")) {
route.setOmschrijving(text);
}
} else if (tagname.equalsIgnoreCase("background")) {
if (typeObject.equalsIgnoreCase("route")) {
route.setKleur(text);
} else if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setBackgroundColor(text);
}
} else if (tagname.equalsIgnoreCase("number")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setNumber(text);
}
} else if (tagname.equalsIgnoreCase("maxnumber")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setMaxNumber(text);
}
} else if (tagname.equalsIgnoreCase("title")) {
if (typeObject.equalsIgnoreCase("chapter")) {
chapter.setTitle(text);
}
}
break;
default:
break;
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fragments;
}
Если вы хотите сохранить код коротким и чистым, попробуйте [Простой] (http://simple.sourceforge.net/download/stream/doc/ примеры/examples.php). Он предлагает подход, основанный на аннотации, аналогичный JAXB. – toniedzwiedz
Попробуйте использовать SAX Parser ... Это поможет вам –
Код уродливый, потому что он все в одном месте. –