Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
R
RemoveUnusedImg
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengjunjing
RemoveUnusedImg
Commits
dd9cf2dd
Commit
dd9cf2dd
authored
Mar 14, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:增加删除string节点的功能
parent
bc90d867
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
5 deletions
+46
-5
StringElement.kt
.../java/com/laihua/projecthelper/stringxml/StringElement.kt
+4
-2
StringXmlParse.kt
...java/com/laihua/projecthelper/stringxml/StringXmlParse.kt
+42
-3
No files found.
src/main/java/com/laihua/projecthelper/stringxml/StringElement.kt
View file @
dd9cf2dd
...
...
@@ -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
,
)
{
...
...
src/main/java/com/laihua/projecthelper/stringxml/StringXmlParse.kt
View file @
dd9cf2dd
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment