Java工具【十五】Dom2XmlUtils:dom4j与xml转化工具类


import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@SuppressWarnings("unchecked")
public class Dom2XmlUtils {

	private static final int MAX_LEN = 300;
	
	public Document read(String fileName) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(fileName));
		
		return document;
	}
	
	private String getBeginElementStr(Element element){
		List<Attribute> attributeList = element.attributes();
		StringBuffer result = new StringBuffer().append("<").append(element.getName());
		for(Attribute attribute : attributeList){
			result.append(" ").append(attribute.getName()).append("=\"").append(attribute.getValue()).append("\"");
		}
		return result.toString();
	}
	
	private String getEndElementStr(Element element){
		return "</"+element.getName()+">";
	}
	
	public void format(Iterator<Node> nodeIterator, List<String> resultList, String blank, boolean maxLen){
		
		while(nodeIterator.hasNext()){
			Node node = nodeIterator.next();
			if(node.getNodeType() == Node.ELEMENT_NODE){
				
				Element element = (Element)node;
				String begin = this.getBeginElementStr(element);
				
				Iterator<Node> subNodeIterator = element.nodeIterator();
				if(!subNodeIterator.hasNext()){
					begin += " />";
					
					resultList.add(blank + begin);
					continue;
				}else {
					begin += " >";
					resultList.add(blank + begin);
				}
				
				format(subNodeIterator, resultList, blank + "    ", maxLen);
				
				String end = this.getEndElementStr(element);
				resultList.add(blank + end);
			}else if(node.getNodeType() == Node.TEXT_NODE){
				String text = node.getText().trim();
				if(StringUtils.isBlank(text)){
					continue;
				}
				text = text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");//.replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
				String[] textArr = text.split("\n");
				for(String item: textArr){
					item = item.trim();
					String lastLine = resultList.get(resultList.size() - 1);
					if(!maxLen && !lastLine.endsWith(">")){
						lastLine += item.trim();
						resultList.set(resultList.size() - 1, lastLine);
						continue;
					}
					if(maxLen){
						String curLine = item.trim();
						if(curLine.length() <= MAX_LEN){
							resultList.add(blank + curLine);
						}else {
							split(curLine, resultList, blank);
						}
						
						continue;
					}
					
					resultList.add(blank + item.trim());
				}
				
			}
			
		}
		
	}
	
	private static void split(String curLine, List<String> resultList, String blank){
		int index = curLine.indexOf(",", MAX_LEN);
		while (index != -1){
			String first = curLine.substring(0, index + 1);
			curLine = curLine.substring(index + 1);
			index = curLine.indexOf(",", MAX_LEN);
			
			resultList.add(blank + first);
		}
		resultList.add(blank + curLine);
	}
	
	public static void format(String fileName, boolean maxLen) throws DocumentException, IOException{
		Dom2XmlUtils utils = new Dom2XmlUtils();
		
		Document document = utils.read(fileName);
		
		Iterator<Node> nodeIterator = document.nodeIterator();
		
		List<String> resultList = new ArrayList<String>();
		utils.format(nodeIterator, resultList, "", maxLen);
		
		String first = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		String second = "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">";
		resultList.add(0, first);
		resultList.add(1, second);
		
		FileUtils.writeLines(new File(fileName), "UTF-8", resultList);
	}
	
	public static void format(String fileName) throws DocumentException, IOException{
		format(fileName, false);
		format(fileName, true);
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值