java编写大数据分析模型

Earlier we learned how to read XML file and how to edit XML file in java using DOM Parser, today we will learn how to write an XML file in java using DOM Parser.

之前我们学习了如何使用DOM分析器读取XML文件以及如何在Java中编辑XML文件 ,今天我们将学习如何使用DOM分析器用Java语言编写XML文件。

Here is the requirement for our XML file.

这是我们XML文件的要求。

  1. The root element will be “Employees” with namespace “https://www.journaldev.com/employee”. This root element will contain the list of Employees.

    根元素将是名称空间为“ https://www.journaldev.com/employee”的“ Employees”。 该根元素将包含“雇员”列表。
  2. The employee information will be written in “Employee” element. There will be two employees information in the XML file.

    员工信息将写在“员工”元素中。 XML文件中将有两个员工信息。
  3. Every employee has an attribute named “id”

    每个员工都有一个名为“ id”的属性
  4. Employee element will have four elements – “name”, “age”, “role”, “gender”.

    雇员元素将包含四个元素-“姓名”,“年龄”,“角色”,“性别”。

Here is the java program for above requirement.

这是上述要求的Java程序。

package com.journaldev.xml;


import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;


public class XMLWriterDOM {

    public static void main(String[] args) {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.newDocument();
            //add elements to Document
            Element rootElement =
                doc.createElementNS("https://www.journaldev.com/employee", "Employees");
            //append root element to document
            doc.appendChild(rootElement);

            //append first child element to root element
            rootElement.appendChild(getEmployee(doc, "1", "Pankaj", "29", "Java Developer", "Male"));

            //append second child
            rootElement.appendChild(getEmployee(doc, "2", "Lisa", "35", "Manager", "Female"));

            //for output to file, console
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            //for pretty print
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(doc);

            //write to console or file
            StreamResult console = new StreamResult(System.out);
            StreamResult file = new StreamResult(new File("/Users/pankaj/emps.xml"));

            //write data
            transformer.transform(source, console);
            transformer.transform(source, file);
            System.out.println("DONE");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static Node getEmployee(Document doc, String id, String name, String age, String role,
            String gender) {
        Element employee = doc.createElement("Employee");

        //set id attribute
        employee.setAttribute("id", id);

        //create name element
        employee.appendChild(getEmployeeElements(doc, employee, "name", name));

        //create age element
        employee.appendChild(getEmployeeElements(doc, employee, "age", age));

        //create role element
        employee.appendChild(getEmployeeElements(doc, employee, "role", role));

        //create gender element
        employee.appendChild(getEmployeeElements(doc, employee, "gender", gender));

        return employee;
    }


    //utility method to create text node
    private static Node getEmployeeElements(Document doc, Element element, String name, String value) {
        Element node = doc.createElement(name);
        node.appendChild(doc.createTextNode(value));
        return node;
    }

}

Notice that I have created two StreamResult, one to print XML in console for debugging purpose and another to write it to file.

请注意,我已经创建了两个StreamResult ,一个用于在控制台中打印XML以进行调试,另一个用于将XML写入文件。

Here is the output XML from above program.

这是上述程序的输出XML。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees xmlns="https://www.journaldev.com/employee">
<Employee id="1">
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
<Employee id="2">
<name>Lisa</name>
<age>35</age>
<role>Manager</role>
<gender>Female</gender>
</Employee>
</Employees>

The XML is not formatted, if you want XML to be properly formatted, read format XML in java.

XML未格式化,如果您希望XML正确格式化,请阅读java中的format XML

翻译自: https://www.journaldev.com/1112/how-to-write-xml-file-in-java-dom-parser

java编写大数据分析模型

Logo

永洪科技,致力于打造全球领先的数据技术厂商,具备从数据应用方案咨询、BI、AIGC智能分析、数字孪生、数据资产、数据治理、数据实施的端到端大数据价值服务能力。

更多推荐