dom4j

package com.huawei.hedex.cooperate.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.CharacterCodingException;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
* <一句话功能简述>
* <功能详细描述>
*
* @author zKF31134(zengfengjiao)
* Created:2010-9-7
* @since HedEx Server V100R002C12/模块
*/
public class A
{

private static File file = new File("C:/Program Files/HuaWei/hedc/libraries/navi.xml");
/**
* <一句话功能简述>
* <功能详细描述>
*
* @param args [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author zKF31134(zengfengjiao)
* Created:2010-9-7
* @throws CharacterCodingException
* @throws DocumentException
*/
public static void main(String[] args)
{

//修改节点
//updateNode();

//删除节点
deleteNode();
}

public static Document getDocument(){
Document dom = null;
SAXReader saxReader = null;
try
{
File file = new File("C:/Program Files/HuaWei/hedc/libraries/navi.xml");
FileInputStream fileInput = new FileInputStream(file);
BufferedInputStream buffered = new BufferedInputStream(fileInput);

saxReader = new SAXReader();
dom = saxReader.read(buffered);

}
catch (DocumentException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}

return dom;
}

//修改节点
public static void updateNode(){
String topicId = "7897964646_6464_10001";
Document dom = A.getDocument();
//获取所有的一级节点
Element element = dom.getRootElement();
//获取指定的某个节点
Element ele =(Element) element.selectSingleNode(".//topic[@id='"+topicId+"']");
//获取需要修改节点的txt属性
Attribute attribute = ele.attribute("txt");
attribute.setValue("修改成功");

A.outPutXml(dom, file);
}

//删除节点
public static void deleteNode(){
String topicId = "7897964646_6464_10002";
//获取Document
Document dom = A.getDocument();
//获取所有的一级节点
Element element = dom.getRootElement();
//找到删除节点进行删除
Element ele =(Element) element.selectSingleNode(".//topic[@id='"+topicId+"']");
element.remove(ele);

A.outPutXml(dom, file);

}


//输入XML文档
public static void outPutXml(Document document,File file){
try
{
FileOutputStream fileOut = new FileOutputStream(file);
XMLWriter xmlWriter = new XMLWriter(fileOut);
xmlWriter.write(document);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值