Commit dd9cf2dd authored by pengjunjing's avatar pengjunjing

feat:增加删除string节点的功能

parent bc90d867
......@@ -7,15 +7,17 @@ import java.io.File
* Author: pengjunjing
* Date: 2022/3/11
* Description:
* 以下方xml代码为例
* <string name="file_name">文件名称</string>
*/
data class StringElement(
// 文件
val file: File,
// 所属模块,指的是代码所属的模块
val belongModule: String,
// 字符串节点名字
// 字符串节点的值:file_name
val attributeName: String,
// 值
// 值:文件名称
val value: String,
) {
......
package com.laihua.projecthelper.stringxml
import com.laihua.projecthelper.implement.belongModule
import org.jdom2.Document
import org.jdom2.Element
import org.jdom2.input.SAXBuilder
import org.jdom2.output.Format
import org.jdom2.output.XMLOutputter
import java.io.File
import java.io.FileWriter
/**
* Author: pengjunjing
......@@ -14,6 +20,7 @@ class StringXmlParse {
companion object {
const val resourcesStringDef = "resources"
const val nameStringDef = "name"
}
......@@ -32,7 +39,12 @@ class StringXmlParse {
if (child.name == "string") {
//获取string节点的数据
val stringElement =
StringElement(stringXmlFile, belongModule, child.attributes[0].value, child.value)
StringElement(
stringXmlFile,
belongModule,
child.attributes[0].value,
child.value
)
stringXmlResCode.stringElementList.add(stringElement)
}
}
......@@ -47,8 +59,35 @@ class StringXmlParse {
fun deleteStringXmlElement(stringElement: StringElement) {
val document = SAXBuilder().build(stringElement.file)
val rootElement = document.rootElement
rootElement.removeChild(stringElement.attributeName)
//todo 删除工作
//找到与name=xxx相同的值
val find: Element? = rootElement.children.find {
it.getAttribute(nameStringDef).value == stringElement.attributeName
}
find?.let {
rootElement.removeContent(it)
}
saveXML(stringElement.file, document)
}
private fun saveXML(file: File, doc: Document) {
// 将doc对象输出到文件
try {
// 创建xml文件输出流
val xmlOutPutter = XMLOutputter()
// 创建文件输出流
val writer = FileWriter(file)
// 指定文档格式
val fm: Format = Format.getPrettyFormat()
xmlOutPutter.format = fm
// 将doc写入到指定的文件中
xmlOutPutter.output(doc, writer)
writer.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment