<!--<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;
public class CreateXML
{
DocumentBuilderFactoryfactory = null ;
DocumentBuilderbuilder = null ;
org.w3c.dom.DocumentmyDocument = null ;
// 创建XML文件
// 要创建的XML名字和路进
public void ctrateXMlFile(Stringfile){
ElementcarElement = new Element( " web-app " ); // 建立元素
DocumentmyDocument = new Document(carElement); // 建立一个文档并指定根元素
try {
XMLOutputteroutputter = new XMLOutputter();
outputter.output(myDocument,System. out );
FileWriterwriter = new FileWriter(file);
outputter.output(myDocument,writer);
writer.close();
} catch (java.io.IOExceptione){
e.printStackTrace();
}
}
// 增加节点
// 第1个参数:要增加节点的名字,第2个参数:要修改xml的路进名
public void addXMLNode(StringnodeName,StringxmlFile){
try {
Elementelement = null ;
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
if (doc.getRootElement().getChild(nodeName) != null ){
System. out .println( " 该节点以存在! " );
}
else {
element = new Element(nodeName);
doc.getRootElement().addContent(element);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加节点属性
// 第1个参数:要增加属性的节点的名字,第2个参数:要增加属性的名字,第3个参数:属性的值,第4个参数:要修改xml的路进名
public void setXMLNodeAttribute(StringnodeName,Stringattribute,Stringvalue,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(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 = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加接点内容
// 第1个参数:要增加内容的节点的名字,第2个参数:要增加的内容,第3个参数:要修改xml的路进名
public void setXMLNodeContent(StringnodeName,Stringcontent,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子接点
// 第1个参数:要增子节点的节点的名字,第2个参数:要增加的子节点的名字,第3个参数:要修改xml的路进名
public void setXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = doc.getRootElement();
if (e.getChild(nodeName) == null ){
System. out .println( " 该节点不存在! " );
}
else if (e.getChild(nodeName).getChild(childName) != null ){
System. out .println( " 该子节点以存在! " );
}
else {
Elementchild = new Element(childName);
e.getChild(nodeName).addContent(child);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子节点属性
// 第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:属性的名字,第4个参数:属性的值,第4个参数:要修改xml的路进名
public void setXMLChildNodeAttribute(StringnodeName,StringchildName,Stringattribute,Stringvalue,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子节点的内容
// 第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:要增加的内容,第4个参数:要修改xml的路进名
public void setXMLChildNodeContent(StringnodeName,StringchildName,Stringcontent,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 删除节点
// 第1个参数:要删除的节点名字,第2个参数:要修改xml的路进名
public void removeXMLNode(StringnodeName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = doc.getRootElement();
if (e.getChild(nodeName) == null ){
System. out .println( " 该节点不存在! " );
}
else {
e.removeChild(nodeName);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 删除子节点
// 第1个参数:节点名字,第2个参数:要删除的子节点的名字,第3个参数:要修改xml的路进名
public void removeXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
public static void main(String[]args)throwsException{
CreateXMLxml = 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 " , " helloword! " , " create.xml " );
// 删除节点
// xml.removeXMLNode("zhangbo3","create.xml");
// 删除子节点
// xml.removeXMLChildNode("zhangbo3","mapping","create.xml");
}
}
importjava.util. * ;
importjava.io. * ;
importorg.jdom.Document;
importorg.jdom.Element;
importorg.jdom.input.SAXBuilder;
importorg.jdom.output.XMLOutputter;
public class CreateXML
{
DocumentBuilderFactoryfactory = null ;
DocumentBuilderbuilder = null ;
org.w3c.dom.DocumentmyDocument = null ;
// 创建XML文件
// 要创建的XML名字和路进
public void ctrateXMlFile(Stringfile){
ElementcarElement = new Element( " web-app " ); // 建立元素
DocumentmyDocument = new Document(carElement); // 建立一个文档并指定根元素
try {
XMLOutputteroutputter = new XMLOutputter();
outputter.output(myDocument,System. out );
FileWriterwriter = new FileWriter(file);
outputter.output(myDocument,writer);
writer.close();
} catch (java.io.IOExceptione){
e.printStackTrace();
}
}
// 增加节点
// 第1个参数:要增加节点的名字,第2个参数:要修改xml的路进名
public void addXMLNode(StringnodeName,StringxmlFile){
try {
Elementelement = null ;
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
if (doc.getRootElement().getChild(nodeName) != null ){
System. out .println( " 该节点以存在! " );
}
else {
element = new Element(nodeName);
doc.getRootElement().addContent(element);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加节点属性
// 第1个参数:要增加属性的节点的名字,第2个参数:要增加属性的名字,第3个参数:属性的值,第4个参数:要修改xml的路进名
public void setXMLNodeAttribute(StringnodeName,Stringattribute,Stringvalue,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(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 = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加接点内容
// 第1个参数:要增加内容的节点的名字,第2个参数:要增加的内容,第3个参数:要修改xml的路进名
public void setXMLNodeContent(StringnodeName,Stringcontent,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子接点
// 第1个参数:要增子节点的节点的名字,第2个参数:要增加的子节点的名字,第3个参数:要修改xml的路进名
public void setXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = doc.getRootElement();
if (e.getChild(nodeName) == null ){
System. out .println( " 该节点不存在! " );
}
else if (e.getChild(nodeName).getChild(childName) != null ){
System. out .println( " 该子节点以存在! " );
}
else {
Elementchild = new Element(childName);
e.getChild(nodeName).addContent(child);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子节点属性
// 第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:属性的名字,第4个参数:属性的值,第4个参数:要修改xml的路进名
public void setXMLChildNodeAttribute(StringnodeName,StringchildName,Stringattribute,Stringvalue,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 增加子节点的内容
// 第1个参数:节点的名字,第2个参数:要增加属性的子节点的名字,第3个参数:要增加的内容,第4个参数:要修改xml的路进名
public void setXMLChildNodeContent(StringnodeName,StringchildName,Stringcontent,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 删除节点
// 第1个参数:要删除的节点名字,第2个参数:要修改xml的路进名
public void removeXMLNode(StringnodeName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = doc.getRootElement();
if (e.getChild(nodeName) == null ){
System. out .println( " 该节点不存在! " );
}
else {
e.removeChild(nodeName);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
// 删除子节点
// 第1个参数:节点名字,第2个参数:要删除的子节点的名字,第3个参数:要修改xml的路进名
public void removeXMLChildNode(StringnodeName,StringchildName,StringxmlFile){
try {
SAXBuilderbuilder = new SAXBuilder();
Documentdoc = builder.build( new File(xmlFile));
Elemente = 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);
XMLOutputterfmt = new XMLOutputter();
fmt.output(doc,System. out );
FileWriterwriter = new FileWriter(xmlFile);
fmt.output(doc,writer);
writer.close();
}
}
catch (Exceptionex){
ex.printStackTrace();
}
}
public static void main(String[]args)throwsException{
CreateXMLxml = 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 " , " helloword! " , " create.xml " );
// 删除节点
// xml.removeXMLNode("zhangbo3","create.xml");
// 删除子节点
// xml.removeXMLChildNode("zhangbo3","mapping","create.xml");
}
}