1.java代码
package com.cgm.domtest;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class PareDomBook {
public static void main(String[] args) {
List list = getAllBook();
for (int i = 0; i < list.size(); i++) {
Book book = (Book) list.get(i);
System.out.println(book.getId() + "---" + book.getTitle() + "---" + book.getPrice());
}
}
public static List getAllBook() {
List list = new ArrayList();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("book.xml");
NodeList boolNodeList = document.getElementsByTagName("book");
for (int i = 0; i < boolNodeList.getLength(); i++) {
Book book = new Book();
Node node = boolNodeList.item(i);
Element el = (Element) node;
String id = el.getAttribute("id");
NodeList chList = el.getChildNodes();
String title = "";
int price = 0;
for (int j = 0; j < chList.getLength(); j++) {
Node chiNode = chList.item(j);
String chiName = chiNode.getNodeName();
if (chiName != null) {
if ("title".equals(chiName)) {
title = chiNode.getTextContent();
}
if ("price".equals(chiName)) {
String priceStr = chiNode.getTextContent();
price = Integer.parseInt(priceStr);
}
}
}
book.setId(id);
book.setTitle(title);
book.setPrice(price);
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void bulder() {
}
}
javabean代码
package com.cgm.domtest;
public class Book {
private String id;
private String title;
private int price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="b001">
<title>java core</title>
<price>688</price>
</book>
<book id="b002">
<title>思想政治</title>
<price>515</price>
</book>
<book id="b003">
<title>世界历史</title>
<price>545</price>
</book>
<book id="b004">
<title>大学生物</title>
<price>512</price>
</book>
</books>
分享到:
相关推荐
java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件 java读写xml文件
用java读取xml文件的四种方法
利用Java下的IO读取或写入xml及word,txt文件,且能有效去除乱码(很适用,且和POI组建比,少占了很多内存),这是我根据网上很多不同的版本,整合而出的一种最易最好的IO读写文件代码;
使用 Java 读取 XML 配置文件 Java 语言和 XML 技术可以说是黄金组合,网上已经有很多文章介绍 XML 在电子商务中的数据交换的作用。但是在平时系统开发中,我们不一定都用到数据交换,是否无法使用 XML?当然不是...
文档中有代码案例,详细讲解了使用java读取xml,并附带有关于xml的dtd讲解,里面包含详细的文档和代码案例。和读取xml所需的jar。
java读取xml文件 ,程序自带jar包在mylib下,加入工程即可,工程导入可用,更具自己需要读取xml文件生成所需文件。
java 读写xml文件操作, 简单易懂
简单利用java读取xml中的标签,也是写xml
java读写xml的代码
java实现读取xml文件内容
java读取XML文件内容的四种方法[归类].pdf
Java 读取 XML 文件的四种方法 在 Java 中读取 XML 文件有多种方法,本文将介绍四种常见的方法,分别使用 DOM、DOM4J、JDOM 和 SAX 解析器。 第一种方法:使用 DOM DOM(Document Object Model)是一种树形结构,...
java代码读取xml文件,生成树形图。这里是源码,可直接使用,是我项目中提取生成树的核心文件。
java从xml中读入数据并存入到sqlserver2005数据库中
java读取xml的四种方法,欢迎下载java读取xml的四种方法
java实现的一个读取本地xml文件的例子,进入正题: 需要的包jdom2-2.0.6.jar:下载地址,http://mvnrepository.com/artifact/org.jdom/jdom2 此例子比较简单
java读取xml文件的四种方法: 第一种 DOM 实现方法 第二种 DOM4J实现方法 第三种 JDOM实现方法 第四种SAX实现方法