package com.imooc.domtest;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*
* 应用DOM方式解析books.xml文件
*/
public class Domtest {
public DocumentBuilder getDocumentBuilder(){
//创建一个DocumentBuilderFactory对象
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();//新的 DocumentBuilder 实例
DocumentBuilder db=null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return db;
}
public void xmlParse(){ //解析xml文件
try {
//通过DocumentBuilder对象的parse方法加载Books.xml文件到当前项目下
Document document= getDocumentBuilder().parse("f:\\books.xml");
//获取所有book节点集合
NodeList booklist= document.getElementsByTagName("book");
System.out.println("一共有"+booklist.getLength()+"本书");
//遍历每个book节点
for(int i=0;i<booklist.getLength();i++){
System.out.println("============下面开始遍历第"+(i+1)+"本书的内容==============");
//通过item(i)方法来获取一个book节点,nodelist的索引值从0开始
Node book= booklist.item(i);
//遍历book节点的所有的属性
NamedNodeMap attrs=book.getAttributes();
System.out.println("第"+(i+1)+"本书共有"+attrs.getLength()+"个属性");
for(int j=0;j<attrs.getLength();j++){
//通过item(index)方法获取属性
Node attr=attrs.item(j);
//获取属性名称
System.out.print("属性名称:"+ attr.getNodeName());
System.out.println("属性值:"+attr.getNodeValue());
//解析book节点的子节点
NodeList childnode= book.getChildNodes();
System.out.println("第"+(i+1)+"本书中有"+childnode.getLength()+"个节点");
//遍历每个子节点的节点名和节点值
for(int m=0;m<childnode.getLength();m++){
//区分element类型的节点和text类型的节点
if(childnode.item(m).getNodeType()==Node.ELEMENT_NODE){
//获取element类型的节点名的节点值
System.out.print("第"+(m+1)+"个节点的节点名是:"+childnode.item(m).getNodeName());
//System.out.println(childnode.item(m).getFirstChild().getNodeValue());}
System.out.println("--节点值是:"+childnode.item(m).getTextContent()); }//注意前一句和本行这句的区别在于:getTextContent()方法可以获取name节点下的子节点的节点值
}
System.out.println("============结束遍历第"+(i+1)+"本书的内容==============");
}
//该种方法的前提是该book节点有且只有一个属性。
//先进行强制类型转换为Element类型
/* Element book=(Element)booklist.item(i);
String attrValue=book.getAttribute("id");
System.out.println("id的属性值是"+attrValue);
System.out.println("============结束遍历第"+(i+1)+"本书的内容==============");*/
}
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 生成XML
*/
public void createXML(){
DocumentBuilder db=getDocumentBuilder();
Document document=db.newDocument();
document.setXmlStandalone(true);
Element bookstore=document.createElement("bookStore");
//向bookstore根节点中添加子节点book
Element book=document.createElement("book");
Element name=document.createElement("name");
book.appendChild(name);
name.setTextContent("老人与海");
Element author=document.createElement("author");
book.appendChild(author);
author.setTextContent("海明威");
Element year=document.createElement("year");
book.appendChild(year);
year.setTextContent("2015");
Element price=document.createElement("price");
book.appendChild(price);
price.setTextContent("67");
book.setAttribute("id", "1");
//将book节点加到bookstore中
bookstore.appendChild(book);
Element book1=document.createElement("book1");
Element name1=document.createElement("name");
book1.appendChild(name1);
name1.setTextContent("看见");
Element author1=document.createElement("author");
book1.appendChild(author1);
author.setTextContent("柴静");
Element year1=document.createElement("year");
book1.appendChild(year1);
year1.setTextContent("2014");
Element price1=document.createElement("price");
book1.appendChild(price1);
price1.setTextContent("87");
Element language=document.createElement("language");
book1.appendChild(language);
language.setTextContent("Chinses");
book1.setAttribute("id", "2");
bookstore.appendChild(book1);
document.appendChild(bookstore);
TransformerFactory tff=TransformerFactory.newInstance();//获取 TransformerFactory 的新实例
try {
Transformer tf=tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");//起着换行的作用
tf.transform(new DOMSource(document),new StreamResult(new File("books1.xml")));
}catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Domtest test=new Domtest();
// test.xmlParse();
test.createXML();
}
}