高级特性六|XML

XML

获取整个XML文档对象
private Document document = null;
public void getDocument(){ 			
			//创建解析器工厂对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //创建解析器对象
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse("收藏信息.xml");
}
显示文档内容信息
public void shouInfo(){		
		//获取Brand标签
		NodeList brands = document.getElementsByTagName("Brand");
        for (int i = 0; i < brands.getLength(); i++) {
            Node node = brands.item(i);
            Element eleBrand = (Element) node;
            //输出nama标签的值
            System.out.println(eleBrand.getAttribute("name"));
            NodeList types = eleBrand.getChildNodes();
            for (int j = 0; j < types.getLength(); j++) {
                Node typeNode = types.item(j);
                if (typeNode.getNodeType()==Node.ELEMENT_NODE){
                    Element eleType = (Element) typeNode;
                    //输出nama标签的值
                    System.out.println(eleType.getAttribute("name"));
                }
            }
        }
}
保存xml文件
public void saveXML(String path){		    
			//创建转换器工厂
            TransformerFactory factory = TransformerFactory.newInstance();
            //设置缩进
            factory.setAttribute("indent-number","4");
            //创建转换器对象
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            //设置缩进
            transformer.setOutputProperty(OutputKeys.INDENT,"yes");
            //创建StreamReasult对象
            StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(path),"utf-				8"));
            //创建DomSource对象
            DOMSource source = new DOMSource(document);
            //开始转换
            transformer.transform(source,result);
}
添加节点
public void add(){		
		//创建Brand元素
        Element element = document.createElement("Brand");
        element.setAttribute("name","三星");
        //创建Type元素
        Element typee = document.createElement("Type");
        typee.setAttribute("name","Note10");
        //将Type元素放入Brand中
        element.appendChild(typee);
        //将Brand元素存入根节点中
        document.getElementsByTagName("PhoneInfo").item(0).appendChild(element);
        this.saveXML("new.xml");
}
修改节点
public void update(){		
		NodeList brands = document.getElementsByTagName("Brand");
        for (int i = 0; i < brands.getLength(); i++) {
            Node brand = brands.item(i);
            Element eleBrand = (Element) brand;
            if (eleBrand.getAttribute("name").equalsIgnoreCase("三星")){
                eleBrand.setAttribute("name","OPPO");
            }
        }
        this.saveXML("new.xml");
}
删除节点
public void delete(){		
		NodeList brands = document.getElementsByTagName("Brand");
        for (int i = 0; i < brands.getLength(); i++) {
            Node brand = brands.item(i);
            Element eleBrand = (Element) brand;
            if (eleBrand.getAttribute("name").equalsIgnoreCase("OPPO")){
                eleBrand.getParentNode().removeChild(eleBrand);
            }
        }
        this.saveXML("new.xml");
}

DOM4J1-6-1

获取整个xml文档
public void loadDocument(){ 			
			SAXReader saxReader = new SAXReader();
            File url = new File("E:\\javaWorkSpace\\高级特效\\Dom4j1.6.1\\src\\收藏信息.xml");
            doc = saxReader.read(url);
}
显示所有信息
public void shouInfo(){		
		Element root = doc.getRootElement();
        Iterator iterator = root.elementIterator();
        while (iterator.hasNext()){
            Element brandEle = (Element) iterator.next();
            String brandStr = brandEle.attributeValue("name");
            Iterator itType = brandEle.elementIterator();
            while (itType.hasNext()){
                Element typeEle = (Element) itType.next();
                String typeStr = typeEle.attributeValue("name");
                System.out.println("手机品牌"+brandStr+",型号:"+typeStr);

            }
        }
}
保存xml文件
public void savexml(String path){		
			OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("gb2312");
            XMLWriter writer = new XMLWriter(new FileWriter(path),format);
            writer.write(doc);
            writer.close();
}
添加节点
public void addPhoneInfo(){		
		Element root = doc.getRootElement();
        //创建一个Brand节点
        Element brandEle = root.addElement("Brand");
        brandEle.addAttribute("name","三星");
        //创建Type节点
        Element TypeEle = brandEle.addElement("Type");
        TypeEle.addAttribute("name","Note10");
        this.savexml("new收藏.xml");
}
修改节点
public void updatePhoneInfo(){		
		Element root = doc.getRootElement();
        Iterator itBrand = root.elementIterator();
        while (itBrand.hasNext()){
            Element brandEle = (Element) itBrand.next();
            if (brandEle.attributeValue("name").equals("三星")){
                brandEle.setAttributeValue("name","SUMSUNG");
            }
        }
        this.savexml("new收藏.xml");
}
删除节点
public void deletePhoneInfo(){
		Element root = doc.getRootElement();
        Iterator itBrand = root.elementIterator();
        while (itBrand.hasNext()){
            Element brandEle = (Element) itBrand.next();
            if (brandEle.attributeValue("name").equals("三星")){
                brandEle.getParent().remove(brandEle);
            }
        }
        this.savexml("new收藏.xml");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值