读取xml文件后将内容打印到浏览器中

时间:2014-02-23 08:14:14

标签: java xml xmldom

我拥有的XML文件是

<xml>
    <ticket>
        <team>A</team>
        <imp>I1</imp>
    </ticket>
    <ticket>
        <team>A</team>
        <imp>I2</imp>
    </ticket>
    <ticket>
        <team>b</team>
        <imp>I2</imp>
    </ticket>
    <ticket>
        <team>A</team>
        <imp>I1</imp>
    </ticket>
    <ticket>
        <team>B</team>
        <imp>I2</imp>
    </ticket>
    <ticket>
        <team>c</team>
        <imp>I1</imp>

ticketcount.java

package com.asn.model;

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import com.asn.model.ticket;

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

import com.asn.model.ticket;

public class ticketcount {

    public static void main(String[] args) {
        List<ticket> ticketList = new ArrayList<ticket>();
        try {

            String Path = "C:\\Users\\";

            File fXmlFile = new File(Path + "\\ticket.xml");

            // File fXmlFile = new File(App.class.getClassLoader().getResource("C:\\Users\// \

            //   \tickets.xml").getFile());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList ticketNodeList = doc.getElementsByTagName("ticket");


            for (int temp = 0; temp < ticketNodeList.getLength(); temp++) {
                Node varNode = ticketNodeList.item(temp);

                if (varNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) varNode;

                    NodeList teamList = eElement.getElementsByTagName("team");
                    NodeList varsionList = eElement.getElementsByTagName("imp");

                    Node teamNode = teamList.item(0);
                    Node impNode = varsionList.item(0);

                    if (teamNode.getNodeType() == Node.ELEMENT_NODE && impNode.getNodeType() ==

                            Node.ELEMENT_NODE) {
                        Element teamElement = (Element) teamNode;
                        Element impElement = (Element) impNode;
                        ticket ticket = new ticket(teamElement.getTextContent(),

                                impElement.getTextContent());
                        ticketList.add(ticket);
                    }
                }
                String content1 =

                        "<HTML><HEAD></HEAD><TABLE><tr><td>Chain</td><td><b>Priority</b></td<td></td><td>count</td><

                                / tr > ";
                String content = "";
                File file = new File(Path + "\\result1.html");
                if (!file.exists()) {
                    file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
            }

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

        Map<ticket, Integer> count = new HashMap<ticket, Integer>();
        for (ticket c : ticketList)
            if (!count.containsKey(c))
                count.put(c, Collections.frequency(ticketList, c));

        List<String> imps = getimps(count);
        List<String> teams = getteams(count);

        StringBuilder heading = new StringBuilder("ticket \t| ");
        for (String s : imps) {
            heading.append(s);
            heading.append("\t| ");
        }

        System.out.println(heading);
        System.out.println("---------------------------------");


        for (String m : teams) {
            System.out.println(m + "\t| " + getNumOfteams(m, imps, count));
            content = content + "<tr><td>" + m + "</td><td>" + getNumOfteams(m, imps, count) + "</td></tr>";
        }
        bw.write(content1 + content + "</TABLE></HTML>");
        bw.close();

        Runtime rTime = Runtime.getRuntime();
        String url = Path + "result.html";
        //String url = "C:\\Users\\a561922\\Desktop\\TEST.html";//"D:/hi.html";
        String browser = "C:/Program Files/Internet Explorer/iexplore.exe ";
        File htmlFile = new File(url);
        Desktop.getDesktop().browse(htmlFile.toURI());

        //   Process pc = rTime.exec(browser + url);
        //    pc.waitFor();

        //Runtime.getRuntime().exec("C:\\Users\\a561922\\Desktop\\TEST.html");
    }


    private static List<String> getteams(Map<ticket, Integer> count) {
        List<String> teams = new ArrayList<String>();
        for (Map.Entry<ticket, Integer> ent : count.entrySet())
            if (!teams.contains(ent.getKey().getteam()))
                teams.add(ent.getKey().getteam());
        return teams;
    }


    private static String getNumOfteams(String team, List<String> imps, Map<ticket, Integer>

            count) {
        StringBuilder builder = new StringBuilder();


        for (String v : imps) {
            Integer cnt = count.get(new ticket(team, v));
            if (cnt == null) {
                cnt = 0;
            }
            builder.append(cnt + "\t");
        }


        return builder.toString();
    }


    private static List<String> getimps(Map<ticket, Integer> count) {
        List<String> imps = new ArrayList<String>();
        for (Map.Entry<ticket, Integer> ent : count.entrySet())
            if (!imps.contains(ent.getKey().getimp()))
                imps.add(ent.getKey().getimp());
        return imps;
    }
}

ticket.java

package com.asn.model;

public class ticket {

    private String team;
    private String imp;

    public ticket(String team, String imp) {

        super();

        this.team = team;


        this.imp = imp;
    }

