Android-使用Dom对XML进行增删改查 更新 3
- UID
- 1066743
|
Android-使用Dom对XML进行增删改查 更新 3
6. 保存
在内存中修改好的document对象,直接保存为新的xml文件,代码如下:
/**
* 保存修改后的Doc
*
* @param doc doc
* @param saveXmlFilePath 路径
* @return 是否成功
*/
public static boolean saveXmlWithDom(Document doc,String saveXmlFilePath) {
if (doc==null || saveXmlFilePath==null || saveXmlFilePath.isEmpty())
return false;
try {
//将内存中的Dom保存到文件
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//设置输出的xml的格式,utf-8
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("version",doc.getXmlVersion());
DOMSource source = new DOMSource(doc);
//打开输出流
File file = new File(saveXmlFilePath);
if (!file.exists())
Log.i("XmlUtils","saveXmlWithDom,createNewFile:"+file.createNewFile());
OutputStream outputStream = new FileOutputStream(file);
//xml的存放位置
StreamResult src = new StreamResult(outputStream);
transformer.transform(source, src);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
7. 附上工具类
/**
* <pre>
* author: Chestnut
* blog :
* time : 2018/1/10 17:14
* desc : XML解析工具类
* thanks To:
* 1. [Android解析XML的三种方式]
* 2. [Android几种解析XML方式的比较]
* 3. [android xml 解析 修改]
* 4. [android 对xml文件的pull解析,生成xml ,对xml文件的增删]
* dependent on:
* update log:
* </pre>
*/
public class XmlUtils {
/**
* DOM解析
* 把文档中的所有元素,按照其出现的层次关系,解析成一个个Node对象(节点)。
* 缺点是消耗大量的内存。
* @param xmlFilePath 文件
* @return Document
*/
public static Document loadWithDom(String xmlFilePath) {
try {
File file = new File(xmlFilePath);
if (!file.exists()) {
throw new RuntimeException("not find file:" + xmlFilePath);
}
else {
InputStream inputStream = new FileInputStream(file);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
} catch (ParserConfigurationException | IOException | SAXException e) {
return null;
}
}
public static Observable<Document> loadWithDomRx(String xmlFilePath) {
return Observable.just(loadWithDom(xmlFilePath));
}
/**
* 保存修改后的Doc
*
* @param doc doc
* @param saveXmlFilePath 路径
* @return 是否成功
*/
public static boolean saveXmlWithDom(Document doc,String saveXmlFilePath) {
if (doc==null || saveXmlFilePath==null || saveXmlFilePath.isEmpty())
return false;
try {
//将内存中的Dom保存到文件
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//设置输出的xml的格式,utf-8
transformer.setOutputProperty("encoding", "utf-8");
transformer.setOutputProperty("version",doc.getXmlVersion());
DOMSource source = new DOMSource(doc);
//打开输出流
File file = new File(saveXmlFilePath);
if (!file.exists())
Log.i("XmlUtils","saveXmlWithDom,createNewFile:"+file.createNewFile());
OutputStream outputStream = new FileOutputStream(file);
//xml的存放位置
StreamResult src = new StreamResult(outputStream);
transformer.transform(source, src);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static Observable<Boolean> saveXmlWithDomRx(Document doc,String saveXmlFilePath) {
return Observable.just(saveXmlWithDom(doc, saveXmlFilePath));
}
} |
|
|
|
|
|