dom4j讀取兩個xml檔案 然後 copy
阿新 • • 發佈:2018-11-02
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();
}
}
}
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();
}
}
}