永发信息网

怎么通过java代码向xml文件中添加注释

答案:2  悬赏:20  手机版
解决时间 2021-01-30 14:38
  • 提问者网友:藍了天白赴美
  • 2021-01-30 05:17
怎么通过java代码向xml文件中添加注释
最佳答案
  • 五星知识达人网友:撞了怀
  • 2021-01-30 05:37
package com.tuobao.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class testDom4j {

private static final String path = "f:" + java.io.File.separator
+ "eee.xml";


public static void main(String[] args) {
// System.out.println(createXMLFile(path));
// System.out.println(modiXMLFile(path, path));
System.out.println(formatXMLFile(path));
}


public static int createXMLFile(String filename) {

int returnValue = 0;

Document document = DocumentHelper.createDocument();

Element booksElement = document.addElement("books");

booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");

Element bookElement = booksElement.addElement("book");

bookElement.addAttribute("show", "yes");

Element titleElement = bookElement.addElement("title");

titleElement.setText("Dom4j Tutorials");


bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "no");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene in Action");


Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");

try {

XMLWriter writer = new XMLWriter(new FileWriter(new java.io.File(
filename)));
writer.write(document);
writer.flush();
writer.close();

returnValue = 1;
} catch (Exception ex) {
returnValue = 0;
ex.printStackTrace();
}
return returnValue;
}


public static int modiXMLFile(String filename, String newfilename) {
int returnValue = 0;
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new java.io.File(filename));


List list = document.selectNodes("/books/book/@show");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("yes")) {
attribute.setValue("no");
}
}


list = document.selectNodes("/books/owner");
iter = list.iterator();
if (iter.hasNext()) {
Element ownerElement = (Element) iter.next();
ownerElement.setText("测试修改");
Element dateElement = ownerElement.addElement("date");
dateElement.setText("2008-09-11");
dateElement.addAttribute("type", "日期");
}


list = document.selectNodes("/books/book");
iter = list.iterator();
while (iter.hasNext()) {
Element bookElement = (Element) iter.next();
Iterator iterator = bookElement.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals("Dom4j Tutorials")) {
bookElement.remove(titleElement);
}
}
}

try {

OutputFormat format = OutputFormat.createPrettyPrint();

// format.setEncoding("GBK");

// XMLWriter writer = new XMLWriter(new FileWriter(new
// File(newfilename)),format);
// 保证编码为UTF-8,支持中文写入
XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
newfilename)), format);
writer.write(document);
writer.flush();
writer.close();

returnValue = 1;
} catch (Exception ex) {
returnValue = 0;
ex.printStackTrace();
}

} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}


public static int formatXMLFile(String filename) {
int returnValue = 0;
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
XMLWriter writer = null;

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("utf-8");
writer = new XMLWriter(new FileWriter(new File(filename)), format);
writer.write(document);
writer.flush();
writer.close();

returnValue = 1;
} catch (Exception ex) {
returnValue = 0;
ex.printStackTrace();
}
return returnValue;
}
}
这个希望对你有用。。
全部回答
  • 1楼网友:洒脱疯子
  • 2021-01-30 07:00
使用 文件流 找到 <-- 起始位置 -->为终止位置,中间就是注释 或者把xml转为string然后用正则匹配把注释找出来
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