java对XML文件的解析、节点的增加、删除操作总结
程序员文章站
2024-02-15 21:34:34
1、java代码: 主要采用dom来进行操作 复制代码 代码如下: package test; import java.io.ioexception; import jav...
1、java代码:
主要采用dom来进行操作
package test;
import java.io.ioexception;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import javax.xml.transform.transformer;
import javax.xml.transform.transformerconfigurationexception;
import javax.xml.transform.transformerexception;
import javax.xml.transform.transformerfactory;
import javax.xml.transform.dom.domsource;
import javax.xml.transform.stream.streamresult;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.nodelist;
import org.w3c.dom.text;
import org.xml.sax.saxexception;
public class xmloprate {
document doc;
documentbuilderfactory factory = documentbuilderfactory.newinstance();
documentbuilder builder;
nodelist imags;
string path;
public nodelist getimags() {
return imags;
}
public void setimags(nodelist imags) {
this.imags = imags;
}
/**
* 构造方法
* @param path:xml文件的路径
* @param nodes:要解析的xml节点名称
*/
public xmloprate(string path) {
super();
this.path = path;
system.out.println(system.getproperty("user.dir"));
}
/**
* 解析xml
* @param path
*/
public void readxml(){
try {
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
nodelist imags =doc.getelementsbytagname("imags");
this.setimags(imags);
for (int i=0;i<imags.getlength();i++){
element link=(element) imags.item(i);
system.out.print("title: ");
system.out.println(link.getelementsbytagname("title").item(0).getfirstchild().getnodevalue());
system.out.print("url: ");
system.out.println(link.getelementsbytagname("url").item(0).getfirstchild().getnodevalue());
system.out.print("imgsrc: ");
system.out.println(link.getelementsbytagname("imgsrc").item(0).getfirstchild().getnodevalue());
system.out.println();
}
}catch (parserconfigurationexception e) {
e.printstacktrace();
} catch (saxexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
/**
* addcode
* @param path
*/
public void addxmlcode(string imgsrc,string title,string url){
try {
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
text textseg;
element imag=doc.createelement("imags");
element linkimgsrc=doc.createelement("imgsrc");
textseg=doc.createtextnode(imgsrc);
linkimgsrc.appendchild(textseg);
imag.appendchild(linkimgsrc);
element linktitle=doc.createelement("title");
textseg=doc.createtextnode(title);
linktitle.appendchild(textseg);
imag.appendchild(linktitle);
element linkurl=doc.createelement("url");
textseg=doc.createtextnode(url);
linkurl.appendchild(textseg);
imag.appendchild(linkurl);
doc.getdocumentelement().appendchild(imag);
transformerfactory tfactory =transformerfactory.newinstance();
transformer transformer;
transformer = tfactory.newtransformer();
domsource source = new domsource(doc);
streamresult result = new streamresult(new java.io.file(path));
transformer.transform(source, result);
}catch(exception e){
}
}
/**
* delete xml code
* @param path
*/
public void delxmlcode(){
try {
builder = factory.newdocumentbuilder();
doc=builder.parse(path);
doc.normalize();
nodelist imags =doc.getelementsbytagname("imags");
element elink=(element) imags.item(0);
elink.removechild(elink.getelementsbytagname("imgsrc").item(0));
elink.removechild(elink.getelementsbytagname("title").item(0));
elink.removechild(elink.getelementsbytagname("url").item(0));
doc.getfirstchild().removechild(elink);
transformerfactory tfactory =transformerfactory.newinstance();
transformer transformer = tfactory.newtransformer();
domsource source = new domsource(doc);
streamresult result = new streamresult(new java.io.file(path));
transformer.transform(source, result);
} catch (parserconfigurationexception e) {
e.printstacktrace();
} catch (saxexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
} catch (transformerconfigurationexception e) {
e.printstacktrace();
} catch (transformerexception e) {
e.printstacktrace();
}
}
}
2、xml部分
<?xml version="1.0" encoding="utf-8" ?>
<root>
<imags>
<imgsrc>images/ad-01.jpg</imgsrc>
<title>胡志明市</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>
</imags>
<imags>
<imgsrc>images/ad-02.jpg</imgsrc>
<title>香港2</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
</root>
3、总结:
看对xml操作的三个方法(读、写、删),他们的初始化语句都相同:
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
开始我打算把这部分相同的部分拿出来写在构造方法中,但是在测试中发现,这样总是会报空指针的错误(搞了我好久);
而只是把
builder = factory.newdocumentbuilder();
放到builder的初始化语句中的时候,读没问题,但是当写或者删的时候也会报错;
所以就写成现在这样了,感觉有点乱乱的,但也没想到什么好的方法,就先贴到这里了,以后可能会用得着
主要采用dom来进行操作
复制代码 代码如下:
package test;
import java.io.ioexception;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import javax.xml.transform.transformer;
import javax.xml.transform.transformerconfigurationexception;
import javax.xml.transform.transformerexception;
import javax.xml.transform.transformerfactory;
import javax.xml.transform.dom.domsource;
import javax.xml.transform.stream.streamresult;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.nodelist;
import org.w3c.dom.text;
import org.xml.sax.saxexception;
public class xmloprate {
document doc;
documentbuilderfactory factory = documentbuilderfactory.newinstance();
documentbuilder builder;
nodelist imags;
string path;
public nodelist getimags() {
return imags;
}
public void setimags(nodelist imags) {
this.imags = imags;
}
/**
* 构造方法
* @param path:xml文件的路径
* @param nodes:要解析的xml节点名称
*/
public xmloprate(string path) {
super();
this.path = path;
system.out.println(system.getproperty("user.dir"));
}
/**
* 解析xml
* @param path
*/
public void readxml(){
try {
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
nodelist imags =doc.getelementsbytagname("imags");
this.setimags(imags);
for (int i=0;i<imags.getlength();i++){
element link=(element) imags.item(i);
system.out.print("title: ");
system.out.println(link.getelementsbytagname("title").item(0).getfirstchild().getnodevalue());
system.out.print("url: ");
system.out.println(link.getelementsbytagname("url").item(0).getfirstchild().getnodevalue());
system.out.print("imgsrc: ");
system.out.println(link.getelementsbytagname("imgsrc").item(0).getfirstchild().getnodevalue());
system.out.println();
}
}catch (parserconfigurationexception e) {
e.printstacktrace();
} catch (saxexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
/**
* addcode
* @param path
*/
public void addxmlcode(string imgsrc,string title,string url){
try {
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
text textseg;
element imag=doc.createelement("imags");
element linkimgsrc=doc.createelement("imgsrc");
textseg=doc.createtextnode(imgsrc);
linkimgsrc.appendchild(textseg);
imag.appendchild(linkimgsrc);
element linktitle=doc.createelement("title");
textseg=doc.createtextnode(title);
linktitle.appendchild(textseg);
imag.appendchild(linktitle);
element linkurl=doc.createelement("url");
textseg=doc.createtextnode(url);
linkurl.appendchild(textseg);
imag.appendchild(linkurl);
doc.getdocumentelement().appendchild(imag);
transformerfactory tfactory =transformerfactory.newinstance();
transformer transformer;
transformer = tfactory.newtransformer();
domsource source = new domsource(doc);
streamresult result = new streamresult(new java.io.file(path));
transformer.transform(source, result);
}catch(exception e){
}
}
/**
* delete xml code
* @param path
*/
public void delxmlcode(){
try {
builder = factory.newdocumentbuilder();
doc=builder.parse(path);
doc.normalize();
nodelist imags =doc.getelementsbytagname("imags");
element elink=(element) imags.item(0);
elink.removechild(elink.getelementsbytagname("imgsrc").item(0));
elink.removechild(elink.getelementsbytagname("title").item(0));
elink.removechild(elink.getelementsbytagname("url").item(0));
doc.getfirstchild().removechild(elink);
transformerfactory tfactory =transformerfactory.newinstance();
transformer transformer = tfactory.newtransformer();
domsource source = new domsource(doc);
streamresult result = new streamresult(new java.io.file(path));
transformer.transform(source, result);
} catch (parserconfigurationexception e) {
e.printstacktrace();
} catch (saxexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
} catch (transformerconfigurationexception e) {
e.printstacktrace();
} catch (transformerexception e) {
e.printstacktrace();
}
}
}
2、xml部分
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<imags>
<imgsrc>images/ad-01.jpg</imgsrc>
<title>胡志明市</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>
</imags>
<imags>
<imgsrc>images/ad-02.jpg</imgsrc>
<title>香港2</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
</root>
3、总结:
看对xml操作的三个方法(读、写、删),他们的初始化语句都相同:
复制代码 代码如下:
builder = factory.newdocumentbuilder();
document doc=builder.parse(path);
doc.normalize();
开始我打算把这部分相同的部分拿出来写在构造方法中,但是在测试中发现,这样总是会报空指针的错误(搞了我好久);
而只是把
builder = factory.newdocumentbuilder();
放到builder的初始化语句中的时候,读没问题,但是当写或者删的时候也会报错;
所以就写成现在这样了,感觉有点乱乱的,但也没想到什么好的方法,就先贴到这里了,以后可能会用得着
上一篇: jsp form表单方法示例
下一篇: JSP errorPage设置方法
推荐阅读
-
java对XML文件的解析、节点的增加、删除操作总结
-
java对XML文件的解析、节点的增加、删除操作总结
-
as3中对xml的创建、增加、删除、修改、检索等操作实例介绍
-
Winform中对自定义xml配置文件进行Xml节点的添加与删除
-
Java使用SAX解析无根节点的xml文件并过滤不合法字符
-
as3中对xml的创建、增加、删除、修改、检索等操作实例介绍
-
Winform中对自定义xml配置文件进行Xml节点的添加与删除
-
Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式
-
java对XML文件的解析、节点的增加、删除操作总结
-
java 对电脑的文件浏览(目录和文件)和删除操作