一、xml 的文件格式
1.
<?xml version='1.0' encoding='UTF-8' ?>
<root>
<Test>
<TestObject ADID="9000001" Name="5325ad4" URL="http://www.xxx.com" />
<TestObject ADID="9000002" Name="5325ad1" URL="http://www.xxx.com" />
<TestObject ADID="9000003" Name="5325ad3" URL="http://www.xxx.com" />
<TestObject ADID="9000004" Name="5325ad2" URL="http://www.xxx.com" />
</Test>
</root>
public boolean createADXML(List<TestObject> data, String localDir) {
boolean bFlag = false;
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String strTmpName = sDateFormat.format(new java.util.Date()) + ".xml";
FileOutputStream fileos = null;
File newXmlFile = new File(localDir + strTmpName);
try {
if (newXmlFile.exists()) {
bFlag = newXmlFile.delete();
} else {
bFlag = true;
}
if (bFlag) {
if (newXmlFile.createNewFile()) {
fileos = new FileOutputStream(newXmlFile);
// we create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
// we set the FileOutputStream as output for the serializer,
// using UTF-8 encoding
serializer.setOutput(fileos, "UTF-8");
// <?xml version=”1.0″ encoding=”UTF-8″>
// Write <?xml declaration with encoding (if encoding not
// null) and standalone flag (if stan dalone not null)
// This method can only be called just after setOutput.
serializer.startDocument("UTF-8", null);
// start a tag called "root"
serializer.startTag(null, "root");
serializer.startTag(null, "Test");
for (TestObject ad : data) {
serializer.startTag(null, "TestObject");
serializer.attribute(null, "ADID", ad.getId());
serializer.attribute(null, "Name", ad.getName());
serializer.attribute(null, "URL", ad.getUrl());
serializer.endTag(null, "TestObject");
}
serializer.endTag(null, "Test");
serializer.endTag(null, "root");
serializer.endDocument();
// write xml data into the FileOutputStream
serializer.flush();
// finally we close the file stream
fileos.close();
}
}
} catch (Exception e) {
}
return bFlag;
}
2.
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<root xmlns="china">
... ...
<BodyList xmlns:sax="male" xml:name="chinese">
<Body>
<sax:xxid>中年男性</sax:xxid>
<name:xxname>尼大叔</name:xxname>
</Body>
<Body>
... ...
</Body>
</BodyList>
... ...
</root>
public String createXml() {
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
/**
* 此处生成的是<root xmlns="china">
*/
xmlSerializer.startTag("", "root");
xmlSerializer.attribute("", "xmlns", "china");
// ... ...
/**
* 这里会生成命名空间,命名空间和属性从表面看最大区别就是:
* 声明的时候(应该说赋值的时候),一个带前缀,一个不带前缀
*/
xmlSerializer.setPrefix("sax", "max");
xmlSerializer.setPrefix("name", "chinese");
xmlSerializer.startTag("", "BodyList");
xmlSerializer.startTag("", "Body");
/**
* 对于命名空间值的运用,把starttag的namespace参数添加上去相应的即可
* public abstract XmlSerializer startTag (String namespace, String name)
*/
xmlSerializer.startTag("sax", "xxid");
xmlSerializer.text("中年大叔");
xmlSerializer.endTag("sax", "xxid");
/**
* 如果此处设置前缀的话(即调用方法setPrefix)就会生成错误的结果
*/
xmlSerializer.startTag("name", "xxname");
xmlSerializer.text("尼大叔"); //这个是中间的文字
xmlSerializer.endTag("name", "xxname");
xmlSerializer.endTag("", "Body");
xmlSerializer.endTag("", "BodyList");
// ... ...
// 文档结束
xmlSerializer.endTag("", "root");
xmlSerializer.endDocument();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String resultString = writer.toString();
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return resultString;
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(handler);
reader.parse(new InputSource(testSAX.this.getResources()
.openRawResource(R.raw.test)));
XmlSerializer serializer = Xml.newSerializer();//使用Android提供XML工具得到序列化器
serializer.setOutput(out, "UTF-8");//设置序列化器输出方向和编码
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "persons");
for(Person person : persons){
serializer.startTag(null, "person");
serializer.attribute(null, "id", person.getId().toString());
serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(person.getAge().toString());
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
out.flush();//以防是缓存数据
out.close();
}
二、 Vcard
三、 Vcalendar
四、Vmsg