java中的XML追加内容,追加节点,和节点的内容,还要常用的设计模式的代码

java中的XML追加内容,追加节点,和节点的内容,还要常用的设计模式的代码(越多越好)
麻烦有例子的代码,打个包都发给我,我的邮箱[email protected]
发邮件的时候记得写下你在百度知道里叫什么名字,我好给分!
本人分不多,先给50,收到了,XML的例题易懂,且可以使用,其他的代码都没问题追加50分,跪求高手发代码!

第1个回答  2012-07-06
/**
* 根据Xml文件生成Document对象
*
* @param file
* xml文件路径
* @return Document对象
* @throws DocumentException
*/
public static Document getDocument(String path) throws DocumentException {
File file = new File(path);
SAXReader xmlReader = new SAXReader();
return xmlReader.read(file);
}

/**
* 根据输入流生成Document对象
*
* @param is
* 输入流
* @return Document对象
* @throws DocumentException
*/
public static Document getDocument(InputStream is) throws DocumentException {
SAXReader xmlReader = new SAXReader();
return xmlReader.read(is);
}

/**
* 根据Document得到根结点
*
* @param doc
* Document目录
* @return 根结点
* @throws DocumentException
*/
public static Element getRoot(String path) throws DocumentException {
Document doc = getDocument(path);
return doc.getRootElement();
}

/**
* 取出当前结点下的所有子结点
*
* @param root
* 当前结点
* @return 一组Element
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Element> getElements(String path)
throws DocumentException {
Element root = getRoot(path);
return root.elements();
}

/**
* 根据元素名称返回一组Element
*
* @param root
* 当前结点
* @param name
* 要返回的元素名称
* @return 一组Element
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Element> getElementsByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.elements(name);
}

/**
* 根据元素名称返回一个元素(如果有多个元素的话,只返回第一个)
*
* @param root
* 当前结点
* @param name
* 要返回的元素名称
* @return 一个Element元素
* @throws DocumentException
*/
public static Element getElementByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.element(name);
}

/**
* 根据当前元素,返回该元素的所有属性
*
* @param root
* 当前结点
* @return 当前结点的所有属性
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Attribute> getAttributes(String path)
throws DocumentException {
Element root = getRoot(path);
return root.attributes();
}

/**
* 根据属性名称,返回当前元素的某个属性
*
* @param root
* 当前结点
* @return 当前结点的一个属性
* @throws DocumentException
*/
public static Attribute getAttributeByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.attribute(name);
}
public static List<Element> getElementWithAttribute(String path,String attr,String value) throws DocumentException{
List<Element> elist = new ArrayList<Element>() ;
List<Element> list = getElements(path);
for(Element element:list){
if(element.attribute(attr).getText().equals(value)){
elist.add(element);
}
}
try {
elist.add(getElementSelf(path, attr, value));
} catch (Exception e) {
// TODO: handle exception
}
return elist;
}
public static Element getElementSelf(String path,String attr,String value) throws DocumentException{
Element element = null ;
List<Element> list = getElements(path);
for(Element e:list){
if(e.attribute(attr).getText().equals(value)){
element = e ;
}
}
return element;
}
相似回答