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();
}
}
}
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();
}
}
}