java操作xml类_Java操作XML的一个类[原创]

2005.04.17发表于blog.csnd.net/zxub

这两天没什么事,又开始摆弄Java了,想写个邮件发送的东东,想到要保存什么参数,怎么保存呢?突然想到XML文件,好,就用这个。

研究了下,感觉用dom4j好,ok,就是它了,下面把代码贴出来(修改版),随便写写,还有不足,仅供参考:

/*

* Created on 2005-4-14 15:26:04

* Modify no 2005-4-19 16:06:12

*/

/**

* @author zxub

*

*/

import java.io.File;

import java.io.FileWriter;

import java.net.MalformedURLException;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

public class OperaXML {

private Document document=null;

public static boolean fileExist(String fileName) {

java.io.File objFile = new java.io.File(fileName);

if (objFile.exists()) {

return true;

} else {

return false;

}

}

public void createXMLFile(String XMLFileName, String rootName) {

if (!fileExist(XMLFileName)) {

this.document = DocumentHelper.createDocument();

Element element = this.document.addElement(rootName);

// 加入注释 element.addComment(String)

// 加入节点 element.addElement(String);

// 加入属性内容 element.addAttribute(NAME,VALUE);

// 设置内容 element.setText(String);

//System.out.println("File created!");

saveXMLFile(XMLFileName);

} else {

System.out.println("File Exist!");

}

}

public void addChild(String fatherPath,String childName, String childValue) {

if (this.document==null)

{

System.out.println("Has not get XML file, add err!");

return;

}

List list = this.document.selectNodes(fatherPath);

Iterator iter = list.iterator();

if (iter.hasNext()) {

Element element = (Element) iter.next();

Element childelement = element.addElement(childName);

childelement.setText(childValue);

} else {

System.out.println("Father node does not exist!Add error!");

}

}

public void modifyNode(String XMLFileName, String nodePath,

String nodeValue, String newValue){

if (this.document==null)

{

System.out.println("Has not get XML file, modify err!");

return;

}

List list = this.document.selectNodes(nodePath);

Iterator iter = list.iterator();

boolean nodeExist = false;

while (iter.hasNext()) {

Element element = (Element) iter.next();

if (element.getText().equals(nodeValue)) {

element.setText(newValue);

nodeExist = true;

}

}

if (!nodeExist) {

System.out.println("Target node does not exist!Modify error!");

}

}

public void saveXMLFile(String XMLFileName) {

if (this.document==null)

{

System.out.println("Has not get XML file, save err!");

return;

}

try {

/** 将document中的内容写入文件中 */

XMLWriter writer = new XMLWriter(new FileWriter(new File(

XMLFileName)));

writer.write(this.document);

writer.close();

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

}

public void read(String XMLFileName){

if (fileExist(XMLFileName)) {

SAXReader reader = new SAXReader();

try {

this.document = reader.read(new File(XMLFileName));

} catch (MalformedURLException e) {

System.out.println(e.getMessage());

} catch (DocumentException e) {

System.out.println(e.getMessage());

}

} else {

System.out.println("XML file does not exist,read error!");

System.exit(0);

}

}

public Element getRootElement() {

if (this.document==null)

{

System.out.println("Has not get XML file, get root element err!");

return null;

}

return this.document.getRootElement();

}

public String getNodeValue(String nodePath){

if (this.document==null)

{

System.out.println("Has not get XML file, get node value err!");

return null;

}

List list = this.document.selectNodes(nodePath);

Iterator iter = list.iterator();

boolean nodeExist = false;

String nodeValue = null;

if (iter.hasNext()) {

Element element = (Element) iter.next();

nodeValue = element.getText();

return nodeValue;

} else {

System.out.println("Target node does not exist!Read node error!");

System.exit(0);

}

return null;

}

public void close()

{

if (this.document==null)

{

System.out.println("Has not get XML file, close err!");

return;

}

this.document=null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值