XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<书架>
<书>
<书名>数据库</书名>
<作者>redarmy</作者>
<售价>40元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>redarmy</作者>
<售价>38元</售价>
</书>
</书架>
SAX解析:
package cn.csdn.com;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class saxXMl {
public static void main(String[] args) throws Exception {
//创建工厂
SAXParserFactory spf = SAXParserFactory.newInstance();
//创建sax解析器
SAXParser sp = spf.newSAXParser();
//获取读取器
XMLReader xmlReader = sp.getXMLReader();
//设置事件处理器
xmlReader.setContentHandler(new Book());
//解析xml文件
xmlReader.parse("src/book.xml");
}
}
class Book extends DefaultHandler{
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("<"+qName+">");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("<"+qName+">");
}
}
DOM解析:
package cn.csdn.com;
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 javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Domallnode {
/**
* 对book.xml文件进行CRUD操作
*/
/*遍历所有的节点*/
public void read() throws Exception{
//1.创建工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.创建解析器
DocumentBuilder db= factory.newDocumentBuilder();
// 3.获取document对象,对xml解析
Document document = db.parse("src/book.xml");
//得到根节点
Node root = document.getElementsByTagName("书架").item(0);
List(root);
}
private void List(Node node) {
Node child;
if (node instanceof Element)
System.out.println(node.getNodeName());
NodeList list=node.getChildNodes();
for(int i =0;i<list.getLength();i++){
child = list.item(i);
List(child);
}
}
//读取属性的值
@Test
public void read1() throws Exception{
//1.创建工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.创建解析器
DocumentBuilder db= factory.newDocumentBuilder();
// 3.获取document对象,对xml解析
Document document = db.parse("src/book.xml");
//得到根节点
Element node = (Element) document.getElementsByTagName("书名").item(0);
String value = node.getAttribute("id");
System.out.println(value);
}
//添加节点
@Test
public void insert() throws Exception{
//1.创建工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.创建解析器
DocumentBuilder db= factory.newDocumentBuilder();
// 3.获取document对象,对xml解析
Document document = db.parse("src/book.xml");
//创建节点
Element element = document.createElement("优惠价");
element.setTextContent("29元");
//把创建出来的节点添加到第一本书上
Element book= (Element) document.getElementsByTagName("书").item(0);
//追加到第一本书上
book.appendChild(element);
/*
Transformer transformer=TransformerFactory.newInstance().newTransformer();
DOMSource source=new DOMSource(document);
FileOutputStream outstream =new FileOutputStream(new File("src/book.xml"));
StreamResult reslut=new StreamResult(outstream);
transformer.transform(source, reslut);
outstream.close();*/
TransformerFactory transformer=TransformerFactory.newInstance();
Transformer tf= transformer.newTransformer();
tf.transform(new DOMSource(document),new StreamResult( new FileOutputStream(new File("src/book.xml"))));
}
//删除节点
@Test
public void delete() throws Exception{
//1.创建工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.创建解析器
DocumentBuilder db= factory.newDocumentBuilder();
// 3.获取document对象,对xml解析
Document document = db.parse("src/book.xml");
//获取要删除的节点
Element element = (Element) document.getElementsByTagName("优惠价").item(0);
/* //获取到要删除的节点的父节点
Element parent = (Element) element.getParentNode();
parent.removeChild(element);*/
//删除正本书的内容
element.getParentNode().getParentNode().removeChild(element.getParentNode());
/*
Transformer transformer=TransformerFactory.newInstance().newTransformer();
DOMSource source=new DOMSource(document);
FileOutputStream outstream =new FileOutputStream(new File("src/book.xml"));
StreamResult reslut=new StreamResult(outstream);
transformer.transform(source, reslut);
outstream.close();*/
TransformerFactory transformer=TransformerFactory.newInstance();
Transformer tf= transformer.newTransformer();
tf.transform(new DOMSource(document),new StreamResult( new FileOutputStream(new File("src/book.xml"))));
}
//更新节点
@Test
public void update() throws Exception{
//1.创建工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.创建解析器
DocumentBuilder db= factory.newDocumentBuilder();
// 3.获取document对象,对xml解析
Document document = db.parse("src/book.xml");
//获取要更新的节点
Element element = (Element) document.getElementsByTagName("售价").item(0);
element.setTextContent("38元");
TransformerFactory transformer=TransformerFactory.newInstance();
Transformer tf= transformer.newTransformer();
tf.transform(new DOMSource(document),new StreamResult( new FileOutputStream(new File("src/book.xml"))));
}
}