자바 에서 xml 파일 을 분석 하여 데이터 베 이 스 를 저장 합 니 다.

2069 단어
package com.ziyun56.chpz.service.poi.util;

import java.io.File;
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 XmlDo {
	public static void main(String[] args) {
		XmlDo dd = new XmlDo();
		dd.parserXml("F:/SalesWareHouseOut_16011502970.xml");
	}
	
	public void parserXml(String fileName) {
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(inputXml);
			Element employees = document.getRootElement();
			
			//--1
			for (Iterator i = employees.elementIterator(); i.hasNext();) {
				Element employee = (Element) i.next();
				for (Iterator j = employee.elementIterator(); j.hasNext();) {
					Element h = (Element) j.next();
					System.out.println(h.attribute("Name").getText());
					System.out.println(h.attribute("MainAction").getText());
					for(Iterator k = h.elementIterator(); k.hasNext();){
						Element m = (Element) k.next();
						for(Iterator a = m.elementIterator(); a.hasNext();){
							Element n = (Element) a.next();
							System.out.println(n.attribute("Code").getText());
							System.out.println(n.attribute("ActDate").getText());
						}
					}
				}
			}
			
			//--2
			List nodes = employees.elements("Events");
			List n = nodes.get(0).elements("Event");
			System.out.println("------------------------------------------");
			for(Element e:n){
				List dfs = e.elements("DataField");
				for(Element e1:dfs){
					List ds = e1.elements("Data");
					for(Element e2:ds){
						System.out.println(e2.attribute("Code").getText());
						System.out.println(e2.attribute("ActDate").getText());
					}
				}
			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
		System.out.println("end");
	}
}

좋은 웹페이지 즐겨찾기