帮女朋友写的一段代码特此留念

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.HashSet;
import java.util.Set;


import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;


public class FileCompareApplication {


private static String englishurl = "";
private static String otherlanguageUrl = "";
private static String writerToFile = "";
private static String  excelOutput="";


private static String excelFirstPath = "";
private static String excelSecondPath = "";
private static String outputPath = "";

private static String excelFilePath = "";
private static String excelDirPath = "";
private static String excelOutputPath = "";

private static String originalexcelFilePath = "";
private static String filterexcelDirPath = "";



private static String getFilePath(JFileChooser choose) {
String fileUrl = null;
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choose.setCurrentDirectory(new File("."));


// 判断是否点击打开按钮
if (choose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fileUrl = choose.getSelectedFile().getAbsolutePath();// 打开文件的绝对路径
}
return fileUrl;
}


private static String getExcelPath(JFileChooser choose) {
String fileUrl = null;
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
choose.setCurrentDirectory(new File("."));


// 判断是否点击打开按钮
if (choose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fileUrl = choose.getSelectedFile().getAbsolutePath();// 打开文件的绝对路径
}
return fileUrl;
}
/**

* @param args
*/
public static void main(String[] args) {


// 创建窗体对象
Frame f = new Frame("添加按钮");
// 设置属性
f.setBounds(800, 400, 800, 600);
// 设置布局为流式布局
f.setLayout(new GridLayout(18, 2, 10, 10));


JButton jFileButton = new JButton("选择英文文件夹");
JButton jFileButton1 = new JButton("选择外文文件夹");
JButton jFileButton2 = new JButton("key-value输出的文件夹");
JButton jFileButton6 = new JButton("excel输出的文件夹");
JButton jFileButton3 = new JButton("第一个excel文件");
JButton jFileButton4 = new JButton("第二个excel文件");
JButton jFileButton5 = new JButton("excel文件生成文件夹");
JButton jFileButton7 = new JButton("公司已经有的excel标准文件");
JButton jFileButton8 = new JButton("excel文件夹其中只能有excel文件");
JButton jFileButton9 = new JButton("生成公司标准文件没有的单词");
JButton jFileButton10 = new JButton("原始excel文件");
JButton jFileButton11 = new JButton("过滤后的excel文件");
final JTextField txtfile = new JTextField("文件夹路径");
final JTextField txtfile1 = new JTextField("文件夹路径");
final JTextField txtfile2 = new JTextField("文件夹路径");
final JTextField txtfile6 = new JTextField("excel生成文件夹路径");
final JTextField txtfile3 = new JTextField("excel文件路径");
final JTextField txtfile4 = new JTextField("excel文件路径");
final JTextField txtfile5 = new JTextField("excel生成文件夹路径");
final JTextField txtfile7 = new JTextField("公司标准文件的路径");
final JTextField txtfile8 = new JTextField("excel的文件夹路径");
final JTextField txtfile9 = new JTextField("excel的文件夹路径");
final JTextField txtfile10 = new JTextField("原始excel的文件路径");
final JTextField txtfile11 = new JTextField("过滤excel的文件路径");

f.add(txtfile);
f.add(jFileButton);
f.add(txtfile1);
f.add(jFileButton1);
f.add(txtfile2);
f.add(jFileButton2);
f.add(txtfile6);
f.add(jFileButton6);
jFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser englishDir = new JFileChooser();
englishurl = getFilePath(englishDir);
txtfile.setText(englishurl);
System.out.println(englishurl);
}
});
jFileButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser otherLanguageDir = new JFileChooser();
otherlanguageUrl = getFilePath(otherLanguageDir);
txtfile1.setText(otherlanguageUrl);
System.out.println(otherlanguageUrl);
}
});
jFileButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
writerToFile = getFilePath(outputDir);
txtfile2.setText(writerToFile);
System.out.println(writerToFile);
}
});
jFileButton6.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
excelOutput = getFilePath(outputDir);
txtfile2.setText(excelOutput);
System.out.println(excelOutput);
}
});
// 创建按钮对象
Button bu = new Button("文件比较与生成");
Button bu1 = new Button("");
// 把按钮添加到窗体
f.add(bu);
// 把按钮添加到窗体
f.add(bu1);


