package com.spg.readXml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.spg.bean.DocFile;
import com.spg.bean.DocFiles;
public class GetXml2DocFile {
public static DocFiles getDocFiles(DocFiles docFiles) throws Exception{
SAXReader read = new SAXReader();
Document document = null;
DocFile docFile = null;
try {
document = read.read(ClassLoader.getSystemResourceAsStream("documents.xml"));
Element root = document.getRootElement();//units
List<Element> unit = root.elements();
//遍历unit
for(Element e4unit : unit){
// 取属性封装
if(null != e4unit.attributeValue("type")){
docFiles.setType(e4unit.attributeValue("type"));
}else{
throw new Exception("the type is null");
}
if(null != e4unit.attributeValue("reference")){
docFiles.setIsReference(e4unit.attributeValue("reference"));
}else{
docFiles.setIsReference("N");
}
List<Element> attrList = e4unit.elements();
String[] attrName = null;
List<String> attrValue = new ArrayList<String>();
Map<String[], String> docFilesSingleMap = new HashMap<String[], String>();
Map<String[], String[]> docFilesRepeatMap = new HashMap<String[], String[]>();
Map<String[], String> docFileSingleMap = null;
Map<String[], String[]> docFileRepeatMap = null;
List<DocFile> docFileList = new ArrayList<DocFile>();
//unit下所有子元素
for(Element e4attr : attrList){
//非多值属性
if(null == e4attr.attributeValue("isRepeating") || !e4attr.attributeValue("isRepeating").equalsIgnoreCase("Y")){
attrName = new String[2];
attrName[0] = e4attr.getName();
if(e4attr.attributeValue("type") != null){
attrName[1] = e4attr.attributeValue("type");
}else{
attrName[1] = "String";
}
docFilesSingleMap.put(attrName, e4attr.getTextTrim());
// docFiles.setSingleAttrs(singleMap);
}else{//多值属性情况
attrName = new String[2];
attrName[0] = e4attr.getName();
if(e4attr.attributeValue("type") != null){
attrName[1] = e4attr.attributeValue("type");
}else{
attrName[1] = "String";
}
List<Element> values = e4attr.elements();
StringBuilder sb = new StringBuilder();
for(Element value : values){
sb.append(value.getTextTrim()+";");
}
attrValue = null;
docFilesRepeatMap.put(attrName, sb.toString().split(";"));
// docFiles.setRepeatAttrs(docFilesRepeatMap);
}
//封装docFile
if(e4attr.getName().equals("documents")){
List<Element> documents = e4attr.elements();
docFileSingleMap = new HashMap<String[], String>();
docFileRepeatMap = new HashMap<String[], String[]>();
for(Element e4document : documents){
docFile = new DocFile();
//设置属性
if(e4document.attributeValue("name") != null){
docFile.setName(e4document.attributeValue("name"));
}
if(e4document.attributeValue("type") != null){
docFile.setType(e4document.attributeValue("type"));
}
//非多值属性
if(null == e4attr.attributeValue("isRepeating") || !e4document.attributeValue("isRepeating").equalsIgnoreCase("Y")){
attrName = new String[2];
attrName[0] = e4document.getName();
if(e4document.attributeValue("type") != null){
attrName[1] = e4document.attributeValue("type");
}else{
attrName[1] = "String";
}
docFileSingleMap.put(attrName, e4document.getTextTrim());
// docFile.setSingleAttrs(docFileSingleMap);
}else{//多值属性情况
attrName = new String[2];
attrName[0] = e4document.getName();
if(e4document.attributeValue("type") != null){
attrName[1] = e4document.attributeValue("type");
}else{
attrName[1] = "String";
}
List<Element> values = e4document.elements();
StringBuilder sb = new StringBuilder();
for(Element value : values){
sb.append(value.getTextTrim()+";");
}
attrValue = null;
docFileRepeatMap.put(attrName, sb.toString().split(";"));
// docFile.setRepeatAttrs(docFileRepeatMap);
}
docFileList.add(docFile);
docFiles.setDocFile(docFileList);
}
docFile.setSingleAttrs(docFileSingleMap);
docFile.setRepeatAttrs(docFileRepeatMap);
}
}
docFiles.setSingleAttrs(docFilesSingleMap);
docFiles.setRepeatAttrs(docFilesRepeatMap);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docFiles;
}
}
dom4j解析xml,自己用的
最新推荐文章于 2025-03-20 20:27:11 发布