?? createxml.java
字號:
//基于jdom...
import java.util.*;
import java.io.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
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");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -