zoukankan
html css js c++ java
xml02 XML编程(CRUD)增删查改
XML解析技术概述
Demo2.java
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class Demo2 { public static void main(String args[])throws Exception { //1.创建工程 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.得到dom解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //3.解析xml文档,得到代表文档的document Document document = builder.parse("src/book.xml"); } }
xml解析技术概述和使用Jaxp对xml文档进行dom解析
package cn.lysine; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; //使用dom方式对xml文档进行 增删查改 CRUD public class Demo3 { // 读取xml文档中;<书名>java就业培训中心</书名> 节点的值 @Test public void read1() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(1); String content = node.getTextContent(); System.out.println(content); //输出 java就业培训中心 } //得到xml中标签属性的值 @Test public void read2() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); // 得到根结点 Node root = document.getElementsByTagName("书架").item(0); list(root); } private void list(Node node) { System.out.println(node.getNodeName()); NodeList list = node.getChildNodes(); for(int i = 0; i <list.getLength(); i++ ){ Node childe = list.item(i); list(childe); } } //得到xml文档中标签属性的值:<书名 name="xxxx">java就业培训教材</书名> @Test public void read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); Element bookname = (Element)document.getElementsByTagName("书名").item(0); String value = bookname.getAttribute("name"); System.out.println(value); } //输出 xxxx }
import java.io.FileOutputStream; import java.io.IOException; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; //使用dom方式对xml文档进行 增删查改 CRUD public class Demo3 { // 读取xml文档中;<书名>java就业培训中心</书名> 节点的值 @Test public void read1() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(0); String content = node.getTextContent(); System.out.println(content); } //得到xml中标签属性的值 @Test public void read2() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); // 得到根结点 Node root = document.getElementsByTagName("书架").item(0); list(root); } private void list(Node node) { System.out.println(node.getNodeName()); NodeList list = node.getChildNodes(); for(int i = 0; i <list.getLength(); i++ ){ Node childe = list.item(i); list(childe); } } //得到xml文档中标签属性的值:<书名 name="xxxx">java就业培训教材</书名> @Test public void read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); Element bookname = (Element)document.getElementsByTagName("书名").item(0); String value = bookname.getAttribute("name"); System.out.println(value); } //向xml文档中添加节点:<售价>59.00元</售价> @Test public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("59.00元"); //得到参考节点 Element refNode = (Element) document.getElementsByTagName("售价").item(0); //得到要挂崽的节点 把创建的节点挂到第一本书上 Element book = (Element)document.getElementsByTagName("书").item(0); //往book节点的指定位置插崽 book.insertBefore(price,refNode); //把更新后内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //add 向xml文档中添加节点:<书名>java就业培训教程</书名> 上添加name=“xxxx”属性 @Test public void addAttr() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); Element bookname = (Element) document.getElementsByTagName("书名").item(0); bookname.setAttribute("name", "xxxxx"); //把更新后内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"))); } //删除整个xml @Test public void delete1() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //得到要删除的结点 Element e = (Element) document.getElementsByTagName("售价").item(0); //得到要删除的结点的爸爸 Element book = (Element) document.getElementsByTagName("书").item(0); //爸爸再删崽 book.removeChild(e); //把更新后内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"))); } public void delete2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //得到要删除的结点 Element e = (Element) document.getElementsByTagName("售价").item(0); e.getParentNode().getParentNode().getParentNode().removeChild(e.getParentNode().getParentNode()); //把更新后内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"))); } //update 更新 public void update()throws ParserConfigurationException, SAXException, IOException,Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //得到要更新的结点 Element e = (Element) document.getElementsByTagName("售价").item(0); e.setTextContent("109元"); //把更新后内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"))); } }
查看全文
相关阅读:
【BZOJ3028】食物(生成函数基础题)
【BZOJ2438】[中山市选2011] 杀人游戏(Tarjan)
【BZOJ4833】[Lydsy1704月赛] 最小公倍佩尔数(神仙数学题)
【BZOJ2109】【BZOJ2535】[NOI2010] 航空管制(拓扑反向建图)
【BZOJ2679】[USACO2012 Open] Balanced Cow Subsets(Meet in Middle)
【BZOJ3091】城市旅行(再次重拾LCT)
sass与compass实战
【Sass初级】开始使用Sass和Compass
nodejs、sass、backbone等api地址
解读2015之前端篇:工业时代 野蛮发展(转)
原文地址:https://www.cnblogs.com/firecode/p/2460926.html
最新文章
【BZOJ2119】股市的预测(后缀数组)
【BZOJ4278】[ONTAK2015] Tasowanie(后缀数组+贪心)
【BZOJ2251】[BJWC2010] 外星联络(重拾后缀数组)
【BZOJ4869】[SHOI2017] 相逢是问候(扩展欧拉定理+线段树)
【BZOJ3884】上帝与集合的正确用法(扩展欧拉定理)
【BZOJ2986】Non-Squarefree Numbers(莫比乌斯函数容斥)
【BZOJ2440】[中山市选2011] 完全平方数(莫比乌斯函数容斥)
【BZOJ3671】[NOI2014] 随机数生成器(听说这题卡常于是我就愉快地被卡了内存)
【BZOJ2667】[CQOI2012] 模拟工厂(暴力枚举+贪心验证)
【BZOJ2697】特技飞行(好久没做过这么水的题了,所以我为何还要水博客)
热门文章
【BZOJ2437】[NOI2011] 兔兔与蛋蛋(博弈论+二分图匹配)
【AtCoder】AtCoder Grand Contest 043 解题报告($E,F$或许将成为永远的坑?)
【BZOJ2275】[COCI2010] HRPA(斐波那契博弈)
【BZOJ2927】[POI1999] 多边形之战(博弈论SB题)
【BZOJ1022】[SHOI2008] 小约翰的游戏(Anti-Nim)
【BZOJ3563】DZY Loves Chinese(挖坟:长达20个月的坑)
【BZOJ3214】[ZJOI2013] 丽洁体(毒瘤字符串读入+动态规划水题)
【BZOJ1791】[IOI2008] 岛屿(水题)
【BZOJ2001】[HNOI2010] 城市建设(神奇的CDQ分治)
【AtCoder】AtCoder Grand Contest 041 解题报告
Copyright © 2011-2022 走看看