dom对xml文件内容的增删改查.docx

上传人:牧羊曲112 文档编号:3155815 上传时间:2023-03-11 格式:DOCX 页数:5 大小:37.23KB
返回 下载 相关 举报
dom对xml文件内容的增删改查.docx_第1页
第1页 / 共5页
dom对xml文件内容的增删改查.docx_第2页
第2页 / 共5页
dom对xml文件内容的增删改查.docx_第3页
第3页 / 共5页
dom对xml文件内容的增删改查.docx_第4页
第4页 / 共5页
dom对xml文件内容的增删改查.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《dom对xml文件内容的增删改查.docx》由会员分享,可在线阅读,更多相关《dom对xml文件内容的增删改查.docx(5页珍藏版)》请在三一办公上搜索。

1、dom对xml文件内容的增删改查dom解析xml文件内容,不说了,直接上代码. package cn.xmk.xml1; import java.io.File; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import

2、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; public class DomTest2 public static void main(String args) throws Exception DomTest2 dt = new DomTest2; / dt.create; /创建 / dt.add;

3、/增加内容 / dt.modify; /修改内容 dt.remove; /移除内容 /删除xml private void remove try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder builder = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(person); for (

4、int i = 0; i nlist.getLength; i+) Element e = (Element) nlist.item(i); String str = e.getElementsByTagName(name).item(0) .getTextContent; if (赵六.equals(str.trim) e.getParentNode.removeChild(e); result(doc); catch (Exception e) e.printStackTrace; /修改xml内容 private void modify try DocumentBuilderFactor

5、y dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder builder = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(person); for (int i = 0; i nlist.getLength; i+) Element e = (Element) nlist.item(i); String str = e.getEleme

6、ntsByTagName(name).item(0) .getTextContent; if (赵六.equals(str.trim) e.getElementsByTagName(phone).item(0) .setTextContent(987654321); result(doc); catch (Exception e) e.printStackTrace; /创建xml public void create try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuil

7、der builder = dbFactory.newDocumentBuilder; / 创建xml对象 Document doc = builder.newDocument; Element linkman = doc.createElement(linkman); Element person = doc.createElement(person); Element name = doc.createElement(name); Element phone = doc.createElement(phone); name.setTextContent(赵六); phone.setText

8、Content(87654321); person.appendChild(name); person.appendChild(phone); linkman.appendChild(person); doc.appendChild(linkman); result(doc); catch (Exception e) e.printStackTrace; /增加一条内容 public void add try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder build

9、er = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(linkman); Element e = (Element) nlist.item(0); Element person = doc.createElement(person); Element name = doc.createElement(name); Element phone = doc.createElement(phone); name.se

10、tTextContent(其他); phone.setTextContent(234324324324); person.appendChild(name); person.appendChild(phone); e.appendChild(person); result(doc); catch (Exception e) e.printStackTrace; / 进行输出 public void result(Document doc) try / 开始输出 TransformerFactory tFactory = TransformerFactory.newInstance; Transformer former = tFactory.newTransformer; DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream( new File(persons.xml); former.transform(domSource, result); catch (Exception e) e.printStackTrace; Xml文件内容如下: 其他234324324324其他234324324324

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号