中易网

如何用java程序通过DOM方式解析xml文件?

答案:5  悬赏:60  
解决时间 2021-03-08 02:01
如xml文件的内容是: 鏉庨攱 9703 86.5 95.2 87.0 78.6 86.825 鐜嫔彲 9601 82.7 83.5 89.0 77.3 83.125
最佳答案
建一个Student实体类封装数据,我给的是我以前解析的一个跟你的xml结构很像的例子,你只要简单的改动下就可以了。

public static List<Student> readXml() {
List<Student> list = new ArrayList<Student>();
//定义一个dom解析器工厂实例
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
//由工厂实例得到一个dom解析器
DocumentBuilder dom = factory.newDocumentBuilder();
//找到xml文档
File file=new File("src/com/jereh/ch05/Students.xml");
Document doc=dom.parse(file);
//
Element root = doc.getDocumentElement();
NodeList stuNodeList = root.getChildNodes();
for (int i = 0; i < stuNodeList.getLength(); i++) {
Node stu = stuNodeList.item(i);
Student student = new Student();
if (stu != null && stu.getNodeType() == Node.ELEMENT_NODE) {
// System.out.println(stu);
Element stuElement = (Element) stu;
student.setNo(stuElement.getAttribute("id"));
// stu.getAttributes().getNamedItem(null);
// Element stu=(Element)stuNodeList
NodeList info = stuElement.getChildNodes();
for (int j = 0; j < info.getLength(); j++) {
info.item(j).getNodeName();
Node n = info.item(j);
if ("name".equals(n.getNodeName())) {
// n.getFirstChild().getNodeValue();
student.setName(n.getLastChild().getNodeValue());
} else if ("age".equals(n.getNodeName())) {
student.setAge(Integer.parseInt(n.getFirstChild()
.getNodeValue()));
}
list.add(student);
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(root.getNodeValue());
return list;
}
全部回答
使用dom4j
导入一楼的dom4j的包,使用二楼的方法,就可以了.
使用dom4j,很简单的
public static void main(String[] args) { try { // 创建解析器工厂 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 获得解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 对XMl文档进行解析,得到Document Document doc = db.parse("c:/1.xml"); // 得到所有《brand》列表信息 NodeList stuList = doc.getElementsByTagName("Student"); // 循环Student信息 if(stuList !=null && stuList.getLength()>0){ for (int i = 0; i < stuList.getLength(); i++) { Node student = stuList.item(i); NodeList infoList = student.getChildNodes(); for (int j = 0; j < infoList.getLength(); j++) { Node typeElement = (Node) infoList.item(j); if(typeElement instanceof Element) { Element element = (Element) typeElement; Text content = (Text)element.getFirstChild(); System.out.println(element.getNodeName()+":"+content.getData()); } } } } } catch (Exception e) { e.printStackTrace(); } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
14岁女生会闭经吗
求广州最专业的游戏培训学校
华雄彩钢夹芯板厂我想知道这个在什么地方
《篮球》英文简写呢?
一首英文歌 歌词有一句是买噶情瞎拉拉 我不会
手机换卡,怎么解除qq管家绑定
有什么浏览器可以在我关机时保存我正在浏览的
好看的热血格斗动漫
天桥区济南济安社区居委会哪位知道具体地址啊
蛋黄酥能用高筋代替中筋吗
安琪酒用酵母接种量一般为多少合适?活化 水
我想给一个女孩儿起名叫曹润…后面是什么?
中国移动薛峰专营店在什么地方啊,我要过去处
在括号里填上词语()的阳光,()的气候
龙之谷手游精灵怎么升级 精灵培养攻略
推荐资讯
弱弱地问一下,怎么增加ASPEN的迭代次数
拓拼音怎么打
一个股票ST前面加个*是什么意思?
国内买美产新百伦993男鞋多少钱1双
什么声什么语又是反义词的成语
翼雅舞蹈这个地址在什么地方,我要处理点事
惠普笔记本电脑,突然特别卡,是什么原因?
巫师3 狼徽章的问题
苹果哪个地方最营养?
现在还有哪个网站可以下载小说?
我想在京东买一双鞋子200内的,软底的那种,
hpm177怎么设置中文
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?