java dom4j解析xml文件代碼實例分享。本站提示廣大學習愛好者:(java dom4j解析xml文件代碼實例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java dom4j解析xml文件代碼實例分享正文
解析xml文件有兩種方法,一種是應用Dom去解析,這類方法寫起代碼比擬費事,關於剛動手的法式員來講比擬輕易出成績;第二種就是應用Dom4j包去解析
在要應用Dom4j包的時刻,確定要先引入包
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* @author Icer
*/
public class Dom4jDemo{
/**
*
* @param fileName 生成的xml文件名
* @param txtName 包括的對賬文件txt文件名
*/
public void createXml(String fileName,String txtName) {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element head = root.addElement("head");
Element type = head.addAttribute("type", "0");
Element code = head.addAttribute("code", "3003");
Element yhlb = head.addElement("yhlb");
yhlb.setText("01");
Element username = head.addElement("username");
username.setText("gsyh");
Element password = head.addElement("password");
password.setText("zheshimima");
Element body = root.addElement("body");
Element data = body.addElement("data");
Element dzwjm = data.addElement("dzwjm");
dzwjm.setText(txtName);
try {
//寫入文件
Writer fileWriter = new FileWriter(fileName);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(fileWriter,format);
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
*
* @param fileName 要解析的文件名
* @return 解析xml文件獲得的須要對賬的文件名
*/
public String parserXml(String fileName) {
String findFileName = "";
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element root = document.getRootElement();
for(Iterator i = root.elementIterator();i.hasNext();){
Element head = (Element) i.next();
for(Iterator j = head.elementIterator();j.hasNext();){
Element elem = (Element) j.next();
System.out.println(elem.getName()+":"+elem.getText());
for(Iterator k=elem.elementIterator();k.hasNext();){
Element last = (Element) k.next();
System.out.println(last.getName()+":"+last.getText());
findFileName = last.getText();
}
}
}
} catch (DocumentException e) {
System.out.println(e.getMessage()+"hello");
}
System.out.println("dom4j parserXml");
return findFileName;
}
/**
* 測試main辦法
* @param args
*/
public static void main(String[] args){
Dom4jDemo demo = new Dom4jDemo();
demo.createXml("D://request.xml","test.txt");
demo.parserXml("e://request.xml");
}