package domtest_01;
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;
public class DOMTest_01 {
public DocumentBuilder getDocumentBuilder(){
//创建documentbuilderfactory对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//创建documentbuilder对象
DocumentBuilder db =null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return db;
}
public void xmlParser(){
try {
//通过documentbuilder对象的parse方法加载books.xml文件到当前项目下
Document document = getDocumentBuilder().parse("book.xml");
NodeList booklist = document.getElementsByTagName("book");
//通过nodelist的getlength获取booklist的长度
System.out.println("一共有"+booklist.getLength()+"本书");
//遍历每一个book节点
for(int i=0; i<booklist.getLength(); i++){
//下面开始遍历i+1本书的内容
System.out.println("====下面开始遍历第"+(i+1)+"本书的内容====");
//通过item(i)获取一个book节点,nodelist的索引值
Node book = booklist.item(i);
//获取book节点的所有属性集合
NamedNodeMap attr = book.getAttributes();
System.out.println("第"+(i+1)+"本书共有"+attr.getLength()+"个属性");
//遍历book的属性
for(int j=0; j<attr.getLength(); j++){
//通过item方法获取book节点的某一属性
Node att = attr.item(j);
//获取属性名
System.out.print("属性名:"+att.getNodeName());
//获取属性值
System.out.println("属性值:"+att.getNodeValue());
}
//解析book节点的子节点
NodeList childNodes = book.getChildNodes();
//遍历childNodes获取每个节点的节点名和节点值
System.out.println("第"+(i+1)+"本书共有"+childNodes.getLength()+"个子节点");
for(int k=0; k<childNodes.getLength(); k++){
//区分text类型的node以及element类型的node
if(childNodes.item(k).getNodeType()==Node.ELEMENT_NODE){
//获取element类型的节点名
System.out.print(childNodes.item(k).getNodeName()+": ");
System.out.println(childNodes.item(k).getFirstChild().getNodeValue());
}
}
System.out.println("====结束遍历第"+(i+1)+"本书的内容====");
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 创建xml
*
*/
public void xmlCreate(){
DocumentBuilder db = getDocumentBuilder();
Document document = db.newDocument();
Element bookstore = document.createElement("bookstore");
//向bookstore节点中添加子节点book
Element book = document.createElement("book");
book.setAttribute("id", "1");
bookstore.appendChild(book);
//将bookstore节点添加到dom树中
document.appendChild(bookstore);
//创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
try {
//创建Transformer对象
Transformer tf = tff.newTransformer();
//换行
tf.setOutputProperty(OutputKeys.INDENT,"yes");
tf.transform(new DOMSource(document),new StreamResult(new File("books1.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//创建DOMTest_01对象
DOMTest_01 test = new DOMTest_01();
//调用解析方法,解析xml文件
// test.xmlParser();
test.xmlCreate();
}
}