java学习之一种dom解析

今天学习了java的dom解析,个人感觉这个并不难,只是需要记住一些步奏就好,在我的代码中有详细的注释,以下,我便贴出我的代码,如果有不明白的地方,可以在博客下留言,我会尽快回答

 
  

import java.util.List;

public class Main {
	public static void main(String[] args) {
		String path="D://My Documents/桌面/book.xml";
		MyDom md=new MyDom(path);
		List books=md.Change();
		for(Book book:books){
			System.out.println(book);
		}
	}

}


package com.jk.dom;

public class Book {
	private int id;
	private String name;
	private float price;

	public int getId() {
		return id;
	}

	public void setId(int i) {
		this.id = i;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	@Override
	// 重写同String以便输出book类的对象的相关信息
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
	}

}

package com.jk.dom;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore.Builder;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Node;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class MyDom {
	File mfile;
	FileInputStream input;

	public MyDom(String path) {
		super();
		//为目录创建文件对象
		mfile = new File(path);
	}
//创建一个方法来实现dom的解析
	public List Change() {

		List list = new ArrayList();
		
		try {
			//为输出流分配内存
			input = new FileInputStream(mfile);
			//以下三步为所有的解析都需要的
			//第一步,获得dom解析器工厂
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//第二步,获得解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			//第三步,解析一个xml文档,获得document对象
			Document document = db.parse(input);
			Element element = document.getDocumentElement();
			//根据标签访问节点
			NodeList booknodes = element.getElementsByTagName("book");
			//遍历节点
			for (int i = 0; i < booknodes.getLength(); i++) {
				Element bookelElement = (Element) booknodes.item(i);
				Book book = new Book();
				 book.setId(Integer.parseInt(bookelElement.getAttribute("id")));
				NodeList childnodes = bookelElement.getChildNodes();
				//遍历子节点
				for (int j = 0; j < childnodes.getLength(); j++) {
					if(childnodes.item(j).getNodeType()==Node.ELEMENT_NODE){
						if("name".equals(childnodes.item(j).getNodeName())){
							book.setName(childnodes.item(j).getFirstChild().getNodeValue());
						}else if("price".equals(childnodes.item(j).getNodeName())){
							book.setPrice(Float.parseFloat(childnodes.item(j).getFirstChild().getNodeValue()));
						}
					}
				}
				//添加的list
				list.add(book);
				
				
			}

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException 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();
		}
//返回list
		return list;

	}

}


你可能感兴趣的:(java学习笔记)