// 设置窗体可以关闭
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);// 退出JVM
}
});


bu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CompareFiles.compareFile(englishurl, otherlanguageUrl,
writerToFile, excelOutput);
}
});


f.add(txtfile3);
f.add(jFileButton3);
f.add(txtfile4);
f.add(jFileButton4);
f.add(txtfile5);
f.add(jFileButton5);


jFileButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser otherLanguageDir = new JFileChooser();
excelFirstPath = getExcelPath(otherLanguageDir);
txtfile3.setText(excelFirstPath);
System.out.println(excelFirstPath);
}
});
jFileButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
excelSecondPath = getExcelPath(outputDir);
txtfile4.setText(excelSecondPath);
System.out.println(excelSecondPath);
}
});
jFileButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
outputPath = getFilePath(outputDir);
txtfile4.setText(outputPath);
System.out.println(outputPath);
}
});

Button exchangeButton = new Button("二个excel文件比较去重生成新的excel");
// 把按钮添加到窗体
f.add(exchangeButton);

exchangeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Set<String> first=ExcelUtils.readExcel(excelFirstPath);
Set<String> second=ExcelUtils.readExcel(excelSecondPath);
first.addAll(second);
ExcelUtils.createFixationSheet(first, outputPath+File.separator+ System.currentTimeMillis()+".xls");
}
});

Button exchangeButton1 = new Button("");
f.add(exchangeButton1);

f.add(txtfile7);
f.add(jFileButton7);
f.add(txtfile8);
f.add(jFileButton8);
f.add(txtfile9);
f.add(jFileButton9);



jFileButton7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser otherLanguageDir = new JFileChooser();
excelFilePath = getExcelPath(otherLanguageDir);
txtfile7.setText(excelFilePath);
System.out.println(excelFilePath);
}
});
jFileButton8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
excelDirPath = getExcelPath(outputDir);
txtfile8.setText(excelDirPath);
System.out.println(excelDirPath);
}
});
jFileButton9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
excelOutputPath = getFilePath(outputDir);
txtfile9.setText(excelOutputPath);
System.out.println(excelOutputPath);
}
});

Button exchangeButton2 = new Button("将公司标准与外语言取差集和交输出到excel文件中");
f.add(exchangeButton2);
exchangeButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CompareFiles.intersectTwoExcel(excelFilePath, excelDirPath, excelOutputPath);

}
});
Button exchangeButton3 = new Button("");
f.add(exchangeButton3);

f.add(txtfile10);
f.add(jFileButton10);
f.add(txtfile11);
f.add(jFileButton11);

jFileButton10.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser otherLanguageDir = new JFileChooser();
originalexcelFilePath = getExcelPath(otherLanguageDir);
txtfile10.setText(originalexcelFilePath);
System.out.println(originalexcelFilePath);
}
});
jFileButton11.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
JFileChooser outputDir = new JFileChooser();
filterexcelDirPath = getFilePath(outputDir);
txtfile11.setText(filterexcelDirPath);
System.out.println(filterexcelDirPath);
}
});

Button exchangeButton4 = new Button("文件过滤");
f.add(exchangeButton4);

exchangeButton4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Set<String> values=ExcelUtils.readExcelFilterBlank(new File(originalexcelFilePath));
ExcelUtils.createFixationSheet(values, filterexcelDirPath+File.separator+"过滤"+System.currentTimeMillis()+".xls");
}
});
// 窗体显示
f.setVisible(true);


}

}


import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;


