Dom4j封裝解析xml資料(個人實際案例)
public static void main(String[] args) throws DocumentException {
//Document document = DocumentHelper.parseText(Dom4jOfEnclosure());
Dom4jOfEnclosure(); // xml的封裝
System.out.println(Dom4jOfAnalysis(DocumentHelper.parseText(Dom4jOfEnclosure()).getRootElement())); // xml的解析
}
public static Map<String, Object> Dom4jOfAnalysis(Element node) {
// 獲取 父節點下的所有的子節點
Element table_ = node.element("baseInfo");
Element mappingID = table_.element("mappingID");
List<Element> nodes = node.elements("row");
// Node nodes = node.selectSingleNode("row");
// System.out.println(nodes.size());
Map<String, Object> map_archives = new HashMap<String, Object>();
if (!(mappingID.getTextTrim().equals(""))) {
// System.out.println("SYS_TABLE_TYPE" + ":" + mappingID.getText());
} else {
System.out.println("無T 表 指向");
return null;
}
for (int i = 0; nodes != null && i < nodes.size(); i++) {
Element archives_root = nodes.get(i).element("fields");
// System.out.println(archives_root.getName());
Iterator<Element> iterator_archives_field = archives_root.elementIterator();
map_archives = setMap(iterator_archives_field);
map_archives.put("SYS_TABLE_TYPE", mappingID.getText());
// System.out.println(map_archives.size());
// 案卷節點 下面的 卷內資訊
Element element_children = nodes.get(i).element("children");
if (element_children == null)
return map_archives;
List<Element> element_child_folders = nodes.get(i).element("children").elements("row");
List<Map<String, Object>> list_child_folder = new ArrayList<Map<String, Object>>();
map_archives.put("list_children", list_child_folder);
for (int j = 0; element_child_folders != null && j < element_child_folders.size(); j++) {
Map<String, Object> map_folder = null;
Element folder_rows = element_child_folders.get(j);
// 卷內資訊
Element folder_fields = folder_rows.element("fields");
Iterator<Element> iterator_folder_field = folder_fields.elementIterator();
map_folder = setMap(iterator_folder_field);
if (map_folder != null && map_folder.size() > 0) {// 如果卷內資訊
// 存在,則查詢
// 原文資訊
list_child_folder.add(map_folder);
Element element_files = folder_rows.element("files");
List<Map<String, Object>> list_original = new ArrayList<Map<String, Object>>();
List<Element> list_original_fields = element_files.elements("file");
map_folder.put("list_children", list_original);
for (int k = 0; list_original_fields != null && k < list_original_fields.size(); k++) {
Map<String, Object> map_original = new HashMap<String, Object>();
list_original.add(map_original);
Element elemnt_file = list_original_fields.get(k);
Iterator<Element> iterator_original_field_ = elemnt_file.elementIterator();
while (iterator_original_field_.hasNext()) {
Element original_ = iterator_original_field_.next();
// System.out.println(original_.getName());
if (!(original_.getTextTrim().equals(""))) {
// System.out.println(original_.getName() + ":"
// + original_.getText());
String key = "";
if (original_.getName().equals("filetitle")) {
key = "FILE_TITLE";
}
if (original_.getName().equals("filesize")) {
key = "FILE_SIZE";
}
if (original_.getName().equals("filepath")) {
key = "FILE_PATH";
}
if (original_.getName().equals("filename")) {
key = "FILE_NAME";
}
if (original_.getName().equals("filetype")) {
key = "FILE_TYPE";
}
if (!key.isEmpty()) {
map_original.put(key, original_.getText());
}
}
}
}
} else {
continue;
}
}
}
return map_archives;
}
public static Map<String, Object> setMap(Iterator<Element> iterator)
{
HashMap<String, Object> map = new HashMap<String, Object>();
while(iterator.hasNext())
{
Element element = iterator.next();
Attribute attribute = element.attribute("id");
if (!(element.getTextTrim().equals(""))) {
//System.out.println(attribute.getValue() + ":" + element.getText());
map.put(attribute.getValue(), element.getText());
}
}
return map;
}
private static String Dom4jOfEnclosure() {
doc = DocumentHelper.createDocument();
doc.setXMLEncoding("GBK");
Element parameters = doc.addElement("pde-interface-content");
Element baseInfo = parameters.addElement("baseInfo");
Element mappingID = baseInfo.addElement("mappingID");
mappingID.addAttribute("explain", "固定值");
mappingID.setText("123");
Element communicationCode = baseInfo.addElement("communicationCode");
communicationCode.addAttribute("explain", "固定值");
communicationCode.setText("123");
Element handleType = baseInfo.addElement("handleType");
handleType.addAttribute("explain", "固定值");
handleType.setText("1");
Element sourceCode = baseInfo.addElement("sourceCode");
sourceCode.addAttribute("explain", "來源編碼");
sourceCode.setText("ETL");
Element fileSourceInfo = parameters.addElement("fileSourceInfo");
Element sourceType = fileSourceInfo.addElement("sourceType");
sourceType.addAttribute("explain", "檔案來源型別");
sourceType.setText("2");// 1=FTP 2=絕對路徑
Element ftp = fileSourceInfo.addElement("ftp");
Element host = ftp.addElement("host");
host.addAttribute("explain", "ftp伺服器IP地址");
host.setText("127.0.0.1");
Element ftpuser = ftp.addElement("user");
ftpuser.addAttribute("explain", "ftp使用者名稱");
ftpuser.setText("pde");
Element pwd = ftp.addElement("pwd");
pwd.addAttribute("explain", "ftp使用者口令");
pwd.setText("888");
Element port = ftp.addElement("port");
port.addAttribute("explain", "ftp伺服器埠號");
port.setText(String.valueOf(21));
Element absolutePath = fileSourceInfo.addElement("absolutePath");
Element absolutePathRoot = absolutePath.addElement("absolutePathRoot");
absolutePathRoot.addAttribute("explain", "檔案絕對路徑根目錄");
absolutePathRoot.setText("absolutePathRoot");
Element row = parameters.addElement("row");
Element fields = row.addElement("fields");
Element field = fields.addElement("field");
field.addAttribute("explain", "");
field.setText("案卷欄位值");
field.addAttribute("isKey", "true");
field.addAttribute("type", "2");
field.addAttribute("id", "1");
Element children = row.addElement("children");
Element rowChil = children.addElement("row");
Element Chilfields = rowChil.addElement("fields");
Element Chilfield = Chilfields.addElement("field");
Chilfield.addAttribute("explain", "");
Chilfield.setText("卷內欄位值");
Chilfield.addAttribute("isKey", "true");
Chilfield.addAttribute("type", "2");
Chilfield.addAttribute("id", "1");
Element files = rowChil.addElement("files");
Element file = files.addElement("file");
Element filetitle = file.addElement("filetitle");
filetitle.addAttribute("explain", "原文標題");
filetitle.addAttribute("isKey", "false");
filetitle.setText("原文標題.txt");
Element filesize = file.addElement("filesize");
filesize.addAttribute("explain", "原文大小");
filesize.setText("0");
Element filepath = file.addElement("filepath");
filepath.addAttribute("explain", "原文路徑,包含檔名");
filepath.setText("C:/path/原文路徑,包含檔名");
Element filename = file.addElement("filename");
filename.addAttribute("explain", "原文名稱(檔名),包含副檔名");
filename.setText("原文名稱(檔名),包含副檔名");
Element filetype = file.addElement("filetype");
filetype.addAttribute("explain", "原文型別");
filetype.setText("原文型別");
Element sourceid = file.addElement("sourceId");
sourceid.addAttribute("explain", "來源ID");
sourceid.setText("來源ID");
Element filehandleType = file.addElement("handleType");
filehandleType.addAttribute("explain", "操作型別");
filehandleType.setText("1");// 1=新增2=修改3=刪除
return doc.asXML();
}