编写一个Java程序,使用DOM解析器解析下列XML文件
答案:3 悬赏:70 手机版
解决时间 2021-11-19 06:36
- 提问者网友:嘚啵嘚啵
- 2021-11-19 01:10
编写一个Java程序,使用DOM解析器解析下列XML文件
最佳答案
- 五星知识达人网友:洒脱疯子
- 2021-11-19 02:38
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Test2 {
public static void main(String args[ ]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("c.xml");
Element root =doc.getDocumentElement();
NodeList list =root.getChildNodes();
double summ=0,sump=0;
int count=0;
double avgm=0,avgp=0;
for(int i=0;i Node node=list.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE){
count++;
NodeList tlist= (( Element)node).getElementsByTagName("数学");
summ=summ+Double.parseDouble(tlist.item(0).getFirstChild().getNodeValue());
NodeList tlist1= (( Element)node).getElementsByTagName("物理");
sump=sump+Double.parseDouble(tlist1.item(0).getFirstChild().getNodeValue());
}else{
System.out.println(node.getNodeType());
}
}
avgm=summ/(count*1.0);
avgp=sump/(count*1.0);
System.out.println(avgm+"|"+avgp);
}catch(Exception e){e.printStackTrace();}
}
}
结束
import org.w3c.dom.*;
public class Test2 {
public static void main(String args[ ]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("c.xml");
Element root =doc.getDocumentElement();
NodeList list =root.getChildNodes();
double summ=0,sump=0;
int count=0;
double avgm=0,avgp=0;
for(int i=0;i
if(node.getNodeType()==Node.ELEMENT_NODE){
count++;
NodeList tlist= (( Element)node).getElementsByTagName("数学");
summ=summ+Double.parseDouble(tlist.item(0).getFirstChild().getNodeValue());
NodeList tlist1= (( Element)node).getElementsByTagName("物理");
sump=sump+Double.parseDouble(tlist1.item(0).getFirstChild().getNodeValue());
}else{
System.out.println(node.getNodeType());
}
}
avgm=summ/(count*1.0);
avgp=sump/(count*1.0);
System.out.println(avgm+"|"+avgp);
}catch(Exception e){e.printStackTrace();}
}
}
结束
全部回答
- 1楼网友:怙棘
- 2021-11-19 04:05
既然有了答案就不贴代码了,我只想说着XML格式设计的不是一般的坑爹。
89
78
- 2楼网友:山河有幸埋战骨
- 2021-11-19 02:51
用dom4j解析,非常快,具体怎么解析
SAXReader reader = new SAXReader();
String filePath = "c:/abc.xml";
File file = new File(filePath);
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
Element select = root.element("select");
for (Iterator i = select.nodeIterator(); i.hasNext();) {
Object obj = i.next();
String content = "";
if (obj instanceof Text) {
content = ((Text)obj).getText().replaceAll("\n", "").trim();
SAXReader reader = new SAXReader();
String filePath = "c:/abc.xml";
File file = new File(filePath);
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
Element select = root.element("select");
for (Iterator i = select.nodeIterator(); i.hasNext();) {
Object obj = i.next();
String content = "";
if (obj instanceof Text) {
content = ((Text)obj).getText().replaceAll("\n", "").trim();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