寫Document:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class DocumentWriterTest {
private static String fileName = "docTest.xml";
private static String filePath = "doc" + File.separator + fileName;
public static void main(String[] arg) {
File file = new File(fileName);
if (file.exists()) {
file.mkdirs();
}
XMLOutputter out = new XMLOutputter();
Element root = new Element("Root");
Document doc = new Document(root);
doc.setRootElement(root);
for (int i = 0; i < 3; i++) {
Element test = new Element("Test");
Element test1 = new Element("Test1");
Element test2 = new Element("Test2");
Element test3 = new Element("Test3");
test1.addContent(i + "\n");
test2.addContent(Math.exp(i) + "\n");
test3.addContent(Math.pow(i, i) + "\n");
test.addContent(test1);
test.addContent(test2);
test.addContent(test3);
root.addContent(test);
}
try {
out.output(doc, new FileWriter(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File path is : " + file.getPath());
}
}
讀Document:
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DocumentReaderTest {
private static String fileName = "docTest.xml";
private static String filePath = "doc" + File.separator + fileName;
private static String seperator = "http://Test";
public static void main(String[] arg) {
try {
/*InputStream in = DocumentReaderTest.class.getClassLoader().getResourceAsStream(filePath);
Document doc = loadDocumentFile(in);*/
Document doc = loadDocumentFile(filePath);
List testList = getElements(doc, seperator);
Iterator it = testList.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println("###################" + i);
int j = 0;
Element child = (Element) it.next();
Iterator childIt = child.elementIterator();
while (childIt.hasNext()) {
Element childEle = (Element) childIt.next();
if (childEle.getName().equalsIgnoreCase("Test1")) {
System.out.println(i + "\n" + j + "Test1 = " + childEle.getText());
}
if (childEle.getName().equalsIgnoreCase("Test2")) {
System.out.println(i + "\n" + j + "Test2 = " + childEle.getText());
}
if (childEle.getName().equalsIgnoreCase("Test3")) {
System.out.println(i + "\n" + j + "Test3 = " + childEle.getText());
}
j ++;
}
i ++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Document loadDocumentFile(String filePath) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(filePath);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
private static List getElements(Document doc, String seperator) {
return doc.selectNodes(seperator);
}
private static Document loadDocumentFile(InputStream in) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(in);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。