public class CompareFiles {


static Set<String> res = new HashSet<String>();


/**
* 比较指定的7个文件

*/
private static final String[] fileNames = new String[] { "cec_strings.xml",
"menu_strings.xml", "mmp_strings.xml", "nav_strings.xml",
"strings.xml", "thr_menu_strings.xml", "timeshift_strings.xml" };


/**

* @param englishPath
*            英文目录的地址
* @param otherLanguagePath
*            其它语言的地址
* @return 结果保存在指定的文件文件中
*/
public static void compareFile(String englishPath,
String otherLanguagePath, String output, String translateOutput) {
File[] englishFiles = FileUtils.getFiles(englishPath);
File[] otherLanguageFiles = FileUtils.getFiles(otherLanguagePath);
for (String fileName : fileNames) {
File englishFile = FileUtils.getFileByFileName(englishFiles,
fileName);
File otherLanguageFile = FileUtils.getFileByFileName(
otherLanguageFiles, fileName);
Map<String, String> english = XMLParser.parseXml(englishFile);
Map<String, String> otherLanguage = XMLParser
.parseXml(otherLanguageFile);
Set<String> englishKey = english.keySet();
Set<String> otherLanguageKey = otherLanguage.keySet();
// 过滤出englishKey特有的key
SetView<String> differenec = Sets.difference(englishKey,
otherLanguageKey);
// 过滤出englishKey和otherLanguageKey的交集
SetView<String> union = Sets.intersection(englishKey,
otherLanguageKey);
Map<String, String> englishRes = setViewCovertToHashMap(differenec,
english);
Map<String, String> otherLanguageRes = getDiffValueBySameKey(union,
english, otherLanguage);
Set<String> englishResValue = getValueByKey(englishRes);
Set<String> otherLanguageResValue = getValueByKey(otherLanguageRes);
res.addAll(englishResValue);
res.addAll(otherLanguageResValue);


FileUtils.writerHashMapToFile(englishRes, output + File.separator
+ fileName.substring(0, fileName.lastIndexOf(".xml")));
FileUtils.writerHashMapToFile(
otherLanguageRes,
output
+ File.separator
+ fileName.substring(0,
fileName.lastIndexOf(".xml")));
}
ExcelUtils.createFixationSheet(res, translateOutput + File.separator
+ System.currentTimeMillis() + ".xls");
res.clear();
}


public static void printViewSet(SetView<String> views) {
Iterator iterator = views.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}


/**
* 将map中的value转换成集合

* @param originalMap
* @return
*/
public static Set<String> getValueByKey(Map<String, String> originalMap) {
Set<String> values = new HashSet<String>();
Iterator<String> iterator = originalMap.keySet().iterator();
while (iterator.hasNext()) {
String value = iterator.next();
values.add(originalMap.get(value));
}
return values;
}


/**
* 如果key相同但是value不同,则构成新的集合

* @param views
* @param english
* @param otherLanguage
* @return
*/
public static Map<String, String> getDiffValueBySameKey(
SetView<String> views, Map<String, String> english,
Map<String, String> otherLanguage) {
Map<String, String> res = new HashMap<String, String>();
Iterator<String> iterator = views.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
if (english.get(value).equals(otherLanguage.get(value))) {
res.put(value, english.get(value));
}
}
return res;
}


/**
* 将原始map中的特有的key过滤到新的集合中

* @param views
*            过滤出来的集合
* @param originalMap
*            原始map集合
* @return
*/
public static Map<String, String> setViewCovertToHashMap(
SetView<String> views, Map<String, String> originalMap) {


Map<String, String> res = new HashMap<String, String>();
Iterator<String> iterator = views.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
res.put(value, originalMap.get(value));
}
return res;
}


/**
* 比较公司标准与外国语言的差集

* @param excelFirstPath
* @param excelSecondPath
* @param excelOutputPath
*/
public static void intersectTwoExcel(String excelFilePath,
String excelDirPath, String excelOutputPath) {
Set<String> otherLanguageValues = new HashSet<String>();
Set<String> englishComany = new HashSet<String>();
englishComany.addAll(ExcelUtils.readExcel(excelFilePath));
otherLanguageValues.addAll(ExcelUtils.readExcel(excelDirPath));
SetView<String> differenec = Sets.difference(otherLanguageValues,
englishComany);
SetView<String> union = Sets.intersection(englishComany,
otherLanguageValues);
ExcelUtils.createFixationSheet(differenec, excelOutputPath + File.separator
+ "差集"+System.currentTimeMillis() + ".xls");
ExcelUtils.createFixationSheet(union, excelOutputPath + File.separator
+ "交集"+System.currentTimeMillis() + ".xls");
}


}


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;


