作業(yè)供參考!
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;
/*
?* 應(yīng)用DOM方式解析books.xml文件
?*/
public class Domtest {
?
?public DocumentBuilder getDocumentBuilder(){
??//創(chuàng)建一個(gè)DocumentBuilderFactory對(duì)象
??DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();//新的 DocumentBuilder 實(shí)例
??DocumentBuilder db=null;
??try {
???db = dbf.newDocumentBuilder();
???
??} catch (ParserConfigurationException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??return db;
??
?}
?public void xmlParse(){ //解析xml文件
??try {
???//通過(guò)DocumentBuilder對(duì)象的parse方法加載Books.xml文件到當(dāng)前項(xiàng)目下
????? Document document=? getDocumentBuilder().parse("f:\\books.xml");
????? //獲取所有book節(jié)點(diǎn)集合
???? NodeList booklist= document.getElementsByTagName("book");
???? System.out.println("一共有"+booklist.getLength()+"本書(shū)");
???? //遍歷每個(gè)book節(jié)點(diǎn)
???? for(int i=0;i<booklist.getLength();i++){
????? System.out.println("============下面開(kāi)始遍歷第"+(i+1)+"本書(shū)的內(nèi)容==============");
????? //通過(guò)item(i)方法來(lái)獲取一個(gè)book節(jié)點(diǎn),nodelist的索引值從0開(kāi)始
??????? Node book= booklist.item(i);
???? //遍歷book節(jié)點(diǎn)的所有的屬性
???? NamedNodeMap attrs=book.getAttributes();
???? System.out.println("第"+(i+1)+"本書(shū)共有"+attrs.getLength()+"個(gè)屬性");
???? for(int j=0;j<attrs.getLength();j++){
????? //通過(guò)item(index)方法獲取屬性
????? Node attr=attrs.item(j);
????? //獲取屬性名稱
????? System.out.print("屬性名稱:"+ attr.getNodeName());
????? System.out.println("屬性值:"+attr.getNodeValue());
????? //解析book節(jié)點(diǎn)的子節(jié)點(diǎn)
???? NodeList childnode= book.getChildNodes();
???? System.out.println("第"+(i+1)+"本書(shū)中有"+childnode.getLength()+"個(gè)節(jié)點(diǎn)");
???? //遍歷每個(gè)子節(jié)點(diǎn)的節(jié)點(diǎn)名和節(jié)點(diǎn)值
???? for(int m=0;m<childnode.getLength();m++){
?????
?????//區(qū)分element類型的節(jié)點(diǎn)和text類型的節(jié)點(diǎn)
?????if(childnode.item(m).getNodeType()==Node.ELEMENT_NODE){
??????//獲取element類型的節(jié)點(diǎn)名的節(jié)點(diǎn)值
?????System.out.print("第"+(m+1)+"個(gè)節(jié)點(diǎn)的節(jié)點(diǎn)名是:"+childnode.item(m).getNodeName()); ?
?????//System.out.println(childnode.item(m).getFirstChild().getNodeValue());}
?????System.out.println("--節(jié)點(diǎn)值是:"+childnode.item(m).getTextContent()); }//注意前一句和本行這句的區(qū)別在于:getTextContent()方法可以獲取name節(jié)點(diǎn)下的子節(jié)點(diǎn)的節(jié)點(diǎn)值
???? }
??????
????? System.out.println("============結(jié)束遍歷第"+(i+1)+"本書(shū)的內(nèi)容==============");
???? }
????? //該種方法的前提是該book節(jié)點(diǎn)有且只有一個(gè)屬性。
????? //先進(jìn)行強(qiáng)制類型轉(zhuǎn)換為Element類型
??? /*? Element book=(Element)booklist.item(i);
????? String attrValue=book.getAttribute("id");
????? System.out.println("id的屬性值是"+attrValue);
????? System.out.println("============結(jié)束遍歷第"+(i+1)+"本書(shū)的內(nèi)容==============");*/
?????
???? }
??}
??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根節(jié)點(diǎn)中添加子節(jié)點(diǎn)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節(jié)點(diǎn)加到bookstore中
??bookstore.appendChild(book);
??
??Element book1=document.createElement("book1");
??Element name1=document.createElement("name");
??book1.appendChild(name1);
??name1.setTextContent("看見(jiàn)");
??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 的新實(shí)例
??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();
?
}
}
2022-03-24
每明白您問(wèn)的意思。。。能不能詳細(xì)描述一下在11章中會(huì)有講解,請(qǐng)繼續(xù)學(xué)習(xí)下去
2016-09-02
。。。。