Android中使用XmlSerializer拼装XML数据
2010-09-05 17:49
232 查看
经常在Http Post Xml请求的时候,需要如何拼装XML数据,在Android中如何来做的呢,通常我们使用FastXmlserializer这个类。比如要拼装如下的XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request xmlns="http://www.demo.com">
<operation>GetCustomerTariffInfo</operation>
<CustomerTariffInfoRequest>
<msisdn>44079324110</msisdn>
<detailsInfo>true</detailsInfo>
</CustomerTariffInfoRequest>
</Request>
代码如下
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XmlSerializer serializer = new FastXmlSerializer();
serializer.setOutput(out, "utf-8");
// true可以让stanalone为Yes.
serializer.startDocument("utf-8", true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "request");
//设置属性
serializer.attribute(null,"xmlns","http://www.demo.com");
serializer.text("/n");
serializer.startTag(null, "account");
serializer.text("");
serializer.endTag(null, "account");
serializer.startTag(null, "nickname");
serializer.text("");
serializer.endTag(null, "nickname");
serializer.startTag(null, "password");
serializer.text("");
serializer.endTag(null, "password");
serializer.endTag(null, "request");
serializer.endDocument();
out.flush();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request xmlns="http://www.demo.com">
<operation>GetCustomerTariffInfo</operation>
<CustomerTariffInfoRequest>
<msisdn>44079324110</msisdn>
<detailsInfo>true</detailsInfo>
</CustomerTariffInfoRequest>
</Request>
代码如下
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XmlSerializer serializer = new FastXmlSerializer();
serializer.setOutput(out, "utf-8");
// true可以让stanalone为Yes.
serializer.startDocument("utf-8", true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "request");
//设置属性
serializer.attribute(null,"xmlns","http://www.demo.com");
serializer.text("/n");
serializer.startTag(null, "account");
serializer.text("");
serializer.endTag(null, "account");
serializer.startTag(null, "nickname");
serializer.text("");
serializer.endTag(null, "nickname");
serializer.startTag(null, "password");
serializer.text("");
serializer.endTag(null, "password");
serializer.endTag(null, "request");
serializer.endDocument();
out.flush();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- Android中使用XmlSerializer拼装XML数据
- Android应用中使用XmlSerializer序列化XML数据的教程
- Android开发之使用DefaultHandler处理XML数据
- Android - 使用XmlPullParser解析从服务器得到的Xml数据
- Android 应用程序中使用 Internet 数据(XML、JSON 和 protocol buffers)
- Android编程使用sax解析xml数据的方法详解
- android_13_使用XmlSerializer生成XML
- 在 Android 应用程序中使用 Internet 数据 解析 XML、JSON 和 protocol buffers 数据
- Android中使用PULL方式解析XML和XmlSerializer进行序列化
- 【第6篇】使用Json-lib的XMLSerializer的write和reader操作json数据和xml
- Android数据转化为Xml形式给客户端及客户端使用xml数据
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- 使用 xmlSerializer 解析xml 数据方法
- Android使用Http访问并解析服务器XML数据
- 在 Android 应用程序中使用 Internet 数据 解析 XML、JSON 和 protocol buffers 数据
- android应用开发笔记-xml数据资源使用
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- Android进阶——使用Pull解析和生成轻量级数据XML
- 在 Android 上使用 XML 传输数据(附例子程序)
- android使用SharedPreferences读取xml数据