Java uses dom to parse and manipulate xml, and recursively parse xml to output in yml format

topic

1. Write a program to parse the score.xml file, and the required output result is shown in Figure 1.

2. Write the program, modify the following XML file book.xml, requirements:

(1) Add an author sub-element for each book element, and the content of the element is customized.

(2) Modify the value of the title element and add the text "(Electronic Industry Press)" after the text content. E.g:

<title> Java object-oriented programming</title> becomes: <title> Java object-oriented programming (Electronics Industry Press)</title>

(3) Delete the original third <book> element.

(4) Add a category attribute sub-element for each book element, the content of the element is customized, such as: <book category="computer">

(5) Use DOM interface, javascript language implementation.

score.xml

<?xml version="1.0" encoding="UTF-8"?>
<score>
	<name id="001" rand="358">
	    <alias>张三</alias>
		<math>89</math>
		<english>90</english>
	</name>
	<name id="002" rand="sdg">
		<alias test="bjkzxv" class="dsvskl">李四</alias>
		<math>92</math>
		<english>87</english>
	</name>
	<name id="003" rand="sdgsd">
		<alias>王五</alias>
		<math>95</math>
		<english>90</english>
	</name>
</score>

book.xml

<?xml version="1.0" encoding="utf-8" ?>
<books>
	<book>
		<title>Java面向对象编程</title>
	</book>
	<book>
		<title>JSP动态网页编程技术</title>
	</book>
	<book>
		<title>精通Hibernate</title>
	</book>
</books>

Effect screenshot

Parse book1.xml and output it in the console in yml format

 

Code

XmlOperator.java

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
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.OutputKeys;
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 java.io.File;
import java.io.IOException;

/**
 * @author passerbyYSQ
 * @create 2020-12-01 10:34
 */
public class XmlOperator {

    /**
     * 读取xml文件,创建document对象
     *
     * @param file
     * @return
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static Document createDocument(File file) throws ParserConfigurationException,
            IOException, SAXException {
        // 获取工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // 获取builder
        DocumentBuilder builder = factory.newDocumentBuilder();
        // 解析xml文件
        Document document = builder.parse(file);
        return document;
    }

    /**
     * 将document对象保存成xml文件
     *
     * @param document
     * @param dest
     * @throws TransformerException
     */
    public static void saveToXmlFile(Document document, File dest) throws TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        // 设置缩进
        //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // 保存位置
        StreamResult result = new StreamResult(dest);
        //把DOM树转换为xml文件并保存
        transformer.transform(domSource, result);
    }

    /**
     * 解析xml文件,以yml格式打印在控制台
     *
     * @param document
     * @param outerNodeName
     */
    public static void toYml(Document document, String outerNodeName) {

        // 最外层结点(一般来说,根节点只有一个。但这里允许最外层结点有多个并列)
        NodeList outerNodeList = document.getElementsByTagName(outerNodeName);

        for (int i = 0; i < outerNodeList.getLength(); i++) {
            Node outNode = outerNodeList.item(i);
            String nodeName = outNode.getNodeName();
            System.out.print(nodeName);

            // 拼接属性
            NamedNodeMap attributes = outNode.getAttributes();
            String attrsStr = joinAttributes(attributes);
            System.out.print(attrsStr);

            // 递归遍历子节点
            NodeList childNodes = outNode.getChildNodes();
            parseByRecursion(childNodes, 1);
        }
    }

    // 递归解析子节点
    private static void parseByRecursion(NodeList nodeList, Integer level) {
        if (nodeList == null || nodeList.getLength() == 0) {
            return;
        }

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            if (node.getNodeType() == Node.TEXT_NODE) {
                // Node.TEXT_NODE的getTextContent()和getNodeValue()一样
                // Node.ELEMENT_NODE的getTextContent()是当前标签里面的xml代码,getNodeValue()为null,
                String text = node.getTextContent();

                if (!"".equals(text.trim())) {
                    System.out.print(": " + text);
                }
            } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                String nodeName = node.getNodeName();
                System.out.println();
                for (int j = 0; j < level; j++) {
                    System.out.print("\t");
                }
                System.out.print(nodeName);

                // 拼接属性
                NamedNodeMap attributes = node.getAttributes();
                String attrsStr = joinAttributes(attributes);
                System.out.print(attrsStr);

                // 递归
                parseByRecursion(node.getChildNodes(), level + 1);
            }
        }
    }

    // 拼接属性
    private static String joinAttributes(NamedNodeMap attributes) {
        if (attributes.getLength() == 0) {
            return "";
        }

        StringBuilder sbd = new StringBuilder();
        sbd.append("[");
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            sbd.append(attr.getNodeName())
                    .append("=")
                    .append(attr.getNodeValue());
            if (i < attributes.getLength() - 1) {
                sbd.append(",");
            }
        }
        sbd.append("]");
        return sbd.toString();
    }


}

TestWork.java

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;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.*;
import java.net.URL;
import java.util.UUID;

/**
 * @author passerbyYSQ
 * @create 2020-12-01 10:55
 */
public class TestWork {

    private static Document document;

    static {
        // 获取工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        File file = new File("E:\\Java\\IdeaProjects\\XmlOparate\\src\\demo\\book.xml");

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(file);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        XmlOperator.toYml(document, "books");
    }

    /**
     * 编写程序,修改下面的XML文件Book.xml,要求:
     * (1)为每一个book元素添加一个author子元素,元素内容自定。
     * (2)修改title元素的值,在文本内容的后面添加文本"(电子工业出版社)"。    例如:
     * 	<title> Java面向对象编程</title>      变为:
     * 	<title> Java面向对象编程(电子工业出版社)</title>
     *  (3)删除原有的第三个<book>元素。
     *  (4)为每一个book元素添加一个category属性子元素,元素内容自定,如:<book  category="computer">
     *  (5)使用DOM接口,javascript语言实现。
     */
    @Test
    public void test3() throws TransformerException {
        //(1)为每一个book元素添加一个author子元素,元素内容自定。
        //(4)为每一个book元素添加一个category属性子元素,元素内容自定,如:<book  category="computer">
        NodeList bookNodeList = document.getElementsByTagName("book");
        for (int i = 0; i < bookNodeList.getLength(); i++) {
            Node bookNode = bookNodeList.item(i);
            // 创建author子节点
            Element authorNode = document.createElement("author");
            authorNode.setTextContent("author" + i);
            // 添加子节点
            bookNode.appendChild(authorNode);

            Element element = (Element) bookNode;
            element.setAttribute("category", "cate" + i);//设置属性load="false"
        }

        // (2)修改title元素的值,在文本内容的后面添加文本"(电子工业出版社)"
        NodeList titleNodeList = document.getElementsByTagName("title");
        for (int i = 0; i < titleNodeList.getLength(); i++) {
            Node titleNode = titleNodeList.item(i);
            String textContent = titleNode.getTextContent();
            //System.out.println(textContent);
            titleNode.setTextContent(textContent + "(电子工业出版社)");
        }

        //(3)删除原有的第三个<book>元素。
        Node item = bookNodeList.item(2);
        item.getParentNode()
                .removeChild(item);

        XmlOperator.toYml(document, "books");

        File destFile = new File("E:\\Java\\IdeaProjects\\XmlOparate\\src\\demo\\book1.xml");
        XmlOperator.saveToXmlFile(document, destFile);
    }

}

 

Guess you like

Origin blog.csdn.net/qq_43290318/article/details/110436888