对xml排序的java代码

场景

业务中测试加密后的报文。接收和转发的xml不一致(接收后对xml进行了处理,处理后可能进行转发)。排序后方便对比差异。

功能

  1. 将xml转换为json,打印到控制台。json已排序
  2. 将json转换为xml;排序的

不足

  1. 可以排序的文件/xml大小没有确认。代码中使用xml的大小是400行
  2. 没有保存xml的attribute
  3. xml的格式化不是飘零
  4. 格式化使用空格而非制表符
  5. 没有保存转换后的json到文件,只打印
  6. 输入和输出的xml文件没有头<?xml version="1.0" encoding="UTF-8"?>
  7. 排序规则为xml元素的字典排序

代码

package com.asiainfo.iom.interfaces.other;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


public class XMLParseTest {

    @Test
    public void testParse() throws JDOMException, IOException {
        SAXBuilder sax = new SAXBuilder();
        File file = new File("path/to/input.xml");
        FileInputStream fis = new FileInputStream(file);
        Document root = sax.build(fis);
        Node node = convertToNode(root.getRootElement());
        System.out.println(node);
        String str = convertToXml(node, "");
        System.out.println(str);
        File outfile = new File("path/to/output.xml");
        FileOutputStream fos = new FileOutputStream(outfile);
        fos.write(str.getBytes(StandardCharsets.UTF_8));
    }

    private static final String space = "  ";
    private String convertToXml(Node node, String intend) {
        StringBuilder sb = new StringBuilder();
        sb.append(intend).append("<").append(node.getTagName()).append(">");
        if (node.getChildren().isEmpty()) {
            sb.append(node.getContent());
        } else {
            sb.append("\n");
            node.getChildren().sort(Comparator.comparing(Node::getTagName));
            for (Node child : node.getChildren()) {
                sb.append(convertToXml(child, intend + space));
            }
        }
        sb.append(intend).append("</").append(node.getTagName()).append(">\n");
        return sb.toString();
    }

    private Node convertToNode(Element root) {
        Node node = new Node();
        List<Element> children = root.getChildren();
        for (Element child : children) {
            node.getChildren().add(convertToNode(child));
        }
        node.setTagName(root.getName());
        node.setContent(root.getTextTrim());
        return node;
    }

    static String xmlStr = "";
}

class Node {
    String tagName;
    String content;
    List<Node> children = new ArrayList<>();

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        this.getChildren().sort(Comparator.comparing(Node::getTagName));
        StringBuilder sb = new StringBuilder();
        sb.append("'").append(this.tagName).append("'").append(":").append("{");

        for (int i = 0; i < children.size(); i++) {
            Node child = children.get(i);
            if(child.getChildren().isEmpty()) {
                sb.append("'").append(child.getTagName()).append("'").append(":").append("'").append(child.getContent()).append("'");
                if (i != children.size() - 1) {
                    sb.append(",");
                }
            } else {
                sb.append(child.toString());
            }
        }
        sb.append("}");
        return sb.toString();
    }
}

posted @ 2021-03-19 18:20  漫步ing  阅读(168)  评论(0编辑  收藏  举报