gpt4 book ai didi

java - 计算 xml 中的值

转载 作者:行者123 更新时间:2023-12-01 13:22:29 25 4
gpt4 key购买 nike

我有一个 XML 文件

      <?xml version="1.0"?>
<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>
</ticket>

输出喜欢:-

 Team     I1  I2   I3
A 2 1 0
B 1 1 0
C 1 0 0

目前 XML 中不存在 I3。是否可以将 I1、I2 和 I3 放入哈希中并检查 I1 是否存在,然后获得符合团队的计数并如上所示显示。

ticket.java

 package com.asn.ticket;

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("ticket [team=");
builder.append(team);
builder.append(", imp=");
builder.append(imp);
builder.append("]");
return builder.toString();
}


}

ticketcounts.java

       package com.asn.team;



import com.asn.team.ticket;

public class ticketsCount {

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

String Path = "C:\\Users";


File fXmlFile = new File(Path+"\\tickets.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);
}
}
}
}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));
}

}

/**
* @param count
* @return
*/
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){

for(Map.Entry<ticket, Integer> ent : count.entrySet())
if(team.equals(ent.getKey().getteam()) && ent.getKey().getimp().equals(v)){
int cnt = ent.getValue();
builder.append((cnt==0)?" ": cnt);
builder.append("\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;
}

}

最佳答案

我还没有执行你的代码,但如果我理解正确的话,它工作正常,除非你没有任何 I3 列,因为 XML 文件中没有票证将 I3 作为其 imp。所以你只需要做就好像文件中有 I3 一样:

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());
}
}

// make sure I3 is listed as imp as well:
if (!imps.contains("I3")) {
imps.add("I3");
}

return imps;
}

在 getNumOfTeams() 方法中,您不应假设至少一张票具有当前的 imp。所以我将其重写为:

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();
}

旁注:您应该真正学习并应用命名约定。不要滥用缩写。将您的 getter 和 setter 命名为 getTeam() 和 setTeam(),而不是 getteam() 和 setteam(),类以大写字母开头。

关于java - 计算 xml 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com