第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【2-3作業(yè)】豐富創(chuàng)建的XML文件中的內(nèi)容

/**
?*Book類
?*/
package?com.imooc.xmlText;

public?class?Book?{
????private?String?id;
????private?String?name;
????private?String?author;
????private?String?year;
????private?String?price;
????private?String?language;

????public?String?getId()?{
????????return?id;
????}

????public?void?setId(String?id)?{
????????this.id?=?id;
????}

????public?String?getName()?{
????????return?name;
????}

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

????public?String?getAuthor()?{
????????return?author;
????}

????public?void?setAuthor(String?author)?{
????????this.author?=?author;
????}

????public?String?getYear()?{
????????return?year;
????}

????public?void?setYear(String?year)?{
????????this.year?=?year;
????}

????public?String?getPrice()?{
????????return?price;
????}

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

????public?String?getLanguage()?{
????????return?language;
????}

????public?void?setLanguage(String?language)?{
????????this.language?=?language;
????}
}
package?com.imooc.xmlText;

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;

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?java.io.File;
import?java.io.IOException;
import?java.util.ArrayList;
import?java.util.List;

public?class?DOMTest1?{
????public?static?void?main(String[]?args)?throws?ParserConfigurationException,?IOException,?SAXException,?TransformerException?{
????????DocumentBuilderFactory?documentBuilderFactory=DocumentBuilderFactory.newInstance();
????????DocumentBuilder?documentBuilder=documentBuilderFactory.newDocumentBuilder();
????????//解析xml文件
????????Document?document=documentBuilder.parse("demo/books1.xml");
????????NodeList?bookList=?document.getElementsByTagName("book");
????????Book?newBook=new?Book();
????????List<Book>?newBookList=new?ArrayList<Book>();
????????System.out.println("共有"+bookList.getLength()+"本書(shū)");
????????for(int?i=0;i<bookList.getLength();i++){
????????????Node?book=bookList.item(i);
????????????NamedNodeMap?attrs=?book.getAttributes();
????????????for(int?j=0;j<attrs.getLength();j++){
????????????????Node?attr=attrs.item(j);
????????????????newBook.setId(attr.getNodeValue());
????????????????System.out.println(attr.getNodeName()+":"+attr.getNodeValue());}
????????????NodeList?childList=book.getChildNodes();
????????????for(int?m=0;m<childList.getLength();m++){
????????????????Node?child=childList.item(m);
????????????????if(child.getNodeType()==Node.ELEMENT_NODE){
????????????????????switch(child.getNodeName()){
????????????????????????case?"name":
????????????????????????????newBook.setName(child.getTextContent());
????????????????????????????break;
????????????????????????case?"author":
????????????????????????????newBook.setAuthor(child.getTextContent());
????????????????????????????break;
????????????????????????case?"year":
????????????????????????????newBook.setYear(child.getTextContent());
????????????????????????????break;
????????????????????????case?"price":
????????????????????????????newBook.setPrice(child.getTextContent());
????????????????????????????break;
????????????????????????case?"language":
????????????????????????????newBook.setLanguage(child.getTextContent());
????????????????????????????break;
????????????????????}
????????????????????System.out.println(child.getNodeName()+":"+child.getTextContent());}
????????????}
????????????newBookList.add(newBook);
????????????newBook=new?Book();
????????}
????????System.out.println("共有"+newBookList.size()+"本書(shū)");
????????for?(Book?book:newBookList)?{
????????????System.out.println("第"+book.getId()+"本書(shū)");
????????????System.out.println("Name:"+book.getName());
????????????System.out.println("Author:"+book.getAuthor());
????????????System.out.println("Year:"+book.getYear());
????????????System.out.println("Price:"+book.getPrice());
????????????System.out.println("Language:"+book.getLanguage());
????????????System.out.println("----finish----");
????????}
????????//生成xml文件
????????Document?newDocument=documentBuilder.newDocument();
????????Element?bookstore=newDocument.createElement("bookstore");
????????for(int?i=0;i<newBookList.size();i++)?{
????????????Book?bookValue=newBookList.get(i);
????????????Element?book?=?newDocument.createElement("book");
????????????book.setAttribute("id",bookValue.getId()?);
????????????if(bookValue.getName()!=null&&!bookValue.getName().trim().equals("")){
????????????Element?name=newDocument.createElement("name");
????????????name.setTextContent(bookValue.getName());
????????????book.appendChild(name);}
????????????if(bookValue.getAuthor()!=null&&!bookValue.getAuthor().trim().equals("")){
????????????Element?author=newDocument.createElement("author");
????????????author.setTextContent(bookValue.getAuthor());
????????????book.appendChild(author);}
????????????if(bookValue.getYear()!=null&&!bookValue.getYear().trim().equals("")){
????????????Element?year=newDocument.createElement("year");
????????????year.setTextContent(bookValue.getYear());
????????????book.appendChild(year);}
????????????if(bookValue.getPrice()!=null&&!bookValue.getPrice().trim().equals("")){
????????????Element?price=newDocument.createElement("price");
????????????price.setTextContent(bookValue.getPrice());
????????????book.appendChild(price);}
????????????if(bookValue.getLanguage()!=null&&!bookValue.getLanguage().trim().equals("")){
????????????Element?language=newDocument.createElement("language");
????????????language.setTextContent(bookValue.getLanguage());
????????????book.appendChild(language);}
????????????bookstore.appendChild(book);
????????}
????????newDocument.appendChild(bookstore);
????????TransformerFactory?transformerFactory=TransformerFactory.newInstance();
????????Transformer?transformer=transformerFactory.newTransformer();
????????transformer.setOutputProperty(OutputKeys.INDENT,"yes");
????????transformer.transform(new?DOMSource(newDocument),new?StreamResult(new?File("demo1/books0.xml")));




????}

}


正在回答

1 回答

確實(shí)可以呀

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

【2-3作業(yè)】豐富創(chuàng)建的XML文件中的內(nèi)容

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)