java 读取 创设 XML文件

java 读取 创建 XML文件

最近需整理,java创建xml文档,java读取xml文件
各个独立的系统之间要进行数据交互,比如说c做的系统,要与java做的系统弄个数据接口
用xml封装数据进行交互,暂时从老大那里听到的,所以现在上班时间整理下,速战速决,还  要 弄其他的东西,(网上资料一大堆)

  1--java创建xml文档





  2--java 读取xml文件


  xml文件
  <?xml version="1.0" encoding="GB2312" standalone="no"?>
<books>
    <book name = "think in java 第四版" >
        <name>美国人牛人</name>
        <price>108元</price>
    </book>
   <book name = "jmaki ajax">
        <name>sun</name>
        <price>56.89元</price>
    </book>
</books> 




  java代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Hashtable;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * 描述:创建xml对象,读取xml数据,将信息设置到vo对象中
 * @author oyp 2009-12-17
 */
public class XMLReader {
	//key=vo标示符,value =vo对象
	public Hashtable<String,Object> hashTableClasses = new Hashtable<String,Object>(); 
	public XMLReader() throws Exception, Exception {
		//key=vo属性,value=vo属性值
		Hashtable<String,Object> hashTableClass = new Hashtable<String,Object>();
		// 1--得到DOM解析器的工厂实例javax.xml.parsers.DocumentBuilderFactory;
		// 类的实例就是我们要的解析器工厂
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		try {
			// 2--从DOM工厂获得DOM解析器DocumentBuilder
			// DocumentBuilder的constructor是protected,不能new的,要通过工厂来创建
			// 复习下java作用域。。。。
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			// 3--把要解析的XML文档转化为输入流,以便DOM解析器解析它,(给它捅根管道)
			InputStream is = new FileInputStream("D:\\liu.xml"); // xml 的路径
			// 4--解析XML文档的输入流,得到一个Document
			Document doc = dombuilder.parse(is);
			// 5--得到XML文档的根节点 在DOM中根节点只有一个且此对象为org.w3c.dom.Element
			Element root = doc.getDocumentElement();
			// 6--得到节点的子节点
			NodeList books = root.getChildNodes();
			if (books != null) {
				for (int i = 0; i < books.getLength(); i++) {
					Node book = books.item(i);
					if (book.getNodeType() == Node.ELEMENT_NODE) {
						// 7--取得节点的属性值!!属性值
						String classname = book.getAttributes()
								.getNamedItem("name").getNodeValue();
						System.out.println(classname);
						// 注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE

						// 8--轮循子节点!!子节点
						for (Node node = book.getFirstChild(); node != null; node = node
								.getNextSibling()) {
							if (node.getNodeType() == Node.ELEMENT_NODE) {
								if (node.getNodeName().equals("name")) {
									String name = node.getNodeValue();
									String name1 = node.getFirstChild()
											.getNodeValue();
									System.out.println("这个值是空的:" + name);
									System.out
											.println("xml里面的name标签值:" + name1);
									hashTableClass.put("name", name1);
								}
								if (node.getNodeName().equals("price")) {
									String price = node.getFirstChild()
											.getNodeValue();
									System.out.println("xml里面的price标签值:"
											+ price);
									hashTableClass.put("price", price);
								}
								
								Book b = setBook(hashTableClass);
								hashTableClasses.put(classname, b);
								
							}
						}
					}
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		System.out.println(new XMLReader().hashTableClasses);
	}
	
	public Book setBook(Hashtable hash) {
		Field[] fields = Book.class.getDeclaredFields();
		Book book = new Book();
		for (int i = 0 ; fields!=null&&i < fields.length ; i++)  {
			Field field = fields[i];
			if (hash.containsKey(field.getName())) {
				try {
					field.setAccessible(true);
					//如果对属性的类型还需要判断,可以自定义一个函数对field.getType()进行判断
					field.set(book, hash.get(field.getName()));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return book;
	}

}
class Book {
	private String name;
	private String price;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
}


未完待续
干正事了 还要看jmaki,时间啊...