【转】用JDOM建立XML文件

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->importjavax.xml.parsers.*;
importjava.util.
*;
importjava.io.
*;
importorg.jdom.Document;
importorg.jdom.Element;
importorg.jdom.input.SAXBuilder;
importorg.jdom.output.XMLOutputter;

publicclassCreateXML
{
DocumentBuilderFactoryfactory
=null;
DocumentBuilderbuilder
=null;
org.w3c.dom.DocumentmyDocument
=null;
//创建XML文件
//要创建的XML名字和路进
publicvoidctrateXMlFile(Stringfile){
ElementcarElement
=newElement("web-app");//建立元素
DocumentmyDocument=newDocument(carElement);//建立一个文档并指定根元素
try{
XMLOutputteroutputter
=newXMLOutputter();
outputter.output(myDocument,System.
out);
FileWriterwriter
=newFileWriter(file);
outputter.output(myDocument,writer);
writer.close();
}
catch(java.io.IOExceptione){
e.printStackTrace();
}
}
//增加节点
//第1个参数:要增加节点的名字,第2个参数:要修改xml的路进名
publicvoidaddXMLNode(StringnodeName,StringxmlFile){
try{
Elementelement
=null;
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
if(doc.getRootElement().getChild(nodeName)!=null){
System.
out.println("该节点以存在!");
}
else{
element
=newElement(nodeName);
doc.getRootElement().addContent(element);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}

}
//增加节点属性
//第1个参数:要增加属性的节点的名字,第2个参数:要增加属性的名字,第3个参数:属性的值,第4个参数:要修改xml的路进名
publicvoidsetXMLNodeAttribute(StringnodeName,Stringattribute,Stringvalue,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
else{
e.getChild(nodeName).setAttribute(attribute,value);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加接点内容
//第1个参数:要增加内容的节点的名字,第2个参数:要增加的内容,第3个参数:要修改xml的路进名
publicvoidsetXMLNodeContent(StringnodeName,Stringcontent,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getText().equals(content)){
System.
out.println("该节点内容以存在!");

}
else{
e.getChild(nodeName).addContent(content);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子接点
//第1个参数:要增子节点的节点的名字,第2个参数:要增加的子节点的名字,第3个参数:要修改xml的路进名
publicvoidsetXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)!=null){
System.
out.println("该子节点以存在!");
}
else{
Elementchild
=newElement(childName);
e.getChild(nodeName).addContent(child);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子节点属性
//第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:属性的名字,第4个参数:属性的值,第4个参数:要修改xml的路进名
publicvoidsetXMLChildNodeAttribute(StringnodeName,StringchildName,Stringattribute,Stringvalue,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).getChild(childName).setAttribute(attribute,value);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//增加子节点的内容
//第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:要增加的内容,第4个参数:要修改xml的路进名
publicvoidsetXMLChildNodeContent(StringnodeName,StringchildName,Stringcontent,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName).getText().equals(content)){
System.
out.println("该子节点内容以存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).getChild(childName).addContent(content);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//删除节点
//第1个参数:要删除的节点名字,第2个参数:要修改xml的路进名
publicvoidremoveXMLNode(StringnodeName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
else{
e.removeChild(nodeName);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}
//删除子节点
//第1个参数:节点名字,第2个参数:要删除的子节点的名字,第3个参数:要修改xml的路进名
publicvoidremoveXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try{
SAXBuilderbuilder
=newSAXBuilder();
Documentdoc
=builder.build(newFile(xmlFile));
Elemente
=doc.getRootElement();
//System.out.println("a"+e.getChild("servlet"));
if(e.getChild(nodeName)==null){
System.
out.println("该节点不存在!");
}
elseif(e.getChild(nodeName).getChild(childName)==null){
System.
out.println("该子节点不存在!");
}
else{
e.getChild(nodeName).removeChild(childName);
XMLOutputterfmt
=newXMLOutputter();
fmt.output(doc,System.
out);
FileWriterwriter
=newFileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}

}
catch(Exceptionex){
ex.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsException{
CreateXMLxml
=newCreateXML();
//新建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","helloword!","create.xml");
//删除节点
//xml.removeXMLNode("zhangbo3","create.xml");
//删除子节点
//xml.removeXMLChildNode("zhangbo3","mapping","create.xml");
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值