使用dom来解析XML文件
car.xml:
<?xml version="1.0" encoding="UTF-8"?><车辆清单><汽车><车牌 出产时间="2009年">奥迪</车牌><产地>北京</产地></汽车></车辆清单>
<?xml version="1.0" encoding="UTF-8"?><!ELEMENT 车辆清单 (汽车+)><!ELEMENT 汽车 (车牌,产地)><!ELEMENT 车牌 (#PCDATA)><!ELEMENT 产地 (#PCDATA)><!ATTLIST 车牌 出产时间 CDATA #REQUIRED>
package cn.itcast.xml.parser.dom;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;public class Demo1 {public static void main(String[] args) throws Exception {//创建DOM解析器工厂[抽象工厂设计模式]DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//设置将空白字符忽略factory.setIgnoringElementContentWhitespace(true);//DOM解析器DocumentBuilder parser = factory.newDocumentBuilder();//加载XML文件,取得代表整个XML的document对象Document document = parser.parse(new File("src/cn/itcast/xml/parser/dom/car.xml"));//取得根元素Element rootElement = document.getDocumentElement();System.out.println("根元素名称:" + rootElement.getNodeName());NodeList list = rootElement.getChildNodes();System.out.println("共有" + list.getLength()+"辆汽车");for(int i=0;i<list.getLength();i++){Element element = (Element) list.item(i);String band = element.getElementsByTagName("车牌").item(0).getFirstChild().getTextContent();String place = element.getElementsByTagName("产地").item(0).getFirstChild().getTextContent();System.out.println("车牌:" + band);System.out.println("产地:" + place);System.out.println("-------------");}}}
package cn.itcast.xml.parser.dom;import java.io.File;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.NodeList;import org.w3c.dom.Text;public class Demo2 {@Testpublic void create() throws Exception{Document document = getDocument();Element rootElement = document.getDocumentElement();//创建元素Element carElement = document.createElement("汽车");//创建文本Text text = document.createTextNode("比亚迪");carElement.appendChild(text);//创建属性carElement.setAttribute("id","aaaaaa");rootElement.appendChild(carElement);writeToFile(document);}@Testpublic void update() throws Exception{Document document = getDocument();Element rootElement = document.getDocumentElement();NodeList list = rootElement.getChildNodes();Element carElement = (Element) list.item(3);//定位于第二辆汽车carElement.getElementsByTagName("产地").item(0).setTextContent("广州");writeToFile(document);}@Testpublic void delete() throws Exception{Document document = getDocument();Element rootElement = document.getDocumentElement();Element carElement = (Element) rootElement.getElementsByTagName("汽车").item(1);carElement.getParentNode().removeChild(carElement);writeToFile(document);}private Document getDocument() throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();factory.setIgnoringElementContentWhitespace(true);DocumentBuilder parser = factory.newDocumentBuilder();Document document = parser.parse(new File("src/cn/itcast/xml/parser/dom/car.xml"));return document;}private void writeToFile(Document document)throws Exception {//创建XML输出工厂TransformerFactory tf = TransformerFactory.newInstance();//创建XML输出对象Transformer t = tf.newTransformer();//源对象DOMSource source = new DOMSource(document);//目标对象StreamResult result = new StreamResult(new File("src/cn/itcast/xml/parser/dom/car.xml"));//真正输出t.transform(source,result);}}