• 欢迎光临~

DOM文件

开发技术 开发技术 2022-07-24 次浏览
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
    <student id="1">
        <name>王同</name>
        <class>java</class>
        <score>89</score>
    </student>
    <student id="2">
        <name>李佳</name>
        <class>sql</class>
        <score>58</score>
    </student>
    <student id="3">
        <name>李白</name>
        <class>python</class>
        <score>90</score>
    </student>
</students>

 

        <!ELEMENT students (student+)>
        <!ELEMENT student (name,class,score)>
        <!ATTLIST student id CDATA #IMPLIED>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT class (#PCDATA)>
        <!ELEMENT score (#PCDATA)>

 

public class TestXML {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        //1.得到document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
        //2.解析和加载xml文件到内存中去
        Document document = documentBuilder.parse("student.xml");
        //3.返回文档中的所有子节点
        NodeList nodeList = document.getChildNodes();
        Node root = nodeList.item(nodeList.getLength() - 1);
        System.out.println("root:" + root.getNodeName());//students
        //4.再查找他的子节点
        NodeList childNodes = root.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if ("student".equals(item.getNodeName())) {
                NodeList nodes = item.getChildNodes();
                for (int j = 0; j < nodes.getLength(); j++) {
                    Node node = nodes.item(j);
                    if ("name".equals(node.getNodeName())) {
                        System.out.println("name:" + node.getTextContent());
                    } else if ("class".equals(node.getNodeName())) {
                        System.out.println("class:" + node.getTextContent());
                    } else if ("score".equals(node.getNodeName())) {
                        System.out.println("score:" + node.getTextContent());
                    }
                }
            }
        }
    }
}

 

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;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

public class UpdateXML {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //使用DOM对XML文件进行数据修改
        //1.得到document
        DocumentBuilderFactory dbf;
        dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
        //解析和加载xml文件到内存中去
        Document document = documentBuilder.parse("student.xml");
        //1.删除某个节点
//        NodeList list = document.getElementsByTagName("student");
//        for (int i = 0; i < list.getLength(); i++) {
//            Element item = (Element)list.item(i);
//            //node:节点(文本节点),element:元素
//            //凡是元素都是节点,但是节点不一定都是元素,例如:一个单纯的回车也是节点
////            System.out.println(item.getAttribute("id"));
//            if ("2".equals(item.getAttribute("id"))){
//                item.getParentNode().removeChild(item);
//            }
//        }


        //2.添加节点
//        //student(name class score)
//        Element student = document.createElement("student");
//        student.setAttribute("id", "3");//设置属性值
//        //name
//        Element stuName = document.createElement("name");//创建节点
//        stuName.setTextContent("李白");//设置节点内容
//        student.appendChild(stuName);//添加节点
//        //class
//        Element stuClass = document.createElement("class");
//        stuClass.setTextContent("python");
//        student.appendChild(stuClass);
//        //score
//        Element stuScore = document.createElement("score");
//        stuScore.setTextContent("90");
//        student.appendChild(stuScore);
//        //找到根节点students
//        NodeList nodeList = document.getElementsByTagName("students");
//        Node students = nodeList.item(nodeList.getLength() - 1);
//        students.appendChild(student);

        //修改:李白-》李清照
        //1.查找节点
        NodeList student3 = document.getElementsByTagName("student");
        for (int i = 0; i < student3.getLength(); i++) {
            Element item = (Element)student3.item(i);
            if ("3".equals(item.getAttribute("id"))){
                Node name = item.getElementsByTagName("name").item(0);
                //2.修改节点内容
                name.setTextContent("李清照");
                break;
            }
        }


        //保存成XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        // 设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        StreamResult result = new StreamResult(new FileOutputStream("student.xml"));
        // 把DOM树转换为XML文件
        transformer.transform(domSource, result);

    }
}

 

程序员灯塔
转载请注明原文链接:DOM文件
喜欢 (0)