zoukankan      html  css  js  c++  java
  • 使用Pull解析器生成XML文件

    有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中;或者使用DOM API生成XML文件,或者也可以使用pull解析器生成XML文件,这里推荐大家使用Pull解析器。
     
    1、使用Pull解析器生成一个与itcast.xml文件内容相同的myitcast.xml文件,代码在下方
    public static String writeXML(List<Person> persons, Writer writer){
        XmlSerializer serializer = Xml.newSerializer();
        try {
            serializer.setOutput(writer);
            serializer.startDocument("UTF-8", true);
          //第一个参数为命名空间,如果不使用命名空间,可以设置为null
            serializer.startTag("", "persons");
            for (Person person : persons){
                serializer.startTag("", "person");
                serializer.attribute("", "id", person.getId().toString());
                serializer.startTag("", "name");
                serializer.text(person.getName());
                serializer.endTag("", "name");
                serializer.startTag("", "age");
                serializer.text(person.getAge().toString());
                serializer.endTag("", "age");
                serializer.endTag("", "person");
            }
            serializer.endTag("", "persons");
            serializer.endDocument();
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    使用代码如下(生成XML文件):
     
    File xmlFile = new File("myitcast.xml");
    FileOutputStream outStream = new FileOutputStream(xmlFile);
    OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
    BufferedWriter writer = new BufferedWriter(outStreamWriter);
    writeXML(persons, writer);
    writer.flush();
    writer.close();
    
    如果只想得到生成的xml字符串内容,可以使用StringWriter:
     
    StringWriter writer = new StringWriter();
    writeXML(persons, writer);
    String content = writer.toString();
    

    2、

    public static void save(List<Person> persons,OutputStream out)throws Exception{
    		XmlSerializer xmlSerializer=Xml.newSerializer();
    		xmlSerializer.setOutput(out, "UTF-8");
    		xmlSerializer.startDocument("UTF-8", true);
    		xmlSerializer.startTag(null, "persons");
    		
    		for(Person person:persons){
    			xmlSerializer.startTag(null, "person");
    			xmlSerializer.attribute(null, "id", person.getId().toString());
    			
    			xmlSerializer.startTag(null, "name");
    			xmlSerializer.text(person.getName());
    			xmlSerializer.endTag(null, "name");
    			
    			xmlSerializer.startTag(null, "age");
    			xmlSerializer.text(person.getAge().toString());
    			xmlSerializer.endTag(null, "age");
    			
    			xmlSerializer.endTag(null, "person");
    		}
    		xmlSerializer.endTag(null, "persons");
    		xmlSerializer.endDocument();
    		out.flush();
    		out.close();
    	}
    

     调用上面方法输出xml文件

    public void testSavePerson()throws Exception{
    		List<Person> persons=new ArrayList<Person>();
    		persons.add(new Person("zhangss",12,23));
    		persons.add(new Person("xiaoxiao",45,21));
    		persons.add(new Person("zhagnni",10,47));
    		File file=new File(getContext().getFilesDir().toString(),"person.xml");
    		FileOutputStream out=new FileOutputStream(file);
    		new PersonService().save(persons, out);
    	}
    
  • 相关阅读:
    Springboot在IDEA中执行,开启热部署
    java 二分法学习
    python爬虫初级--获取指定页面上的菜单名称以及链接,然后导出
    使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
    Linux CentOS6.8 项目部署脚本实现
    Springboot启动源码详解
    FreeMarker基础语法,宏,引用 等
    在eclipse中启动java程序的时候,每次都会在一个未设置断点的源码里面,卡断点
    Vue学习--
    阿里的maven镜像仓库,eclipse中使用maven下载jar包的时候提升速度
  • 原文地址:https://www.cnblogs.com/wdc224/p/3920715.html
Copyright © 2011-2022 走看看