加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml dom 解析

发布时间:2020-12-16 05:02:25 所属栏目:百科 来源:网络整理
导读:两种解析技术: 1、dom document object model,w3c 标准 ;2、sax simple api for xml books.xml ?xml version="1.0" encoding="UTF-8"?booksbook id="b001"titleJava 核心技术/title price98/price/bookbook id="b002"titleThinking in Java/title price220

两种解析技术:

1、dom document object model,w3c 标准 ;2、sax simple api for xml


books.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book id="b001">
		<title>Java 核心技术</title>
	 	<price>98</price>
	</book>
	<book id="b002">
		<title>Thinking in Java</title>
	 	<price>220</price>
	</book>
</books>
Books.java
package cn.ouc.xml;

public class Books {
	private String id ;
	private String title ;
	private String price ;
	
	public Books() {
	}
	
	public Books(String id,String title,String price) {
		super();
		this.id = id;
		this.title = title;
		this.price = 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 String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Books [id=" + id + ",title=" + title + ",price=" + price
				+ "]";
	}
}

package cn.ouc.xml.dom;

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.ouc.xml.Books;

public class Dom {
public static void main(String[] args) throws Exception {
		System.out.println(getAllBook());
	}
	
	/*
	 * 获得所有的书籍,将所有的书籍内容保存list
	 */
	public static List getAllBook() throws Exception{
		//创建list,用于保存所有的数据
		List allBookList = new ArrayList();
		
		/* 从xml文档中将需要的数据,查询出来,替换模拟数据 */
		//获得实例工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//获得解析
		DocumentBuilder builder = factory.newDocumentBuilder();
		//获得document --解析xml文档   java.io.FileNotFoundException
		Document document = builder.parse("books.xml");  //指java项目的根路径下的文件
		//获得所有的书籍元素
		NodeList bookElements = document.getElementsByTagName("book");
		//遍历所有的书籍元素
		for(int i = 0 ; i < bookElements.getLength() ; i++){
			//获得每一本书籍元素
			Node node = bookElements.item(i);
//			node.getAttributes();  --获得所有的属性
			Element bookEle = (Element)node;
			//获得指定名称id,的属性值
			String id = bookEle.getAttribute("id");
			String title = "";
			String price = "";
			
			//将title和price查询
			//获得当前book元素的所有的子节点
			NodeList childList = bookEle.getChildNodes();
			System.out.println( id + ":" + childList.getLength());
			//遍历所有的孩子
			for(int c = 0 ; c < childList.getLength() ; c++){
				//获得所有的孩子
				Node childNode = childList.item(c);
				//Element child = (Element)childNode;  //java.lang.ClassCastException:
				//获得节点名称
				String childName = childNode.getNodeName();
				
				//判断是否title
				//if(childName.equals("title"))
				if("title".equals(childName)){
					//获得title内容
					title = childNode.getTextContent();
				}
				
				//判断是否是price
				if("price".equals(childName)){
					price = childNode.getTextContent();
				}
			}
			//创建JavaBean Book对象
			Books book = new Books();
			book.setId(id);
			book.setPrice(price);
			book.setTitle(title);
			
			//将保存所有数据的对象Book添加到list中
			allBookList.add(book);
		
		}
		
		return allBookList;
	}
	
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读