XML文档
<?xml version="1.0" encoding="UTF-8"?> <PhoneInfo name="小米"> <Brand name="华为"> <Type name="U8650"/> <Type name="HW123"/> <Type name="HW321"/> </Brand> <Brand name="苹果"> <Type name="iPhone4"/> </Brand> <Brand name="小米"/> </PhoneInfo>
编写java 对 XML增删改查
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; 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; /** * DOM解析 * @author ZZH * */ public class Dom { //收藏信息XML对应的Document对象 private Document document; /** * 获得DOM树,获得Document对象 */ public void getDom() { //获得解析器工厂 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); try { //根据解析工厂获取解析器 DocumentBuilder builder=factory.newDocumentBuilder(); //解析器来解析XML文件获得Document对象 document = builder.parse("收藏信息.xml"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取手机品牌及其型号 */ public void showInfo() { //以Document做起点,先拿到所有的Brand节点 NodeList brands = document.getElementsByTagName("Brand"); //遍历brands,从中拿出每一个Brand:手机品牌 for(int i=0;i<brands.getLength();i++) { Node node=brands.item(i); Element eleBrand =(Element)node; System.out.println(eleBrand.getAttribute("name")); //查找每个Brand的子节点 NodeList types = eleBrand.getChildNodes(); //遍历types,从中拿出每一个types:手机型号 for(int j=0;j<types.getLength();j++) { Node typeNode = types.item(j); if(typeNode.getNodeType() ==Node.ELEMENT_NODE) { //如果是元素节点,判断节点是什么类型的 Element typeEle = (Element)typeNode; //如果是元素节点,强转成元素节点 System.out.println("\t"+typeEle.getAttribute("name")); } } } } /** * 保存 */ public void saveXML(String site) { //转换器工厂 TransformerFactory factory=TransformerFactory.newInstance(); //格式:缩进4字节 factory.setAttribute("indent-number",4); try { //转换器 Transformer transformer =factory.newTransformer(); //指定转换的格式 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //编码格式 transformer.setOutputProperty(OutputKeys.INDENT,"YES"); //缩进开启 //源 DOMSource source =new DOMSource(document); OutputStream out =new FileOutputStream(site); StreamResult result =new StreamResult(new OutputStreamWriter(out,"UTF-8")); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * 为XML文件添加元素 */ public void addEle(String phone,String model) { //<Brand name="三星"/> Element brand = document.createElement("Brand"); brand.setAttribute("name", phone); //<Type name="NOTE2"/> Element type=document.createElement("Type"); type.setAttribute("name", model); //将Type作为Brand的子元素 brand.appendChild(type); //获取文件的PhoneInfo目录集合.item方法(下标是0),再将Brand添加到它的子元素 document.getElementsByTagName("PhoneInfo").item(0).appendChild(brand); } /** * 修改元素 加id */ public void updateEle() { //获取所有brand NodeList brands =document.getElementsByTagName("Brand"); for(int i=0;i<brands.getLength();i++) { Node brand = brands.item(i); Element brandELe =(Element)brand; brandELe.setAttribute("id",i+""); } saveXML("新收藏信息.xml"); //保存 } /** * 删除元素 */ public void deleteEle(String phone) { //获取所有的Brand NodeList brands = document.getElementsByTagName("Brand"); for(int i=0;i<brands.getLength();i++) { Node brand = brands.item(i); Element brandEle = (Element)brand; if(brandEle.getAttribute("name").equals(phone)) { brandEle.getParentNode().removeChild(brandEle); } } saveXML("新收藏信息.xml"); //保存 } /** * main方法 */ public static void main(String[] args) { Dom dom=new Dom(); //创建XML对象 dom.getDom(); //获取Document对象 dom.addEle("三星","NOTE2"); //添加手机 dom.saveXML("新收藏信息.xml"); //保存信息 dom.deleteEle("华为"); //删除手机 dom.showInfo(); //展示信息 } }
运行前
运行后