使用java

时间:2017-05-08 04:56:14

标签: java xml

我目前正在使用java(neon eclipse)阅读XML文件。我已经成功完成了,但我在这里遇到了两个问题:

1)我尝试删除XML文件和java。好吧,说实话,我只是在Google中引用编码而我不知道, 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();
}
  }

}

非常感谢你!

2 个答案:

答案 0 :(得分: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)来保存所有标记,而不是找到一名学生。

我添加了另一个地图(courseCodeToMarkList)来保存针对courseCode的所有标记以查找课程的平均值

希望这有帮助

答案 1 :(得分:0)

我根据我想要的输出编辑了一些@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();
    }
}

}