如何用java程序通过DOM方式解析xml文件?
答案:5 悬赏:60
解决时间 2021-03-08 02:01
- 提问者网友:樱花树下最美的约定
- 2021-03-07 22:27
如xml文件的内容是: 鏉庨攱 9703 86.5 95.2 87.0 78.6 86.825 鐜嫔彲 9601 82.7 83.5 89.0 77.3 83.125
最佳答案
- 二级知识专家网友:最后战士
- 2021-03-07 23:32
建一个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;
}
全部回答
- 1楼网友:如果这是命
- 2021-03-08 02:47
使用dom4j
- 2楼网友:疯山鬼
- 2021-03-08 01:58
导入一楼的dom4j的包,使用二楼的方法,就可以了.
- 3楼网友:情窦初殇
- 2021-03-08 01:05
使用dom4j,很简单的
- 4楼网友:陪我到地狱流浪
- 2021-03-08 00:51
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();
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