JAVA 修改XML文件 (二)

2014-11-24 11:45:02 · 作者: · 浏览: 24


/**
*
* @param document
* @param filePath
* @throws IOException
*/
private void writeXml(Document document, String filePath) throws IOException{
File file = new File(filePath);
XMLWriter writer = null;
try {
if(file.exists())
{
file.delete();
}
writer = new XMLWriter(new FileOutputStream(file));
writer.write(document);
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
{
writer.close();
}
}
}



/**
*
* @param columns
*/
private void testAddElements(List columns) {
try {
Document baseXML = getDocument("./src/com/cnten/test/theme.xml");//原XML锟侥硷拷.

addElement(baseXML, columns);

writeXml(baseXML, "./src/com/cnten/test/theme.xml");//锟斤拷锟铰猴拷锟 ML锟侥硷拷
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
*
* @param filePath
* @return
* @throws DocumentException
*/
private Document getDocument(String filePath) throws DocumentException{
File file = new File(filePath);
SAXReader reader = new SAXReader();

return reader.read(file);
}


}
\