import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestDom4j {
public static void main(String[] args) {
TestDom4j test = new TestDom4j();
//test.XmlRead();
test.XmlWrite();
}
public void XmlRead() {
String fileName = "C:/bea/wlserver6.1/webhome/WEB-INF/web.xml";
try {
File file = new File(fileName);
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
System.out.println(doc.toString());
//System.out.println(doc.asXML() );
Iterator node = doc.nodeIterator();
while (node.hasNext()) {
System.out.println(node.next());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
}
public void XmlWrite() {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("iso-8859-1");
document.addDocType("CTM_Message", "-//TFN//DTD InfoRequest 1.3//EN", "InfoRequest.dtd");
Element root = document.addElement("CTM_Message");
Element infoRequest = root.addElement("InfoRequest");
Element infoRequestBody = infoRequest.addElement("InfoRequestBody").addAttribute("ViewRequestIndicator", "111").addAttribute("QueryType", "222");
Element accessPath = infoRequestBody.addElement("AccessPath").addElement("IDOwner", "333");
Element tradeLevelIdentifiers = accessPath.addElement("TradeLevelIdentifiers").addElement("MasterReference", "4444");
//return document;
System.out.println("*****************************");
System.out.println(document.asXML());
}
}