XML语言学习CRUD3----方立勋JAXP

来源:互联网 发布:linux打开u盘命令 编辑:程序博客网 时间:2024/06/14 18:29

XML解析方式分为两种:dom和sax

dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式。

sax:(Simple API for XML) 不是官方标准,但它是 XML 社区事实上的标准,几乎所有的 XML 解析器都支持它。

dom文档:将文档装载到document,所有元素都会创建一个对象。只要得到根节点就可以得到一切。

优点:使用dom解析xml文档,实现crud特别方便,操作速度也比较快。 

缺点:使用dom解析xml文档,如果文件比较大,对内存消耗就特别大,极容易导致内存溢出,所以dom方式不适合操作大的xml文档。


JAVA虚拟机默认修改IntelliJ IDEA 7.0\bin下idea.exe.vmoptions   -xmx大小

sax:读到一行解析一行。

特点:

1、解析速度快、对内存占用少,查找数据特别方便

2、只适合查找数据,不适合作增删改操作

dom4j最好的XML解析开发包。但Jaxp是SUN公司的标准。


jaxp对xml文件进行crud:

先产生一个document解析对象。

//1.获取工厂DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//2.产生解析器DocumentBuilder builder = factory.newDocumentBuilder();//3.解析xml文档,得到代表文档的documentDocument document = builder.parse(new File("src/book1.xml"));


操作xml文档的元素时,一般都把元素当作node对象,但是程序员如果发现node不好使时,就应把node强转成相应类型


标签的CEUD操作:

查询:

//得到售价结点的值@Testpublic void read() throws Exception{//1.获取工厂DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book.xml"));NodeList list = document.getElementsByTagName("售价");Node price = list.item(0);String value = price.getTextContent();System.out.println(value);}



修改:   transformer

//修改结点的值:<售价>39.00元</售价>改为109@Testpublic  void update() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder  builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book1.xml"));Node price = document.getElementsByTagName("售价").item(0);price.setTextContent("109");//把内存中的document写到xml文档TransformerFactory tf = TransformerFactory.newInstance();//得到转换器Transformer ts = tf.newTransformer();ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));}



增加:

//向指定节点中增加孩子节点(售价节点)@Testpublic void add() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder  builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book1.xml"));//创建需要增加的节点Node price = document.createElement("售价");price.setTextContent("59元");//得到需要增加的节点的父亲Node parent = document.getElementsByTagName("书").item(0);//把需要增加的节点挂到父结点上parent.appendChild(price);TransformerFactory tf = TransformerFactory.newInstance();Transformer ts = tf.newTransformer();ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));}

//向指定位置上插入售价节点@Testpublic void add2() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder  builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book1.xml"));Node node = document.createElement("售价");node.setTextContent("39元");Node parent = document.getElementsByTagName("书").item(0);parent.insertBefore(node, document.getElementsByTagName("书名").item(0));TransformerFactory tf = TransformerFactory.newInstance();Transformer ts = tf.newTransformer();ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));}






删除:

//删除xml文档的售价结点@Testpublic void delete() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder  builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book1.xml"));Node node = document.getElementsByTagName("售价").item(2);node.getParentNode().removeChild(node);TransformerFactory tf = TransformerFactory.newInstance();Transformer ts = tf.newTransformer();ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));}


xml文档属性操作:

//操作xml文档属性@Testpublic void updateAttribute() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder  builder = factory.newDocumentBuilder();Document document = builder.parse(new File("src/book1.xml"));//操作xml文档的元素时,一般都把元素当作node对象,但是程序员如果发现node不好使时,就应把node强转成相应类型Node node  = document.getElementsByTagName("书").item(0);Element book = null;if(node.getNodeType()==Node.ELEMENT_NODE){  //在作结点转换之前,最好先判断结点类型book  = (Element)node;}book.setAttribute("name", "yyyyyyy");book.setAttribute("password", "123");book.removeAttribute("password");TransformerFactory tf = TransformerFactory.newInstance();Transformer ts = tf.newTransformer();ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));}


遍历(递归):

//遍历public static void list(Node node){if(node.getNodeType()==Node.ELEMENT_NODE){System.out.println(node.getNodeName());}NodeList list = node.getChildNodes();for(int i=0;i<list.getLength();i++){Node child = list.item(i);list(child);}}










 

0 0
原创粉丝点击