When we write a XML in android, you can choose difference ways, in this case we recommend XMLSerializer
, which is an efficient and maintainable way to parse XML on Android.
Implements an XML serializer supporting both DOM and SAX pretty serializing.
We put this simple line:
1 2 3 4 5 6 7 8 9 10 11 | xmlSerializer.setOutput(writer); xmlSerializer.startDocument( "UTF-8" , true ); xmlSerializer.startTag( null , "doc" ); xmlSerializer.startTag(ns, country); xmlSerializer.text(filmObject.getCountry()); xmlSerializer.endTag(ns, country); xmlSerializer.endTag(ns, "doc" ); xmlSerializer.endDocument(); xmlSerializer.flush(); |
We made a example with films:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <? xml version = '1.0' encoding = 'UTF-8' standalone = 'yes' ?> < doc > < film title = "Scarface" > < runningTime >163 min.</ runningTime > < country >USA</ country > < director >Brian De Palma</ director > < cast >< name >Al</ name > < surname >Pacino</ surname >< name > Steven</ name > < surname >Bauer</ surname >< name >Michelle</ name >< surname >Pfeiffer</ surname > < name >Mary Elizabeth</ name > < surname >Mastrantonio</ surname > </ cast > </ film > < film title = "Hitman" > < runningTime >96 min.</ runningTime > < country >USA</ country > < director >Aleksander Bach</ director > < cast >< name >Rupert</ name > < surname >Friend</ surname > < name >Zachary</ name > < surname >Quinto</ surname > < name >Hannah</ name > < surname >Ware</ surname > < name >Ciarán</ name > < surname >Hinds</ surname > </ cast > </ film > < film title = "Out of Africa" > < runningTime >160 min.</ runningTime > < country >USA</ country > < director >Sydney Pollack</ director > < cast > < name >Robert</ name > < surname >Redford</ surname > < name >Meryl</ name > < surname >Streep</ surname > < name >Klaus Maria</ name > < surname >Brandauer</ surname > < name >Michael</ name > < surname >Kitchen</ surname > </ cast > </ film > </ doc > |
In your activity you can put this lines with this films:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | public void writeXml(List<Film> films) { try { FileOutputStream fileOutputStream = new FileOutputStream(mFileOutPut); XmlSerializer xmlSerializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); xmlSerializer.setOutput(writer); xmlSerializer.startDocument( "UTF-8" , true ); xmlSerializer.startTag( null , "doc" ); insertFilms(xmlSerializer, films); xmlSerializer.endTag(ns, "doc" ); xmlSerializer.endDocument(); xmlSerializer.flush(); String dataWrite = writer.toString(); fileOutputStream.write(dataWrite.getBytes()); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void insertFilms(XmlSerializer xmlSerializer, List<Film> films) throws IOException { final String film = "film" ; String title = "title" ; String runningTime = "runningTime" ; String country = "country" ; String director = "director" ; String cast = "cast" ; Film filmObject; for ( int i= 0 ; i<films.size(); i++) { filmObject = films.get(i); xmlSerializer.startTag(ns, film); xmlSerializer.attribute(ns, title, filmObject.getTitle()); xmlSerializer.startTag(ns, runningTime); xmlSerializer.text(filmObject.getRunningTime()); xmlSerializer.endTag(ns, runningTime); xmlSerializer.startTag(ns, country); xmlSerializer.text(filmObject.getCountry()); xmlSerializer.endTag(ns, country); xmlSerializer.startTag(ns, director); xmlSerializer.text(filmObject.getDirector()); xmlSerializer.endTag(ns, director); xmlSerializer.startTag(ns, cast); insertCast(xmlSerializer, filmObject.getCast()); xmlSerializer.endTag(ns, cast); xmlSerializer.endTag( null , film); } } public void insertCast (XmlSerializer xmlSerializer, List<Actor> cast) throws IOException { String name = "name" ; String surname = "surname" ; Actor actor; for ( int i= 0 ; i<cast.size(); i++) { actor = cast.get(i); xmlSerializer.startTag( null , name); xmlSerializer.text(actor.getName()); xmlSerializer.endTag( null , name); xmlSerializer.startTag( null , surname); xmlSerializer.text(actor.getSurname()); xmlSerializer.endTag( null , surname); } } |