2017-05-08 8 views
0

現在、java(ネオン・エクリプス)を使用してXMLファイルを読み取り中です。私は正常にそれをやったが、私はここに2つの問題があります:Javaを使用してXMLファイルを読み取った後に平均を計算します。

1)私はまた、XMLファイルとjavaを削除しようとします。 OK、正直言って私はちょうどGoogleのコードを参照しています。何がありますか?NodeList do

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

} 

ありがとうございました!

答えて

0
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)を追加しました。私はに基づいて@anthoonの答えの一部を編集した

+0

ためにこのコードを使用して他の人と共有したい出力?私はそれを直接コピーするので、何かが恋しいですか? – Anna

+0

@Annaコードを修正しました。混乱させて申し訳ありません。 – anthoon

0

私はもちろんの平均を見つけるために、courseCodeに対して、すべてのマークを保持するために別のマップ(courseCodeToMarkList)を追加した

・ホープこのことができます私が欲しいと私はMatricNoとcourseCodeラインの平均マークのための出力がないのはなぜ良い:)

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

} 
関連する問題