Home>

1. java code:

package com.android.antking.xml;
import java.io.outputstream;
import java.util.list;
import org.xmlpull.v1.xmlserializer;
import android.util.xml;
/** Generate xml file with pull
 *
 * @author antkingwei
 *
 * /
public class pullbuildxmlservice {
 public void buildxml (list<person>persons, outputstream outputstream) throws exception {
  xmlserializer serializer=xml.newserializer ();
  serializer.setoutput (outputstream, "utf-8");
  serializer.startdocument ("utf-8", true);
  serializer.starttag (null, "perisons");
  for (person person:persons) {
   serializer.starttag (null, "perison");
   serializer.attribute (null, "id", string.valueof (person.id));
   serializer.starttag (null, "name");
   serializer.text (person.name);
   serializer.endtag (null, "name");
   serializer.starttag (null, "age");
   serializer.text (string.valueof (person.age));
   serializer.endtag (null, "age");
   serializer.endtag (null, "perison");
  }
  serializer.endtag (null, "perisons");
  serializer.enddocument ();
  outputstream.close ();
 }
}

2. java ben:

package com.android.antking.xml;
public class person {
  public int id;
  public string name;
  public int age;
}

3. Calling method:

public void writefile () throws throwable {
  list<person>list=new arraylist<person>();
  for (int i=0;i<10;i ++) {
   person person=new person ();
   person.id=1;
   person.name="ant";
   person.age=12;
   list.add (person);
  }
  file file=new file (mainactivity.this.getfilesdir (), "person.xml");
  fileoutputstream outputstream=new fileoutputstream (file);
  pullbuildxmlservice service=new pullbuildxmlservice ();
  service.buildxml (list, outputstream);
}
  • Previous Java implementation of socket client connection server
  • Next Example analysis of continuous animation effect with jQuery