zoukankan      html  css  js  c++  java
  • Dom4j操作xml

    package com.kk.xml;
    
    import java.io.*;
    import java.util.*;
    
    import org.dom4j.*;
    import org.dom4j.io.*;
    
    public class OperateXML {
    
        /**
         * @param args
         * @throws IOException 
         * @throws DocumentException 
         */
        public static void main(String[] args) throws IOException, DocumentException {
            generateXML();
            parseXMLFromString();
        }
    
        /**
         * 将生成的xml以字符串形式打印
         * @throws IOException
         */
        private static void generateXML() throws IOException {
            Document document=DocumentHelper.createDocument();
            Element message=document.addElement("message");
            message.addAttribute("type", "GetAllDefence");
            Element students=message.addElement("students");
            for(int i=0;i<10;i++){
                Element student=students.addElement("student");
                student.addElement("name").setText("my name is"+i);
                student.addElement("gender").setText("男");
            }
            System.out.println(document.asXML());
        }
        
        /**
         * 将生成的xml以字符串形式打印(字节流)
         * @throws IOException
         */
        private static void generateXMLByStream() throws IOException {
            Document document=DocumentHelper.createDocument();
            Element message=document.addElement("message");
            message.addAttribute("type", "GetAllDefence");
            Element students=message.addElement("students");
            for(int i=0;i<10;i++){
                Element student=students.addElement("student");
                student.addElement("name").setText("my name is"+i);
                student.addElement("gender").setText("男");
            }
            OutputStream os=new ByteArrayOutputStream();
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter output=new XMLWriter(os,format);
            output.write(document);
            output.close();
            String str=os.toString();
            os.close();
            System.out.println(str);        
        }
        
        private static void parseXMLFromFile() throws DocumentException{
            SAXReader reader=new SAXReader();
            Document document=reader.read("students.xml"); //读流
            Element root=document.getRootElement(); //message
            //for(Iterator it=root.selectNodes("//students/student").iterator();it.hasNext();){ //xpath方式,需要jaxen.jar
            for(Iterator it=root.element("students").elementIterator("student");it.hasNext();){
                Element student=(Element) it.next();
                System.out.println(student.elementTextTrim("name"));
                System.out.println(student.elementTextTrim("gender"));
            }
        }
        
        private static void parseXMLFromString() throws DocumentException{
            String xml="<?xml version=\"1.0\" encoding=\"gbk\"?>"
                        +"<message type=\"GetAllDefence\">"
                        +"<students>"
                        +"<student num=\"0\">"
                        +"<name>my name is0</name>"
                        +"<gender>男</gender>"
                        +"</student>"
                        +"</students>"
                        +"</message>";
            Document document=DocumentHelper.parseText(xml); //读字符串
            Element root=document.getRootElement(); //message
            for(Iterator it=root.element("students").elementIterator("student");it.hasNext();){
                Element student=(Element) it.next();
                System.out.println(student.elementTextTrim("name"));
                System.out.println(student.elementTextTrim("gender"));
            }
        }
    
    }
  • 相关阅读:
    .net framework缓存遍历
    R语言中统计数据框所有项中的并集
    R语言中在数据框中批量替换指定项
    R语言中 %in%用法
    windows中如何查看端口占用情况、端口是否开启
    R语言中rbind函数和cbind的用法
    linux系统shell实现统计 plink文件基因频率
    linux 系统中实现列转行 及 行转列
    linux系统中向行末添加换行符
    R语言中实现方差和标准差
  • 原文地址:https://www.cnblogs.com/BigIdiot/p/2600906.html
Copyright © 2011-2022 走看看