/**
 * Excel工具类
 * 
 * @author yy
 * 
 */
public class ExcelUtils {


/**

* @param res
*            内存中的数据
* @param path
*            要输出的excel文件地址
*/
public static void createFixationSheet(Set<String> res, String path) {


File outputfile = new File(path);
if (!outputfile.exists()) {
try {
outputfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream os = null;
try {
os = new FileOutputStream(outputfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<String> values = new ArrayList<String>();
values.addAll(res);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
sheet.createFreezePane(0, 1);
try {
HSSFRow row = null;
for (int i = 0; i < values.size(); i++) {
row = sheet.createRow((short) i);
cteateCell(wb, row, (short) 0, values.get(i));
}


} catch (Exception e) {
e.printStackTrace();
}
try {
wb.write(os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.flush();
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


/**
* 创建excel单元格

* @param wb
* @param row
* @param col
* @param val
*/
private static void cteateCell(HSSFWorkbook wb, HSSFRow row, short col,
String val) {
HSSFCell cell = row.createCell(col);
cell.setCellValue(val);
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
cell.setCellStyle(cellstyle);
}


public static Set<String> readExcel(File file) {


Set<String> res = new HashSet<String>();
if (file == null) {
return res;
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = null;
sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
for (int i = 0; i <= rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row.getCell((short) 0) != null) {
row.getCell((short) 0).setCellType(Cell.CELL_TYPE_STRING);
res.add(row.getCell((short) 0).getStringCellValue());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}


/**
* 过滤第一列和第二列都不为null,只要第一列
* @param file
* @return
*/
public static Set<String> readExcelFilterBlank(File file) {


Set<String> res = new HashSet<String>();
if (file == null) {
return res;
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = null;
sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
for (int i = 0; i <= rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row.getCell((short) 0) != null
&& row.getCell((short) 1) != null) {
row.getCell((short) 0).setCellType(Cell.CELL_TYPE_STRING);
res.add(row.getCell((short) 0).getStringCellValue());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}


public static Set<String> readExcel(String path) {


if (StringUtils.isBlank(path)) {
return new HashSet<String>();
}
File file = new File(path);
return readExcel(file);
}


public static void main(String[] args) {
Set<String> bb = new HashSet<String>();
bb = readExcelFilterBlank(new File("C://Users/yy/Desktop/res/aa.xls"));
for (String a : bb) {
System.out.println(a);
}
}


}



import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.util.Map;  
import java.util.Set;  
  
import org.apache.commons.lang.StringUtils;  
  
/** 
 *  
 * 文件工具类 
 */  
public class FileUtils {  
  
    /** 
     * 获取指定路径下的所有文件 
     *  
     * @param path 
     * @return 
     */  
    public static File[] getFiles(String path) {  
        File[] res = null;  
        if (StringUtils.isNotBlank(path)) {  
            File dictory = new File(path);  
            if (dictory.isDirectory()) {  
                res = dictory.listFiles();  
            }  
        }  
        return res;  
    }  
  
    /** 
     * 根据文件名获取指定的文件 
     * @param files 文件集合 
     * @param fileName 文件名 
     * @return 
     */  
    public static File getFileByFileName(File[] files, String fileName) {  
        if (files != null && StringUtils.isNotBlank(fileName)) {  
            for (File file : files) {  
                if (file.getName().equals(fileName)) {  
                    return file;  
                }  
            }  
        }  
        return null;  
    }  
      
      
    /** 
     * 将set中的值输出到指定的文件中 
     * @param values 
     * @param path 
     */  
    public static void writerSetToFile(Set<String> values, String path) {  
        File writeToFile=new File(path);  
        if(!writeToFile.exists()){  
            try {  
                writeToFile.createNewFile();  
            } catch (IOException e) {  
            }  
        }  
        FileWriter fw = null;  
        BufferedWriter bw = null;  
        try {  
            fw = new FileWriter(path);  
            bw = new BufferedWriter(fw);  
            for (String value : values) {  
                bw.write(value);  
                bw.newLine();  
            }    
        } catch (IOException e) {  
        } finally {  
            try {  
                if (bw != null) {  
                    bw.close();  
                }  
            } catch (IOException e) {  
            }  
            try {  
                if (fw != null) {  
                    fw.close();  
                }  
            } catch (IOException e) {  
            }  
  
        }  
    }  
  
    /** 
     * 将map中的值输出到指定的文件中 
     * @param map 
     * @param path 
     */  
    public static void writerHashMapToFile(Map<String, String> map, String path) {  
        File writeToFile=new File(path);  
        if(!writeToFile.exists()){  
            try {  
                writeToFile.createNewFile();  
            } catch (IOException e) {  
            }  
        }  
        FileWriter fw = null;  
        BufferedWriter bw = null;  
        try {  
            fw = new FileWriter(path,true);  
            bw = new BufferedWriter(fw);  
            Set<String> keys = map.keySet();  
            for (String key : keys) {  
                bw.write(key + "=" + map.get(key));  
                bw.newLine();  
            }  
            bw.write("----------------------------------");  
            bw.newLine();  
            bw.newLine();  
            bw.newLine();  
            bw.newLine();  
        } catch (IOException e) {  
        } finally {  
            try {  
                if (bw != null) {  
                    bw.close();  
                }  
            } catch (IOException e) {  
            }  
            try {  
                if (fw != null) {  
                    fw.close();  
                }  
            } catch (IOException e) {  
            }  
  
        }  
    }  
  
}  



import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * 
 * 解析xml文件例如 <?xml version="1.0" encoding="utf-8"?> <resources> <string
 * name="app_name">Live TV</string> <!-- For virtualkeypad --> <string
 * name="keypad_power" translatable="false">POWER</string> <string
 * name="keypad_mute" translatable="false">MUTE</string> <string
 * name="keypad_chlist" translatable="false">CH LIST</string> <string
 * name="keypad_menu" translatable="false">MENU</string> <string
 * name="keypad_source" translatable="false">SOURCE</string> <string
 * name="keypad_info" translatable="false">INFO</string> <string
 * name="keypad_epg" translatable="false">EPG</string> <string name="keypad_fav"
 * translatable="false">FAV</string> <string name="keypad_zoom"
 * translatable="false">ZOOM</string> <string
 * name="source_on_talkback_up_down">Please use UP/DOWN key to change source
 * when TalkBack On </string> </resources>
 */
public class XMLParser {
/**
* 将指定的xml文件转换成Map key=app_name value=Live TV

* @param path
* @return
*/
public static Map<String, String> parseXml(String path) {
File file = new File(path);
return parseXml(file);
}


/**
* 将指定的xml文件转换成Map key=app_name value=Live TV

* @param path
* @return
*/
public static Map<String, String> parseXml(File file) {
Map<String, String> attributes = new HashMap<String, String>();
if (file == null) {
return attributes;
}
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(file);
} catch (DocumentException e) {
}
Element root = document.getRootElement();
// 遍历
listNodes(root, attributes);
return attributes;
}


/**
* 递归遍历xml的节点,并解析成key-value的内存结构

* @param node
*            当前节点
* @param attributes
*            内存中数据结构
*/
private static void listNodes(Element node, Map<String, String> attributes) {
// 首先获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attribute : list) {
if ("string".equals(node.getName())) {
attributes.put(attribute.getValue(), node.getText());
}
}
// 同时迭代当前节点下面的所有子节点
// 使用递归
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e, attributes);
}
}


/**
* 循环遍历map,用于测试xml解析是否正确

* @param attributes
*/
public static void printMap(Map<String, String> attributes) {
Set<String> keys = attributes.keySet();
for (String key : keys) {
System.out.println(key + ":" + attributes.get(key));
}
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值