1.XML文件转换为Map、List集合
public static List parseForList(String xmlDoc) {
StringReader xmlString = new StringReader(xmlDoc);
InputSource source = new InputSource(xmlString);
SAXBuilder saxb = new SAXBuilder();
List result = null;
try {
result = new ArrayList();
Document doc = saxb.build(source);
Element root = doc.getRootElement();
List node = root.getChildren();
Element et = null;
for (int i = 0; i < node.size(); i++) {
et = (Element) node.get(i);
List subNode = et.getChildren();
Map map = new HashMap();
Element subEt = null;
for (int j = 0; j < subNode.size(); j++) {
subEt = (Element) subNode.get(j);
map.put(subEt.getName(), subEt.getText());
}
if (map.size() > 0)
result.add(map);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static Map parseForMap(String xmlDoc) throws Exception {
Map map = new HashMap();
try{
StringReader xmlString = new StringReader(xmlDoc);
InputSource source = new InputSource(xmlString);
SAXBuilder saxb = new SAXBuilder();
List result = null;
result = new ArrayList();
Document doc = saxb.build(source);
Element root = doc.getRootElement();
List node = root.getChildren();
Element et = null;
for (int i = 0; i < node.size(); i++) {
et = (Element) node.get(i);
if (et.getChildren() != null && et.getChildren().size() > 0){
List<Map> details= new ArrayList<Map>();
for (int j = 0 ; j < et.getChildren().size() ; j++){
Element dt = (Element)et.getChildren().get(j);
if (dt.getChildren() != null && dt.getChildren().size() >0){
Map<String ,Object> deMap = new HashMap<String,Object>();
for (int z = 0 ; z < dt.getChildren().size(); z++){
Element le = (Element)dt.getChildren().get(z);
deMap.put(le.getName(), le.getText());
}
details.add(deMap);
}
}
map.put("details", details);
}else{
map.put(et.getName(), et.getText());
}
}
}catch(Exception e){
logger.error("parseForMap===error===="+e);
throw new Exception(e);
}
return map;
}
2.金额转大写
package com.cmrh.atp.util;
import org.apache.commons.lang.StringUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
public class NumberUtil {
private final static String[] CN_NUMERALS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private final static String[] CN_NUM_UNIT = {"", "拾", "佰", "仟"};
private final static String[] CN_NUM_HIGHT_UNIT = {"", "万", "亿", "万亿"};
private final static String[] CN_NUM_DECIMAL_UNIT = {"", "角", "分"};
public static String toUppercase(Double num){
return toUppercase(String.valueOf(num));
}
public static String toUppercase(String num){
String[] strNumArray = num.split("\\.");
int strNumArrayLength = strNumArray.length;
if(strNumArrayLength > 2 || (strNumArrayLength == 2 && strNumArray[1].length() > 2)){
System.out.println("金额格式异常,不能有多个小数点,小数位最多只能两位!");
}
BigDecimal bigDecimal = new BigDecimal(strNumArray[0]);
BigInteger bigInteger = bigDecimal.toBigInteger();
char[] chars = bigInteger.toString().toCharArray();
int intLength = chars.length;
StringBuilder strBuilder = new StringBuilder();
for(int i=0; i<intLength; i++){
String curCnNum = CN_NUMERALS[charNumToInt(chars[i])];
int ui = ((intLength-1)-i)%4;
int hui = ((intLength-1)-i)/4;
strBuilder.append(curCnNum);
if(!isZero(curCnNum)){
strBuilder.append(CN_NUM_UNIT[ui]);
String heightUnit = CN_NUM_HIGHT_UNIT[hui];
if(ui == 0 || isZeroInResidue(chars, i, ui)){
strBuilder.append(heightUnit);
if(heightUnit.equals(CN_NUM_HIGHT_UNIT[1]) && !isZeroOfIndex(chars, (ui+1)+i)){
i = i+skipZero(chars, i);
}
}
}
}
clearZero(0, strBuilder);
strBuilder.append("圆");
if(strNumArrayLength == 1 || strNumArray[1].matches("^[0]*$")){
strBuilder.append("整");
return strBuilder.toString();
}
char[] decimalChars = strNumArray[1].toCharArray();
int decimalCharLength = decimalChars.length;
for(int i=1; i<=decimalCharLength; i++){
String cnNum = CN_NUMERALS[charNumToInt(decimalChars[i-1])];
if(!isZero(cnNum)){
strBuilder.append(cnNum).append(CN_NUM_DECIMAL_UNIT[i]);
}else if(i == decimalCharLength){
if(strBuilder.toString().startsWith("圆")){
return strBuilder.toString().substring(1);
}
return strBuilder.toString();
}else{
strBuilder.append(cnNum);
}
}
if(strBuilder.toString().startsWith("圆零")){
return strBuilder.toString().substring(2);
}
return strBuilder.toString();
}
private static void clearZero(int startIndex, StringBuilder strBuilder){
int strLength = strBuilder.length();
int zeroIndex = strBuilder.indexOf(CN_NUMERALS[0], startIndex);
if(zeroIndex <= -1){
return;
}
if(strLength <= zeroIndex+1){
strBuilder.deleteCharAt(zeroIndex);
return;
}
if(zeroIndex > -1 && isZero(strBuilder.charAt(zeroIndex+1))){
strBuilder.deleteCharAt(zeroIndex);
clearZero(startIndex, strBuilder);
return;
}
if(strLength > zeroIndex+1){
clearZero(zeroIndex+1, strBuilder);
return;
}
}
private static boolean isZero(String cnNum){
return cnNum.equals(CN_NUMERALS[0]);
}
private static boolean isZero(char cnNum){
return isZero(String.valueOf(cnNum));
}
private static int skipZero(char[] chars, int beginIndex){
int newIndex = 0;
int charLength = chars.length;
for(int i=beginIndex+1; i<charLength; i++){
if(isZero(CN_NUMERALS[charNumToInt(chars[i])])){
newIndex++;
}else{
break;
}
}
return newIndex;
}
private static boolean isZeroOfIndex(char[] chars, int index){
int numInex = Integer.parseInt(String.valueOf(chars[index]), 10);
String cnNum = CN_NUMERALS[numInex];
return isZero(cnNum);
}
private static boolean isZeroInResidue(char[] chars, int begin, int end){
for(int i=1; i<=end; i++){
boolean r = isZeroOfIndex(chars, begin+i);
if(!r){
return false;
}
}
return true;
}
private static int charNumToInt(char charNum){
String ns = String.valueOf(charNum);
return Integer.parseInt(ns, 10);
}
public static String addOne(String str){
String[] strs = str.split("[^0-9]");
String numStr = strs[strs.length-1];
if(numStr != null && numStr.length()>0){
int n = numStr.length();
int num = Integer.parseInt(numStr)+1;
String added = String.valueOf(num);
n = Math.min(n, added.length());
return str.subSequence(0, str.length()-n)+added;
}else{
throw new NumberFormatException();
}
}
}
标题