DOM4J怎么修改jar包里的xml文件

DOM4J怎么修改jar包里的xml文件,第1张

static Properties readJarProperty() throws IOException {

currentJarPath = URLDecoder.decode(com.taisys.ota.util.RunMain.class

.getProtectionDomain().getCodeSource().getLocation().getFile(),

"UTF-8")// 获取当前Jar文件名,并对其解码,防止出现中文乱码

System.out.println("currentJarPath=" + currentJarPath)

JarFile currentJar = new JarFile(currentJarPath)

JarEntry dbEntry = currentJar

.getJarEntry("com/taisys/ota/util/jdbc.properties")

InputStream in = currentJar.getInputStream(dbEntry)

Properties p = new Properties()

p.load(in)

in.close()

return p

}

static void updateJarProperty() throws IOException {

currentJarPath = URLDecoder.decode(com.taisys.ota.util.RunMain.class

.getProtectionDomain().getCodeSource().getLocation().getFile(),

"UTF-8")// 获取当前Jar文件名,并对其解码,防止出现中文乱码

String EntryName = "jdbc.properties"

currentPath = startServer()

byte[] data = null

File properFile = new File(currentPath + "/jdbc.properties")

if (properFile.exists()) {

InputStream in = new FileInputStream(properFile)

data = getByte(in)

}

try {

JarFile jf = new JarFile(currentJarPath)

TreeMap<String, byte[]>tm = new TreeMap<String, byte[]>()

Enumeration es = jf.entries()

while (es.hasMoreElements()) {

JarEntry je = (JarEntry) es.nextElement()

byte[] b = getByte(jf.getInputStream(je))

tm.put(je.getName(), b)

}

JarOutputStream out = new JarOutputStream(new FileOutputStream(

currentJarPath))

Set set = tm.entrySet()

Iterator it = set.iterator()

boolean has = false

while (it.hasNext()) {

Map.Entry me = (Map.Entry) it.next()

String name = (String) me.getKey()

JarEntry jeNew = new JarEntry(name)

// System.out.println("jeNew.getName()="+jeNew.getName())

out.putNextEntry(jeNew)

byte[] b

if (name.equals(EntryName)) {

b = data

has = true

} else

b = (byte[]) me.getValue()

out.write(b, 0, b.length)

}

if (!has) {

JarEntry jeNew = new JarEntry(EntryName)

out.putNextEntry(jeNew)

out.write(data, 0, data.length)

}

out.flush()

out.finish()

out.close()

} catch (Exception e) {

e.printStackTrace()

}

}

可以参考 :

package com.zuxia.dom4j

import java.io.File

import java.io.FileOutputStream

import java.util.Iterator

import java.util.List

import java.util.Scanner

import org.dom4j.Document

import org.dom4j.Element

import org.dom4j.io.OutputFormat

import org.dom4j.io.SAXReader

import org.dom4j.io.XMLWriter

/**

*

* 使用dom4j解析xml

*

* 1. 创建解析器

*

* 2. 创建文档对象Document

*

* 3. 获取根节点

*

*/

public class Dom4jParseXML {

public static void main(String[] args) {

//1. 创建解析器

SAXReader saxreader = new SAXReader()

Document doc = null

try {

//2. 创建文档对象Document

doc = saxreader.read(new File("src/studentinfo.xml"))

} catch (Exception e) {

System.out.println("读取xml文件异常!")

}

//3. 获取根节点

Element root = doc.getRootElement()

//4. 获取元素

Iterator<Element>iter = root.elementIterator()

while(iter.hasNext()){

Element student = iter.next()

System.out.println("学号:"+student.attributeValue("stuno")+"\t姓名:"+student.elementText("name"))

}

//提示用户添加新的数据

Scanner sc = new Scanner(System.in)

System.out.println("请输入学号:")

String stuno = sc.nextLine()

System.out.println("请输入姓名:")

String name = sc.nextLine()

System.out.println("请输入年龄:")

String age = sc.nextLine()

//将数据添加在Document中

Element student = root.addElement("student")

student.addAttribute("stuno", stuno)

student.addElement("name").addText(name)

student.addElement("age").addText(age)

//3. 设置格式

OutputFormat format = OutputFormat.createCompactFormat()

format.setIndentSize(4)

format.setNewlines(true)

try {

//4. 保存xml文件

XMLWriter out = new XMLWriter(new FileOutputStream("src/studentinfo.xml"),format)

out.write(doc)

System.out.println("ok!!!")

} catch (Exception e) {

System.out.println("失败!")

}

System.out.println("完成了!")

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8043170.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存