import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Xiti5{
public static void main(String args[]){
GiveData give=new GiveData();
try { DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder domPaser=dbf.newDocumentBuilder();
Document document=domPaser.parse(new File("xiti5.xml")) ;
NodeList nodeList=document.getChildNodes();
give.output(nodeList);
System.out.println("数学平均:"+give.average1/give.m1+"物理平均:"+give.average2/give.m2);
}
catch(Exception e){
System.out.println(e);
}
}
}
class GiveData{
double average1=0,m1=0;
double average2=0,m2=0;
public void output(NodeList nodeList){
int size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.TEXT_NODE){
Text textNode=(Text)node;
String content=textNode.getWholeText();
System.out.print(content);
Element parent=(Element)textNode.getParentNode();
boolean boo1=(parent.getNodeName()).equals("数学");
boolean boo2=(parent.getNodeName()).equals("物理");
if(boo1==true){
content=textNode.getWholeText();
average1=average1+Double.parseDouble(content.trim());
m1++;
}
if(boo2==true){
content=textNode.getWholeText();
average2=average2+Double.parseDouble(content.trim());
m2++;
}
}
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
String name=elementNode.getNodeName();
System.out.print(name);
NodeList nodes=elementNode.getChildNodes();
output(nodes);
}
}
}
}
import javax.xml.parsers.*;
import java.io.*;
public class Xiti5{
public static void main(String args[]){
GiveData give=new GiveData();
try { DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder domPaser=dbf.newDocumentBuilder();
Document document=domPaser.parse(new File("xiti5.xml")) ;
NodeList nodeList=document.getChildNodes();
give.output(nodeList);
System.out.println("数学平均:"+give.average1/give.m1+"物理平均:"+give.average2/give.m2);
}
catch(Exception e){
System.out.println(e);
}
}
}
class GiveData{
double average1=0,m1=0;
double average2=0,m2=0;
public void output(NodeList nodeList){
int size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.TEXT_NODE){
Text textNode=(Text)node;
String content=textNode.getWholeText();
System.out.print(content);
Element parent=(Element)textNode.getParentNode();
boolean boo1=(parent.getNodeName()).equals("数学");
boolean boo2=(parent.getNodeName()).equals("物理");
if(boo1==true){
content=textNode.getWholeText();
average1=average1+Double.parseDouble(content.trim());
m1++;
}
if(boo2==true){
content=textNode.getWholeText();
average2=average2+Double.parseDouble(content.trim());
m2++;
}
}
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
String name=elementNode.getNodeName();
System.out.print(name);
NodeList nodes=elementNode.getChildNodes();
output(nodes);
}
}
}
}