2014-02-23 102 views
-2

XML文件我有後打印內容到瀏覽器讀取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(); 
    } 
} 
+0

但我無法打印的內容進入瀏覽器。有些地方我犯了錯誤..但無法找到哪裏和什麼 – user3280677

+0

你被評論了一部分代碼'File fXmlFile = new File ...',這使得它不能編譯。 –

+0

它在那裏String Path =「C:\\ Users \\」; File fXmlFile = new File(Path +「\\ ticket.xml」); 我沒有評論:( – user3280677

回答

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

我想這就是你想要什麼樣的輸出...讓我知道如果u面對任何問題

+0

我會試着改變這個...有沒有什麼機會能夠以表格的形式顯示.. – user3280677

+0

Naren對不起,我認爲我是請求更多..但它是字符串內容我們如何可以顯示爲表格結構..請幫助 – user3280677

+0

嗨有一個更多的小疑問在這裏上面的代碼我硬編碼爲鏈,優先級計數,但實際上Headin應該StringBuilder hea ding = new StringBuilder(「ticket \ t |」) 「); for(String s:imps){ heading.append(s); heading.append(」\ t |「); 我們如何在HTML標題中添加此項 – user3280677