欢迎来到天天文库
浏览记录
ID:47427749
大小:53.01 KB
页数:11页
时间:2020-01-10
《dom对xml文件内容的增删改查》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、dom解析xml文件内容,不说了,直接上代码.packagecn.xmk.xml1;importjava.io.File;importjava.io.FileOutputStream;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFacto
2、ry;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.stream.StreamResult;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.NodeList;publicclassDomTest2{publicstaticvoidmain(String[]args)throwsException{DomTest2dt=newDomTes
3、t2();//dt.create();//创建//dt.add();//增加内容//dt.modify();//修改内容dt.remove();//移除内容}//删除xmlprivatevoidremove(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();Documentdoc=builder.parse(
4、"persons.xml");NodeListnlist=doc.getElementsByTagName("person");for(inti=0;i5、}}result(doc);}catch(Exceptione){e.printStackTrace();}}//修改xml内容privatevoidmodify(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();Documentdoc=builder.parse("persons.xml");NodeLis6、tnlist=doc.getElementsByTagName("person");for(inti=0;i7、"987654321");}}result(doc);}catch(Exceptione){e.printStackTrace();}}//创建xmlpublicvoidcreate(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();//创建xml对象Documentdoc=builder.newDocume8、nt();Elementlinkman=doc.createElement("linkman");Elementperson=doc.createElement("person");Elementname=doc.createElement("name");Elementphone=doc.createElement("phone");name.setTextConten
5、}}result(doc);}catch(Exceptione){e.printStackTrace();}}//修改xml内容privatevoidmodify(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();Documentdoc=builder.parse("persons.xml");NodeLis
6、tnlist=doc.getElementsByTagName("person");for(inti=0;i7、"987654321");}}result(doc);}catch(Exceptione){e.printStackTrace();}}//创建xmlpublicvoidcreate(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();//创建xml对象Documentdoc=builder.newDocume8、nt();Elementlinkman=doc.createElement("linkman");Elementperson=doc.createElement("person");Elementname=doc.createElement("name");Elementphone=doc.createElement("phone");name.setTextConten
7、"987654321");}}result(doc);}catch(Exceptione){e.printStackTrace();}}//创建xmlpublicvoidcreate(){try{DocumentBuilderFactorydbFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dbFactory.newDocumentBuilder();//创建xml对象Documentdoc=builder.newDocume
8、nt();Elementlinkman=doc.createElement("linkman");Elementperson=doc.createElement("person");Elementname=doc.createElement("name");Elementphone=doc.createElement("phone");name.setTextConten
此文档下载收益归作者所有