JAVA 修改XML文件
package com.cnten.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class XmlUtil {
/**
* @param args
*/
public static void main(String[] args) {
List<String> columns = new ArrayList<String>();//锟斤拷锟斤拷锟接的节碉拷
columns.add("Arden");
columns.add("Pinco");
columns.add("Denis");
// new XmlUtil().testAddElements(columns);//锟斤拷锟斤拷锟斤拷锟接凤拷锟斤拷
new XmlUtil().testUupdateElements();//锟斤拷锟皆革拷锟铰凤拷锟斤拷
}
/**
* 修改
*/
@Test
public void testUupdateElements() {
try {
String path1 = "../CntenSecurity/res/values/theme.xml";///CntenSecurity/res/values/theme.xml
Document baseXML1 = getDocument(path1);
updateElementByName(baseXML1, "item", "android:textSize", "33sp");
writeXml(baseXML1, path1);
/*String path2 = "./src/com/cnten/test/test.xml";
Document baseXML2 = getDocument(path2);
updateElementByName(baseXML2, "report_content", "start_row", "2013-04-06");
writeXml(baseXML2, path2); */
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param document
* @param elementName
* @param attributeName
* @param newValue
*/
@SuppressWarnings("deprecation")
private void updateElementByName(Document document, String elementName, String attributeName, String newValue){
Element root = document.getRootElement();
System.err.println(root.element("style").elements(elementName).size());
// Element updatedNode1 = root.element("blackTheme");
// Element updatedNode = (Element) root.element("style").elements(elementName).get(1); //parent element.
// updatedNode.setText("aaa");
// updatedNode.setAttributeValue(attributeName, newValue);
Iterator it = root.element("style").elementIterator(elementName);
while (it.hasNext()) {
Element request = (Element)it.next();
if(request.attributeValue("name").equals(attributeName)){
request.setText(newValue);
}
}
}
/**
*
* @param document
* @param columns
*/
private void addElement(Document document, List<String> columns){
Element root = document.getRootElement();
Element updatedNode = root.element("report_content"); //parent element.
for (int i = 0; i < columns.size(); i++) {
String column = columns.get(i);
Element newElement = updatedNode.addElement("property");//child element.
newElement.addAttribute("row_name", column);//the first property.
newElement.addAttribute("row_id", String.valueOf(i));//the second property.
}
}
/**
*
* @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<String> 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);
}
}