現在、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();
}
}
}
ありがとうございました!
ためにこのコードを使用して他の人と共有したい出力?私はそれを直接コピーするので、何かが恋しいですか? – Anna
@Annaコードを修正しました。混乱させて申し訳ありません。 – anthoon