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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package com.carlos.xml;
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document; import org.w3c.dom.Element;
import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log;
public class MainActivity extends Activity {
private List<UserInfo> infos; private UserInfo info;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
crateData();
createXml();
}
void crateData() { infos = new ArrayList<UserInfo>(); info = new UserInfo(); info.setUsername("Carlos"); info.setAge("21"); infos.add(info); info = new UserInfo(); info.setUsername("Jack"); info.setAge("19"); infos.add(info); }
void createXml() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {
DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.newDocument(); document.setXmlVersion("1.0"); document.setXmlStandalone(true); Element root = document.createElement("root"); document.appendChild(root);
for (int i = 0; i < infos.size(); i++) { info = infos.get(i); Element userinfo = document.createElement("userinfo"); root.appendChild(userinfo); Element username = document.createElement("username"); username.appendChild(document.createTextNode(info.getUsername())); Element age = document.createElement("age"); age.appendChild(document.createTextNode(info.getAge())); userinfo.appendChild(username); userinfo.appendChild(age); }
TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf = tff.newTransformer(); DOMSource domSource = new DOMSource(document);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); tf.transform(domSource, new StreamResult(baos)); String result = baos.toString(); Log.i("xml", "生成的xml数据为:\n" + result);
File file = new File(Environment.getExternalStorageDirectory(), "userinfo.xml"); if (!file.exists()) { file.createNewFile(); Log.i("xml", "该xml文件创建成功,保存在" + file.getAbsolutePath() + "下"); } else { Log.i("xml", "该xml文件已存在"); } FileOutputStream fos = new FileOutputStream(file); StreamResult xmlResult = new StreamResult(fos); tf.transform(domSource, xmlResult);
baos.close(); fos.close(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
} }
|