    public String getteam() {
        return team;
    }

    public void setteam(String team) {

        this.team = team;
    }

    public String getimp() {
        return imp;
    }

    public void setimp(String imp) {
        this.imp = imp;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((team == null) ? 0 : team.hashCode());
        result = prime * result + ((imp == null) ? 0 : imp.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ticket other = (ticket) obj;
        if (team == null) {
            if (other.team != null)
                return false;
        } else if (!team.equals(other.team))
            return false;
        if (imp == null) {
            if (other.imp != null)
                return false;
        } else if (!imp.equals(other.imp))
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("incident [team=");
        builder.append(team);
        builder.append(", imp=");
        builder.append(imp);
        builder.append("]");
        return builder.toString();
    }
}

1 个答案:

答案 0 :(得分:1)

你可以这样做......根据你改变路径

package com.asn.model;

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class ticketcount {

    public static void main(String[] args) throws IOException {
        List<ticket> ticketList = new ArrayList<ticket>();
        String content = "";
        String content1 ="<HTML><HEAD></HEAD><TABLE border=3>";
        FileWriter fw =null;
        BufferedWriter bw=null;
        String Path = "src";


        try {



            File fXmlFile = new File(Path + "\\file.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList ticketNodeList = doc.getElementsByTagName("ticket");

            for (int temp = 0; temp < ticketNodeList.getLength(); temp++) {
                Node varNode = ticketNodeList.item(temp);

                if (varNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) varNode;

                    NodeList teamList = eElement.getElementsByTagName("team");
                    NodeList varsionList = eElement.getElementsByTagName("imp");

                    Node teamNode = teamList.item(0);
                    Node impNode = varsionList.item(0);

                    if (teamNode.getNodeType() == Node.ELEMENT_NODE
                            && impNode.getNodeType() ==

                            Node.ELEMENT_NODE) {
                        Element teamElement = (Element) teamNode;
                        Element impElement = (Element) impNode;
                        ticket ticket = new ticket(
                                teamElement.getTextContent(),

                                impElement.getTextContent());
                        ticketList.add(ticket);
                    }
                }


                File file = new File(Path + "\\result1.html");
                if (!file.exists()) {
                    file.createNewFile();
                }

                 fw = new FileWriter(file.getAbsoluteFile());
                 bw = new BufferedWriter(fw);
            }

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

        Map<ticket, Integer> count = new HashMap<ticket, Integer>();
        for (ticket c : ticketList)
            if (!count.containsKey(c))
                count.put(c, Collections.frequency(ticketList, c));

        List<String> imps = getimps(count);

        List<String> teams = getteams(count);


        content=content+"<tr><th>ticket</th> ";
        for (String s : imps) {

            content=content+"<th>"+s+"</th>";
        }

        content=content+"</tr>";

        System.out.println("---------------------------------");

        for (String m : teams) {
            System.out.println(m + "\t| " + getNumOfteams(m, imps, count));
            content = content + "<tr><td>" + m + "</td>"
                    + getNumOfteams(m, imps, count) + "</tr>";
        }
        bw.write(content1 + content + "</TABLE></HTML>");
        bw.close();

        Runtime rTime = Runtime.getRuntime();
        String url = Path + "//result1.html";
        // String url = "C:\\Users\\a561922\\Desktop\\TEST.html";//"D:/hi.html";
        String browser = "C:/Program Files/Internet Explorer/iexplore.exe ";
        File htmlFile = new File(url);
        System.out.println(url);
        Desktop.getDesktop().browse(htmlFile.toURI());

        // Process pc = rTime.exec(browser + url);
        // pc.waitFor();

        // Runtime.getRuntime().exec("C:\\Users\\a561922\\Desktop\\TEST.html");
    }

    private static List<String> getteams(Map<ticket, Integer> count) {
        List<String> teams = new ArrayList<String>();
        for (Map.Entry<ticket, Integer> ent : count.entrySet())
            if (!teams.contains(ent.getKey().getteam()))
                teams.add(ent.getKey().getteam());
        return teams;
    }

    private static String getNumOfteams(String team, List<String> imps,
            Map<ticket, Integer>

            count) {
        StringBuilder builder = new StringBuilder();

        for (String v : imps) {
            Integer cnt = count.get(new ticket(team, v));
            if (cnt == null) {
                cnt = 0;
            }
            builder.append("<td>"+cnt + "</td>");

        }

        return builder.toString();
    }

    private static List<String> getimps(Map<ticket, Integer> count) {
        List<String> imps = new ArrayList<String>();
        for (Map.Entry<ticket, Integer> ent : count.entrySet())
            if (!imps.contains(ent.getKey().getimp()))
                imps.add(ent.getKey().getimp());
        return imps;
    }
}

现在输出: -

enter image description here

我认为这就是你想要的输出......如果你遇到任何问题,请告诉我