使用DOM读取XML文件
2013-02-19 11:51
351 查看
import java.io.InputStream;
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;
import cn.itcast.xml.domain.Person;
/**
* 使用Dom解析xml文件
*
*/
public class DomXMLReader {
public static List<Person> readXML(InputStream inStream) {
List<Person> persons = new ArrayList<Person>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inStream);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("person");//ᄇ←ᅰᅭᅨᅮperson프ᄉ ̄
for (int i = 0; i < items.getLength(); i++) {
Person person = new Person();
//得到第一个person节点
Element personNode = (Element) items.item(i);
//获取person节点的id属性值
person.setId(new Integer(personNode.getAttribute("id")));
//获取person节点下的所有子节点(标签之间的空白节点和name/age元素)
NodeList childsNodes = personNode.getChildNodes();
for (int j = 0; j < childsNodes.getLength(); j++) {
Node node = (Node) childsNodes.item(j); //判断是否为元素类型
if(node.getNodeType() == Node.ELEMENT_NODE){
Element childNode = (Element) node;
//判断是否name元素
if ("name".equals(childNode.getNodeName())) {
//获取name元素下Text节点,然后从Text节点获取数据
person.setName(childNode.getFirstChild().getNodeValue());
} else if (“age”.equals(childNode.getNodeName())) {
person.setAge(new Short(childNode.getFirstChild().getNodeValue()));
}
}
}
persons.add(person);
}
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
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;
import cn.itcast.xml.domain.Person;
/**
* 使用Dom解析xml文件
*
*/
public class DomXMLReader {
public static List<Person> readXML(InputStream inStream) {
List<Person> persons = new ArrayList<Person>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inStream);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("person");//ᄇ←ᅰᅭᅨᅮperson프ᄉ ̄
for (int i = 0; i < items.getLength(); i++) {
Person person = new Person();
//得到第一个person节点
Element personNode = (Element) items.item(i);
//获取person节点的id属性值
person.setId(new Integer(personNode.getAttribute("id")));
//获取person节点下的所有子节点(标签之间的空白节点和name/age元素)
NodeList childsNodes = personNode.getChildNodes();
for (int j = 0; j < childsNodes.getLength(); j++) {
Node node = (Node) childsNodes.item(j); //判断是否为元素类型
if(node.getNodeType() == Node.ELEMENT_NODE){
Element childNode = (Element) node;
//判断是否name元素
if ("name".equals(childNode.getNodeName())) {
//获取name元素下Text节点,然后从Text节点获取数据
person.setName(childNode.getFirstChild().getNodeValue());
} else if (“age”.equals(childNode.getNodeName())) {
person.setAge(new Short(childNode.getFirstChild().getNodeValue()));
}
}
}
persons.add(person);
}
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
相关文章推荐
- 使用SAX或者DOM或者pull读取XML文件
- 使用xml.dom.minidom读取xml文件中的值
- 利用python读取xml布局文件下面使用中文或者引用资源文件里面的中文,并且显示行号
- 使用Xpath读取xml文件
- 使用dom4j读取xml配置文件
- Android下使用TinyXml读取xml配置文件(Cocos2d-x游戏开发)
- 利用DOM读取XML文件
- C#中使用XMLReader读取XML文件
- dom4j中使用SAXReader读取xml文件出现Premature end of file异常问题
- 使用JSTL读取XML文件
- JAva使用DOM读取XML数据(解析)
- 使用DOM4J读取XML文件
- 使用PropertyPlaceholderConfigurer类读取外部属性文件内容到XML
- 使用T4模板读取XML文件
- 使用DOM解析XML的文件属性名和属性值、节点名和节点值
- ZendFramework学习第三章(核心组件—使用配置数据之从XML配置文件中读取数据)
- 使用XmlReader读取xml文件
- 使用jQuery读取XML文件数据
- 使用.NET读取XML文件,.net开发,.net,ASP.NET
- PHP使用DOM和simplexml读取xml文档的方法示例