用JDOM建立XML文件

类别:Java 点击:0 评论:0 推荐:

import javax.xml.parsers.*;
import java.util.*;
import java.io.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class CreateXML {
  DocumentBuilderFactory factory=null;
    DocumentBuilder builder=null;
    org.w3c.dom.Document myDocument = null;
 //创建XML文件
   //要创建的XML名字和路进
 public void ctrateXMlFile(String file){
  Element carElement = new Element("web-app");//建立元素
  Document myDocument = new Document(carElement);//建立一个文档并指定根元素
  try {
      XMLOutputter outputter = new XMLOutputter();
      outputter.output(myDocument, System.out);
      FileWriter writer = new FileWriter(file);
      outputter.output(myDocument, writer);
      writer.close();
  } catch (java.io.IOException e) {
      e.printStackTrace();
  }
 }
 //增加节点
   //第1个参数:要增加节点的名字,第2个参数:要修改xml的路进名
 public void addXMLNode(String nodeName,String xmlFile){
         try {
     Element element=null;
     SAXBuilder builder = new SAXBuilder();
     Document doc = builder.build(new File(xmlFile));
    if(doc.getRootElement().getChild(nodeName)!=null){
     System.out.println("该节点以存在!");
    }
    else{
     element =new Element(nodeName);
             doc.getRootElement().addContent(element);
             XMLOutputter fmt = new XMLOutputter();
             fmt.output(doc, System.out);
             FileWriter writer = new FileWriter(xmlFile);
             fmt.output(doc, writer);
       writer.close();
    }
             }
         catch(Exception ex){
          ex.printStackTrace();
         }

 }
 //增加节点属性
   //第1个参数:要增加属性的节点的名字,第2个参数:要增加属性的名字,第3个参数:属性的值,第4个参数:要修改xml的路进名
 public void setXMLNodeAttribute(String nodeName,String attribute,String value,String xmlFile){
  try {
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
      //System.out.println("a"+ e.getChild("servlet"));
    if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else{
   e.getChild(nodeName).setAttribute(attribute,value);
   XMLOutputter fmt = new XMLOutputter();
            fmt.output(doc, System.out);
            FileWriter writer = new FileWriter(xmlFile);
            fmt.output(doc, writer);
      writer.close();
   }
            }
        catch(Exception ex){
         ex.printStackTrace();
        }
 }
 //增加接点内容
   //第1个参数:要增加内容的节点的名字,第2个参数:要增加的内容,第3个参数:要修改xml的路进名
 public void setXMLNodeContent(String nodeName,String content,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
   //System.out.println("a"+ e.getChild("servlet"));
   if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else if(e.getChild(nodeName).getText().equals(content)){
    System.out.println("该节点内容以存在!");
    
   }
   else{
    e.getChild(nodeName).addContent(content);
   XMLOutputter fmt = new XMLOutputter();
   fmt.output(doc, System.out);
   FileWriter writer = new FileWriter(xmlFile);
   fmt.output(doc, writer);
   writer.close();
   }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
 }
    //增加子接点
   //第1个参数:要增子节点的节点的名字,第2个参数:要增加的子节点的名字,第3个参数:要修改xml的路进名
 public void setXMLChildNode(String nodeName,String childName,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
            if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else if(e.getChild(nodeName).getChild(childName)!=null){
    System.out.println("该子节点以存在!");
   }
   else{
    Element child=new Element(childName);
    e.getChild(nodeName).addContent(child);
    XMLOutputter fmt = new XMLOutputter();
    fmt.output(doc, System.out);
    FileWriter writer = new FileWriter(xmlFile);
    fmt.output(doc, writer);
    writer.close();
   }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
 }
 //增加子节点属性
   //第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:属性的名字,第4个参数:属性的值,第4个参数:要修改xml的路进名
    public void setXMLChildNodeAttribute(String nodeName,String childName,String attribute,String value,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
      if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else if(e.getChild(nodeName).getChild(childName)==null){
    System.out.println("该子节点不存在!");
   }
   else{
    e.getChild(nodeName).getChild(childName).setAttribute(attribute,value);
    XMLOutputter fmt = new XMLOutputter();
    fmt.output(doc, System.out);
    FileWriter writer = new FileWriter(xmlFile);
    fmt.output(doc, writer);
    writer.close();
   }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
    }
 //增加子节点的内容
   //第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:要增加的内容,第4个参数:要修改xml的路进名
 public void setXMLChildNodeContent(String nodeName,String childName,String content,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
      if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else if(e.getChild(nodeName).getChild(childName).getText().equals(content)){
    System.out.println("该子节点内容以存在!");
   }
   else if(e.getChild(nodeName).getChild(childName)==null){
    System.out.println("该子节点不存在!");
   }
   else{
    e.getChild(nodeName).getChild(childName).addContent(content);  
    XMLOutputter fmt = new XMLOutputter();
    fmt.output(doc, System.out);
    FileWriter writer = new FileWriter(xmlFile);
    fmt.output(doc, writer);
    writer.close();
   }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }  
 }
 //删除节点
   //第1个参数:要删除的节点名字,第2个参数:要修改xml的路进名
 public void removeXMLNode(String nodeName,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
      if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else{
    e.removeChild(nodeName);
   XMLOutputter fmt = new XMLOutputter();
   fmt.output(doc, System.out);
   FileWriter writer = new FileWriter(xmlFile);
   fmt.output(doc, writer);
   writer.close();
   }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
 }
 //删除子节点
   //第1个参数:节点名字,第2个参数:要删除的子节点的名字,第3个参数:要修改xml的路进名
 public void removeXMLChildNode(String nodeName,String childName,String xmlFile){
  try{
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File(xmlFile));
   Element e=doc.getRootElement();
   //System.out.println("a"+ e.getChild("servlet"));
   if(e.getChild(nodeName)==null){
    System.out.println("该节点不存在!");
   }
   else if(e.getChild(nodeName).getChild(childName)==null){
    System.out.println("该子节点不存在!");
   }
   else{
   e.getChild(nodeName).removeChild(childName);
   XMLOutputter fmt = new XMLOutputter();
   fmt.output(doc, System.out);
   FileWriter writer = new FileWriter(xmlFile);
   fmt.output(doc, writer);
   writer.close();
   }
   
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
 }
 public static void main(String[] args)throws Exception{
  CreateXML xml=new CreateXML();
  //新建xml
  xml.ctrateXMlFile("create.xml");
  //增加节点
  xml.addXMLNode("zhangbo3","create.xml");
  //增加节点属性
  xml.setXMLNodeAttribute("zhangbo3","name","zhangbo","create.xml");
  //增加节点的内容
  xml.setXMLNodeContent("zhangbo3","white-collar","create.xml");
  //增加子节点
  xml.setXMLChildNode("zhangbo3","mapping","create.xml");
  //增加子节点的属性
  xml.setXMLChildNodeAttribute("zhangbo3","mapping","name","struts-config.xml","create.xml");
  //增加子节点的内容
  xml.setXMLChildNodeContent("zhangbo3","mapping","hello word!","create.xml");
  //删除节点
  //xml.removeXMLNode("zhangbo3","create.xml");
  //删除子节点
  //xml.removeXMLChildNode("zhangbo3","mapping","create.xml");
 }
}

本文地址:http://com.8s8s.com/it/it11048.htm