使用java读取XML文件后计算平均值

问题描述:

我正在使用java(neon eclipse)读取XML文件。我已经成功地做到了,但我在这里有两个问题:使用java读取XML文件后计算平均值

1)我尝试删除XML文件和java。好吧,说实话,我只是提到谷歌的编码,我不知道,NodeList做什么

2)我想从XML文件中的标记计算平均值。我不知道如何将它们作为一个变量,然后可以进行计算。

这是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<studentMarks> 

<student id="1001"> 
    <matricNO>S123</matricNO> 
    <courseCode>CYY502</courseCode> 
    <mark>84</mark> 
</student> 

<student id="1001"> 
    <matricNO>S123</matricNO> 
    <courseCode>CYY503</courseCode> 
    <mark>72</mark> 
</student> 

<student id="1001"> 
    <matricNO>S123</matricNO> 
    <courseCode>CYY501</courseCode> 
    <mark>90</mark> 
</student> 

<student id="1001"> 
    <matricNO>S123</matricNO> 
    <courseCode>CYY506</courseCode> 
    <mark>87</mark> 
</student> 
</studentMarks> 

这是我的Java代码:

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 

import org.w3c.dom.Element; 
import java.io.File; 

public class ReadXMLFile { 

    public static void main(String argv[]) { 

try { 

File fXmlFile = new File("C:/Users/Lenovo/Desktop/marks.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 

doc.getDocumentElement().normalize(); 

NodeList nList = doc.getElementsByTagName("student"); 

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

for (int temp = 0; temp < nList.getLength(); temp++) { 

    Node nNode = nList.item(temp); 

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

     Element eElement = (Element) nNode; 

     System.out.println("Matric Number : " + eElement.getElementsByTagName("matricNO").item(0).getTextContent()); 
     System.out.println("Course Code : " + eElement.getElementsByTagName("courseCode").item(0).getTextContent()); 
     System.out.println("Marks   : " + eElement.getElementsByTagName("mark").item(0).getTextContent()); 
     System.out.println("\n"); //to print new empty line 

     //---------------------------------calculate the average------------------------------------- 

     //give a variable to the marks : 


     //calculate the average : 


     //------------------------------------------------------------------------------------------- 
    } 

} 
System.out.println("Average Marks :"); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
    } 

} 

太谢谢你了!

import java.io.File; 
import java.util.ArrayList; 
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 ReadXMLFile { 

    public static void main(String argv[]) { 

     try { 

      File fXmlFile = new File("C:/Users/Lenovo/Desktop/marks.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile); 

      doc.getDocumentElement().normalize(); 

      NodeList nList = doc.getElementsByTagName("student"); 

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

      Map<String, List<Integer>> matricNOToMarkList = new HashMap<>(); 
      Map<String, List<Integer>> courseCodeToMarkList = new HashMap<>(); 

      for (int temp = 0; temp < nList.getLength(); temp++) { 

       Node nNode = nList.item(temp); 

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

        Element eElement = (Element) nNode; 

        String matricNO = eElement.getElementsByTagName("matricNO").item(0).getTextContent(); 
        String mark = eElement.getElementsByTagName("mark").item(0).getTextContent(); 
        String courseCode = eElement.getElementsByTagName("courseCode").item(0).getTextContent(); 

        System.out.println("Matric Number : " + matricNO); 
        System.out.println("Course Code : " + courseCode); 
        System.out.println("Marks   : " + mark); 
        System.out.println("\n"); //to print new empty line 

        // if average mark of a student is required 
        List<Integer> markList = matricNOToMarkList.get(matricNO); 

        if(markList == null) 
        { 
         markList = new ArrayList<>(); 
         matricNOToMarkList.put(matricNO, markList); 
        } 

        markList.add(Integer.parseInt(mark)); 

        // if average mark of a course is required 
        List<Integer> markList1 = courseCodeToMarkList.get(courseCode); 

        if(markList1 == null) 
        { 
         markList1 = new ArrayList<>(); 
         courseCodeToMarkList.put(courseCode, markList1); 
        } 

        markList1.add(Integer.parseInt(mark)); 
       } 

      } 

      for (String matricNo : matricNOToMarkList.keySet()) 
      { 
       List<Integer> markList = matricNOToMarkList.get(matricNo); 
       int sum = 0; 
       for (Integer mark : markList) 
       { 
        sum += mark; 
       } 
       System.out.println("Average Marks for MatricNo : " + matricNo + " is : " + (sum/markList.size())); 
      } 

      for (String courseCode : courseCodeToMarkList.keySet()) 
      { 
       List<Integer> markList = courseCodeToMarkList.get(courseCode); 
       int sum = 0; 
       for (Integer mark : markList) 
       { 
        sum += mark; 
       } 
       System.out.println("Average Marks for courseCode : " + courseCode + " is : " + (sum/markList.size())); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

我添加了一个地图(matricNOToMarkList)持有的所有标记针对基质没有发现平均一个学生。

我加入了另一张图(courseCodeToMarkList)持有的所有标记针对courseCode找到一门课程的平均

希望这有助于

+0

为什么没有输出为MatricNo和courseCode线的平均标记?我错过了什么,因为我直接复制它... – Anna

+0

@安娜我纠正了代码。对困惑感到抱歉。 – anthoon

我已经编辑基础上,一些@anthoon答案输出,我想和我想与其他人分享使用此代码为好:)

import java.io.File; 
import java.util.ArrayList; 
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 Average { 

    public static void main(String argv[]) { 

     try { 
     File fXmlFile = new File("C:/Users/Lenovo/Desktop/marks.xml"); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(fXmlFile); 

     doc.getDocumentElement().normalize(); 

     NodeList nList = doc.getElementsByTagName("student"); 

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

     Map<String, List<Integer>> matricNOToMarkList = new HashMap<>(); 
     Map<String, List<Integer>> courseCodeToMarkList = new HashMap<>(); 

     for (int temp = 0; temp < nList.getLength(); temp++) { 

      Node nNode = nList.item(temp); 

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

       Element eElement = (Element) nNode; 

       String matricNO = eElement.getElementsByTagName("matricNO").item(0).getTextContent(); 
       String mark = eElement.getElementsByTagName("mark").item(0).getTextContent(); 
       String courseCode = eElement.getElementsByTagName("courseCode").item(0).getTextContent(); 

       System.out.println("Matric Number : " + matricNO); 
       System.out.println("Course Code : " + courseCode); 
       System.out.println("Marks   : " + mark); 
       System.out.println("\n"); //to print new empty line 

       // if average mark of a student is required 
       List<Integer> markList = matricNOToMarkList.get(matricNO); 

       if(markList == null) 
       { 
        markList = new ArrayList<>(); 
        matricNOToMarkList.put(matricNO, markList); 
       } 

       markList.add(Integer.parseInt(mark)); 

       // if average mark of a course is required 
       List<Integer> markList1 = courseCodeToMarkList.get(courseCode); 

       if(markList1 == null) 
       { 
        markList1 = new ArrayList<>(); 
        courseCodeToMarkList.put(courseCode, markList1); 
       } 

       markList1.add(Integer.parseInt(mark)); 
      } 

     } 

     for (String matricNo : matricNOToMarkList.keySet()) 
     { 
      List<Integer> markList = matricNOToMarkList.get(matricNo); 
      float sum = 0; 
      for (Integer mark : markList) 
      { 
       sum += mark; 
      } 
      System.out.println("Average Marks is : " + (sum/markList.size())); 
     } 


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

}