如何用java程序通过DOM方式解析xml文件?
如xml文件的内容是:<?xmlversion="1.0"encoding="gb2312"?><XMLID="StudentList"><...
如xml文件的内容是:<?xml version="1.0" encoding="gb2312"?><XML ID="StudentList"> <Students> <Student> <Name>鏉庨攱</Name> <Class>9703</Class> <YuWen>86.5</YuWen> <ShuXue>95.2</ShuXue> <JiSuanJi>87.0</JiSuanJi> <YingYu>78.6</YingYu> <AverageScore>86.825</AverageScore> </Student> <Student> <Name>鐜嫔彲</Name> <Class>9601</Class> <YuWen>82.7</YuWen> <ShuXue>83.5</ShuXue> <JiSuanJi>89.0</JiSuanJi> <YingYu>77.3</YingYu> <AverageScore>83.125</AverageScore> </Student> </Students></XML>
展开
5个回答
展开全部
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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
导入一楼的dom4j的包,使用二楼的方法,就可以了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建一个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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询