使用dom4j解析xml字符串

public class GetStandarDocs{

//private static String menuPath = File.separator+"menu_XML.xml";

private static String fileRealPath = File.separator+"standardPDF"+File.separator;

private static Logger log = Logger.getLogger(GetStandarDocs.class);

public static Document spellStandarDocsXML(String file,List<StandarDocs> standarDocsList) throws Exception{
log.info("-----------------------spellStandarDocsXML Start------------------------------------");
if(file == null) return null;
Document document = null;
try{//这里使用DocumentHelper来解析传过来的字符串

//SAXReader read = new SAXReader();
//document = read.read(file);
此时file为文件或者文件路径
document = DocumentHelper.parseText(file);
Element root = document.getRootElement();
List<Element> elementList = root.elements();
for(Element treeElement : elementList){
root.remove(treeElement);
}
List<StandarDocs> standarDocsList1 = new ArrayList<StandarDocs>();
List<StandarDocs> standarDocsList2 = new ArrayList<StandarDocs>();
for(StandarDocs standarDocs : standarDocsList){
String lv = standarDocs.getLv();
if("1".equals(lv)){
standarDocsList1.add(standarDocs);
}else{
standarDocsList2.add(standarDocs);
}
}
for(StandarDocs standarDocs1:standarDocsList1){
Element treeElement = root.addElement("tree");
treeElement.addAttribute("id", changeEmpety(standarDocs1.getId()));
treeElement.addAttribute("lv", changeEmpety(standarDocs1.getLv()));
Element textElement = treeElement.addElement("text");
textElement.setText(changeEmpety(standarDocs1.getName()));
Element targetElement = treeElement.addElement("target");
targetElement.setText("Rmain");
Element titleElement = treeElement.addElement("title");
titleElement.setText(changeEmpety(standarDocs1.getTitle()));
Element linkElement = treeElement.addElement("link");
linkElement.setText(URLEncoding(changeEmpety(standarDocs1.getLink())));
for(StandarDocs standarDocs2:standarDocsList2){
String parentid = standarDocs2.getParentid();
if(parentid.equals(changeEmpety(standarDocs1.getId()))){
Element subTreeElement = treeElement.addElement("tree");
subTreeElement.addAttribute("id", changeEmpety(standarDocs2.getId()));
subTreeElement.addAttribute("lv", changeEmpety(standarDocs2.getLv()));
Element subtextElement = subTreeElement.addElement("text");
subtextElement.setText(changeEmpety(standarDocs2.getName()));
Element subtargetElement = subTreeElement.addElement("target");
subtargetElement.setText("Rmain");
Element subtitleElement = subTreeElement.addElement("title");
subtitleElement.setText(changeEmpety(standarDocs2.getTitle()));
Element sublinkElement = subTreeElement.addElement("link");
sublinkElement.setText(URLEncoding(changeEmpety(standarDocs2.getLink())));
}
}
}
}catch(Exception e){
e.printStackTrace();
}
log.info("-----------------------spellStandarDocsXML End------------------------------------");
return document;
}

private static String URLEncoding(String changeEmpety) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
return URLEncoder.encode(changeEmpety,"UTF-8");
}

public static void deleteFile(String name,String filePath){
log.info("-----------------------RemoveStandarDocsAction Start------------------------------------");
try{
if(filePath!=null){
filePath = filePath + fileRealPath + name;
}else{
return;
}
log.info("-----------------------filePath:"+filePath+"------------------------------------");
File file = new File(filePath);
if(file.exists()) file.delete();
}catch(Exception e){
e.printStackTrace();
}
log.info("-----------------------RemoveStandarDocsAction End------------------------------------");
}

private static String changeEmpety(String str){
if(str==null){
str="";
}
return str;
}
}

你可能感兴趣的:(dom4j)