package com.hbsi.xml;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.crypto.dsig.Transform;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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;
import org.xml.sax.SAXException;
public class Demo {
@Test
public void read(){
try {
//创建工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//通过创建的工厂,创建解析器
DocumentBuilder db=factory.newDocumentBuilder();
//获取document对象,对xml进行解析
Document document=db.parse("src/Country.xml");
//获取根节点
NodeList list=document.getElementsByTagName("中国");
Node root=list.item(0);
list(root);
} catch (Exception e) {
e.printStackTrace();
}
}
private void list(Node root) {
//判断是否是指定的类型element,如果是就输出
if(root instanceof Element){
System.out.println(root.getNodeName());
}
NodeList list=root.getChildNodes();
for(int i=0;i<list.getLength();i++){
Node child=list.item(i);
//使用递归
list(child);
}
}
//读取属性的值
@Test
public void read2(){
try {
//创建工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//创建解析器
DocumentBuilder db=factory.newDocumentBuilder();
//获取document对象,对xml进行解析
Document document=db.parse("src/Country.xml");
Element element=(Element) document.getElementsByTagName("省份").item(1);
String value=element.getAttribute("省名");
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
//添加新节点
@Test
public void insert() throws ParserConfigurationException, SAXException, IOException, TransformerException{
//创建工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//通过创建的工厂,创建解析器
DocumentBuilder db=factory.newDocumentBuilder();
//获取document对象,对xml进行解析
Document document=db.parse("src/Newspaper.xml");
//创建新的节点
Element element=document.createElement("作者");
//对新的节点进行内容设置
element.setTextContent("张三");
//把创建出来的节点添加到ARTICLE中
Element author=(Element) document.getElementsByTagName("ARTICLE").item(0);
author.appendChild(element);
//把添加的节点写的xml文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Newspaper.xml")));
}
//在指定的位置上添加节点
@Test
public void insert2(){
try{
//创建工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//通过创建的工厂,创建解析器
DocumentBuilder db=factory.newDocumentBuilder();
//获取document对象,对xml进行解析
Document document=db.parse("src/Newspaper.xml");
//创建新的节点
Element element=document.createElement("编号");
element.setTextContent("王五");
//获取参考节点
Element notes=(Element) document.getElementsByTagName("NOTES").item(0);
//把创建出来的节点添加到ARTICLE上
Element newspaper=(Element) document.getElementsByTagName("ARTICLE").item(0);
newspaper.insertBefore(element, notes);
//把添加的节点写到xml文件中
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Newspaper.xml")));
}catch(Exception e){
e.printStackTrace();
}
}
//更新节点
@Test
public void update(){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder db=factory.newDocumentBuilder();
Document document=db.parse("src/Newspaper.xml");
//获取要更新的节点
Element id=(Element) document.getElementsByTagName("编号").item(0);
//要更新的内容
id.setTextContent("12");
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Newspaper.xml")));
}catch(Exception e){
e.printStackTrace();
}
}
//删除节点
@Test
public void delete(){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder db=factory.newDocumentBuilder();
Document document=db.parse("src/Newspaper.xml");
//获取要删除的节点
Element e=(Element) document.getElementsByTagName("作者").item(0);
//获取要删除节点的父节点
Element parent=(Element) e.getParentNode();
parent.removeChild(e);
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Newspaper.xml")));
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.hbsi.xml;
import javax.xml.parsers.ParserConfigurationException;
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 Demo2 {
public static void main(String[] args){
try {
//创建工厂
SAXParserFactory spf=SAXParserFactory.newInstance();
//创建SAX解析器
SAXParser sp=spf.newSAXParser();
//获取读取器
XMLReader xmlreader=sp.getXMLReader();
//设置事件处理器
xmlreader.setContentHandler(new CountryContHandler());
xmlreader.parse("src/Country.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
class CountryContHandler extends DefaultHandler{
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("<"+qName+">");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("<"+qName+">");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
}