文件格式哦哦哦

一、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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值