dom4j读取两个xml文件 然后 copy

package chapter;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLReader {
    public static void main(String[] args) {
         long lasting = System.currentTimeMillis();
            try {   
                File f = new File("F:/io/client_side.xml");  
                File f1 = new File("F:/io/group_sername.xml");
                SAXReader reader = new SAXReader();   
                
                Document doc = reader.read(f);   
                Element root = doc.getRootElement();  
                
                Document doc1 = reader.read(f1);   
                Element root1 = doc1.getRootElement();
                
                OutputFormat outFmt = new OutputFormat();
                XMLWriter xmlWriter = null;
                FileWriter fw = null;
                Element foo;   
                Element foo1;

                for (Iterator i = root.elementIterator("serverGroup"); i.hasNext();) {   
                    foo = (Element) i.next();   
                    System.out.println(foo.attributeValue("name").trim());//dcz   dy1
                    System.out.println(foo.getStringValue().trim());
                    System.out.println("走到这1");
                }
                
                for (Iterator i = root1.elementIterator("Server"); i.hasNext();) {   
                    foo1 = (Element) i.next();   
                    System.out.println(foo1.elementText("Hostname"));
                    System.out.println(foo1.elementText("Port"));
                    System.out.println("这也走了****");
                }
                
                System.out.println("走到这2");
                fw=new FileWriter("F:/io/copyclient_side.xml");
                //fw.append("");
                   
                System.out.println("走到这3");
                outFmt=new OutputFormat("   ",true);
                System.out.println("走到这4"+"f="+outFmt);
                xmlWriter = new XMLWriter(fw, outFmt);
                System.out.println("走到这5");
                xmlWriter.write(doc);
                System.out.println("走到这6");
                fw.flush ();
                xmlWriter.flush ();
                xmlWriter.close ();
                System.out.println("走到这7");
                fw.close ();
            } catch (Exception e) {
                System.out.println("baocuo l ");
             e.printStackTrace();   
            }   
        
        
    }
    
}

猜你喜欢

转载自blog.csdn.net/Cai_javaXiaoFan/article/details/78789054