Java 怎么使用DOM生成XML间隔示例(interval sample)

2019年7月29日19:43:52Java 怎么使用DOM生成XML间隔示例(interval sample)已关闭评论 677 1976字阅读6分35秒

1.简介

Java 怎么使用DOM生成XML间隔示例(interval sample)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 


import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    public static void main(String[] argv) throws Exception {
        double time = 2.45678;
        long commits = 2;
        long transactions = 2;
        long commitMemRefs = 2;
        long totalMemRefs = 2;
        String hardware = "tah1986.com";
        boolean finalStats = true;
        generateXMLIntervalSample(time, commits, transactions,
                commitMemRefs, totalMemRefs, hardware, finalStats);
    }

    static Document doc;

    static void generateXMLIntervalSample(double time, long commits,
            long transactions, long commitMemRefs, long totalMemRefs,
            String hardware, boolean finalStats) {
        Element element;
        Element root = doc.getDocumentElement();

        Element me;
        if (finalStats)
            me = doc.createElement("FinalSample");
        else
            me = doc.createElement("Sample");
        root.appendChild(me);
        root = me;

        element = doc.createElement("Timestamp");
        element.setTextContent(Double.toString(time));
        root.appendChild(element);

        element = doc.createElement("Hardware");
        element.setTextContent(hardware);
        root.appendChild(element);

        
        element = doc.createElement("DataAvailable");
        element.setTextContent("True");
        root.appendChild(element);

        element = doc.createElement("Transactions");
        element.setTextContent(Long.toString(transactions));
        root.appendChild(element);

        element = doc.createElement("Commits");
        element.setTextContent(Long.toString(commits));
        root.appendChild(element);

        String pc;
        if (commits != 0 && transactions != 0) {
            pc = Long.toString(100 * commits / transactions);
        } else {
            pc = "0";
        }
        element = doc.createElement("PercentCommits");
        element.setTextContent(pc);
        root.appendChild(element);

        element = doc.createElement("MemRefs");
        element.setTextContent(Long.toString(totalMemRefs));
        root.appendChild(element);

        element = doc.createElement("CommitMemRefs");
        element.setTextContent(Long.toString(commitMemRefs));
        root.appendChild(element);

        String pm;
        if (commitMemRefs != 0 && totalMemRefs != 0) {
            pm = Long.toString(100 * commitMemRefs / totalMemRefs);
        } else {
            pm = "0";
        }
        element = doc.createElement("PercentCommitMemRefs");
        element.setTextContent(pm);
        root.appendChild(element);
    }
}

 
继续阅读
编程十万个怎么办