XML程序設(shè)計習(xí)題解答耿祥義張躍平 編著_第1頁
XML程序設(shè)計習(xí)題解答耿祥義張躍平 編著_第2頁
XML程序設(shè)計習(xí)題解答耿祥義張躍平 編著_第3頁
XML程序設(shè)計習(xí)題解答耿祥義張躍平 編著_第4頁
XML程序設(shè)計習(xí)題解答耿祥義張躍平 編著_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、XML程序設(shè)計 課后習(xí)題習(xí)題一 1答:HTML是用來編寫Web頁的語言、不允許用戶自定義標(biāo)記,HTML體現(xiàn)數(shù)據(jù)的顯示格式。XML描述數(shù)據(jù)的組織結(jié)構(gòu)、可自定義標(biāo)記,其標(biāo)記名稱是對標(biāo)記所包含的數(shù)據(jù)內(nèi)容含義的抽象,而不是數(shù)據(jù)的顯示格式。2答:使用UTF-8保存5 答:(1)不可以,(2)可以,(3)不可以6答:Show.css:time display:block;font-size:18pt;font-weight:boldhour display:line;font-size:16pt;font-style:italicmimute display:line;font-size:9pt;font

2、-weight:bold習(xí)題二1答:(1)使用ANSI編碼。(2)可以。(3)不合理。2答:不相同。3答:(1)和(2)。4答:A3.xml。5答:“root”標(biāo)記包含的文本內(nèi)容都是空白字符。 “a1”標(biāo)記包含的文本內(nèi)容:<CCTV5>?!癮2”標(biāo)記包含的文本內(nèi)容: 子曰"有朋自遠(yuǎn)方來,不亦樂乎"。6答:解析器不對CDATA段的內(nèi)容做分析處理,CDATA段中的內(nèi)容可以包含任意的字符。(1)和(5)是正確的CDATA段。(2)不正確,原因是CDATA段的開始標(biāo)識中有回行,(3)不正確,原因是:CDATA段的結(jié)束標(biāo)識不正確,(4)不正確,原因是CDATA段中嵌套了另

3、一個CDATA段。7答: <p1:開關(guān)> 我也是中國制造的 </p1:開關(guān)>是錯誤的,因為p1的有效范圍是在“電視機”標(biāo)記中。8答:標(biāo)記:" root "未隸屬任何名稱空間標(biāo)記:" 電視機 "隸書的名稱空間:www.C標(biāo)記:" 顯示器 "隸書的名稱空間:www.C標(biāo)記:" 開關(guān) "隸書的名稱空間:www.G標(biāo)記:" 電線 "隸書的名稱空間:www.E習(xí)題三1答:一個規(guī)范的XML文件如果和某個DTD文件相關(guān)聯(lián),并遵守該DTD文件規(guī)定的約束條件,就稱之為有效的XML文件。2答

4、:DTD文件的編碼必須和其約束的XML文件的編碼相一致。3答:無關(guān)。4答:(1) 使用SYSTEM文檔類型聲明的格式:<DOCTYPE 根標(biāo)記的名稱 SYSTEM "DTD文件的URI">(2) 使用PUBLIC文檔類型聲明的格式:<!DOCTYPE 根標(biāo)記的名稱 PUBLIC "正式公用標(biāo)識符" "DTD文件的URI">5答:一定。6答:(1)約束標(biāo)記“張三”必須有“學(xué)號”屬性 (2)約束標(biāo)記“張三”必須有“學(xué)號”屬性,而且學(xué)號的屬性值是固定的220123。 (3)約束標(biāo)記“張三”可以有也可以沒有“學(xué)號”屬性

5、。7答:ID類型的屬性的屬性值具有互斥性,即所有ID類型的屬性的屬性值必須互不相同。8答:不合理。9答:(1)、(3)和(4)。10答,B.xml不是有效的。將B.xml修改為有效:B.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE 成績單 SYSTEM "A.dtd"><成績單> <學(xué)生> <姓名> 張三 </姓名> <成績> 優(yōu)秀 </成績> </學(xué)生> <學(xué)生&

6、gt; <姓名> 李四 </姓名> <成績> 良好 </成績> </學(xué)生></成績單>11答: Goods.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE 商品信息 SYSTEM "dtdFile.dtd"><商品信息> <商品 商標(biāo)=“Lenovo”> <p:名稱 xmlns:p=“ookk” 條形碼=“1298-24”> 聯(lián)想電腦 </p:

7、名稱> <價錢>1234</價錢> <連鎖店> <q:名稱 xmlns:q="Beijing" > 中關(guān)村專賣店</q名稱> <地點> 北京市 </地點> </連鎖店> </商品></商品信息>習(xí)題四1答:使用操作指令:<?xml-stylesheet href ="樣式表的URI" type= "text/css" ?>2答:student display:block;font-size:18pt

8、; color:red;3答:假設(shè)XML文件中有三個標(biāo)記的名稱都是“name”, “ID”屬性分別是a001,a002,a003。CSS文件如下:namea001 display:block; border-style:double; width=260; height=120 namea002 display:block; border-style:dotted; width=150; height=60 ; font-size:10pt;namea003 display:block; border-style:ridge; width=90; height=30; font-size:10

9、pt;4答:修改XML文件。5答:math display:block; font-size:12pt; font-style:italic; background-color:rgb(227,228,229);chemistry display:block; font-size:12pt; text-decoration:underline; background-color:cyan;low display:line; font-size:8pt; vertical-align:bottom;習(xí)題五1答:由若干個模板所構(gòu)成,但必須有一個主模板。2答:必須有主模板嗎。主要體現(xiàn)在該模板中的ma

10、tch屬性的值是“/”:<xsl:template match="/"> 主模板的內(nèi)容</xsl:template>3答:student的孫標(biāo)記。4答:<HTML><table border="1"> <tr> <th> <臺燈 /></th> <td>234元</td> <td>北京</td> </tr> <tr> <th> <壁燈 /></th> &

11、lt;td>56元</td> <td>上海</td> </tr></table></HTML>5答:<HTML><table border="1"> <tr> <td> <張三 /></td> <td>獲得一等獎學(xué)金</td> <td>25歲</td> </tr></table></HTML>習(xí)題六1.答:一個返回子孫節(jié)點,而另一個只返回子節(jié)點

12、:(1)getElementsByTagName(String name):返回一個NodeList對象,該對象由當(dāng)前節(jié)點的Element類型子孫節(jié)點組成,這些子孫節(jié)點的名字由參數(shù)name指定(2)getChildNodes():節(jié)點調(diào)用該方法返回一個由當(dāng)前節(jié)點的所有子節(jié)點組成的NodeList對象。2答:分別是DocumentType類型和Element類型。3答:Element類型?4答:不可以。5答:JAXPXiti5.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JA

13、XPXiti5 public static void main(String args) OutResult outContent=new OutResult(); try DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance(); DocumentBuilder builder= factory. newDocumentBuilder(); Document document= builder. parse(new File("a.xml") ; Element root=document.

14、getDocumentElement() ; String rootName=root.getNodeName(); NodeList nodeList=root.getChildNodes(); outContent.output(nodeList); System.out.println("數(shù)學(xué)平均成績"+outContent.mathAver); System.out.println("物理平均成績"+outContent.physicsAver); catch(Exception e) System.out.println(e); class O

15、utResult double sumAllMath,sumAllPhysics,mathAver,physicsAver; int mathNumber=0, physicsNumber=0; public void output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;k<size;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=

16、textNode.getWholeText(); System.out.print(content); Element parent=(Element)textNode.getParentNode(); if(parent.getNodeName().equals("數(shù)學(xué)") mathNumber+; sumAllMath=sumAllMath+Double.parseDouble(content.trim(); if(parent.getNodeName().equals("物理") physicsNumber+; sumAllPhysics=sumA

17、llPhysics+Double.parseDouble(content.trim(); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name); NodeList nodes=elementNode.getChildNodes(); output(nodes); mathAver=sumAllMath/mathNumber; physicsAver=sumAllPhysics

18、/physicsNumber; 6答:JAXPXiti6.javaimport javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPXiti6 public static void main(String args) try String 車次="T21","K456&qu

19、ot;,"T89" String 區(qū)間="北京-上海","長沙-北京","天津-重慶" DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.newDocument(); document.setXmlVersion("1.0"); Element r

20、oot=document.createElement("列車表"); document.appendChild(root); for(int k=1;k<=車次.length;k+) Node node=document.createElement("客車"); root.appendChild(node); NodeList nodeList=document.getElementsByTagName("客車"); int size=nodeList.getLength(); for(int k=0;k<size;k+)

21、 Node node=nodeList.item(k); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; Node nodeName=document.createElement("車次"); Node nodeNumber=document.createElement("運行區(qū)間"); nodeName.appendChild(document.createTextNode(車次k); nodeNumber.appendChild(document.

22、createTextNode(區(qū)間k); elementNode.appendChild(nodeName); elementNode.appendChild(nodeNumber); TransformerFactory transFactory=TransformerFactory.newInstance(); Transformer transformer=transFactory.newTransformer(); DOMSource domSource=new DOMSource(document); File file=new File("列車表.xml");

23、FileOutputStream out=new FileOutputStream(file); StreamResult xmlResult=new StreamResult(out); transformer.transform(domSource, xmlResult); out.close(); catch(Exception e) System.out.println(e); 習(xí)題七1答:DOM解析器的核心是在內(nèi)存中建立和XML文件相對應(yīng)的樹形結(jié)構(gòu)數(shù)據(jù),如果XML文件較大,相應(yīng)的Document對象就要占用較多的內(nèi)存空間;SAX解析器不在內(nèi)存中建立和XML文件相對應(yīng)的樹形結(jié)構(gòu)數(shù)據(jù),和

24、DOM解析器相比,SAX解析器占有的內(nèi)存少。2答:參見例子1的表7.1。3答:不報告。4答:uri和localName的取值是空字符組成的串,即uri="",localName="",參數(shù)qName取值是標(biāo)記名稱,atts是標(biāo)記的所有屬性的集合。5答:當(dāng)忽略標(biāo)記之間形成的空白區(qū)域時。6答:是“文件定位器”事件。7答:Xiti7.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class Xiti7 public static void main(St

25、ring args) OutTotal outWeight=new OutTotal(); try DocumentBuilderFactory factory=DocumentBuilderFactory. newInstance(); DocumentBuilder builder= factory. newDocumentBuilder(); Document document= builder.parse(new File("a.xml") ; Element root=document.getDocumentElement() ; NodeList nodeLis

26、t=root.getChildNodes(); outWeight.output(nodeList); System.out.println("總重量:"+outWeight.sum); catch(Exception e) System.out.println(e); class OutTotal double sum; public void output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;k<size;k+) Node node=nodeList.item(k); if(n

27、ode.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=textNode.getWholeText(); System.out.print(content); Element parent=(Element)textNode.getParentNode(); if(parent.getNodeName().equals("重量") sum=sum+Double.parseDouble(content.trim(); if(node.getNodeType()=Node.ELEMEN

28、T_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name); NodeList nodes=elementNode.getChildNodes(); output(nodes); 習(xí)題八1答:Node節(jié)點由可細(xì)分為Document,Element,Text,Attribue,ProcessingInstruction和Namespace節(jié)點。2答:ProcessingInstruction和Element子節(jié)點。3答:Element類型節(jié)點對應(yīng)X

29、ML中的標(biāo)記。4答:可以。5答:一個XPath路徑表達式由若干個“定位步”構(gòu)成。6答:假設(shè)XPath絕對路徑經(jīng)過第n個定位步后尋找出的第n個節(jié)點集中包含有m個節(jié)點,這m個節(jié)點按著他們在XML文件對應(yīng)的順序進行排列,序號從1開始,然后這m個節(jié)點依次使用第n+1個定位步尋找節(jié)點,它們找出的全部節(jié)點就是XPath絕對路徑經(jīng)過第n+1個定位步后尋找出的第n+1個節(jié)點集,依次類推,XPath絕對路徑經(jīng)過最后一個定位步尋找出的節(jié)點集就是XPapth絕對路徑尋找出的節(jié)點集。5答:謂詞的目的是給出定位步所尋找出的節(jié)點需滿足的進一步條件,即當(dāng)前節(jié)點在使用定位步的軸和節(jié)點測試找出若干個節(jié)點后,再使用謂詞從這些節(jié)點

30、中篩選出滿足謂詞條件的節(jié)點。6答:(1)按順序?qū)?yīng)著第一個名字是“教室”的標(biāo)記中的第一個名字是“桌子”標(biāo)記的“價格”標(biāo)記。(2) 對應(yīng)著具有屬性number,屬性值是02、名字為“桌子”的標(biāo)記,而且這些桌子標(biāo)記有子標(biāo)記“價格”,而且價格包含的文本是數(shù)字且值大于等于360。(3)對應(yīng)“教室”的屬性。(4)number屬性所在的“教室”節(jié)點。(5)109(6)396,267(7)對應(yīng)著“教室”標(biāo)記,具教室標(biāo)記有孫標(biāo)記“價格”,且“價格”標(biāo)記含有的數(shù)字文本的值大于390,即是第2個“教室”標(biāo)記。(8)對應(yīng)著價格標(biāo)記,且“價格”標(biāo)記含有的數(shù)字文本的值大于396。7答:(1) 【結(jié)果1】: A1001定

31、單的總額:(2) 【結(jié)果2】: 40.0(3) 【結(jié)果3】: 4(4) 【結(jié)果4】:1008答:Xiti8.javaimport javax.xml.xpath.*;import org.xml.sax.*;import org.w3c.dom.*; public class Xiti8 public static void main(String args) try XPathFactory xPathFactory=XPathFactory.newInstance(); XPath xPath=xPathFactory.newXPath(); InputSource source=new

32、InputSource("Xiti8.xml"); String countPath="count(/貨品列表/貨品/重量)" String sumPath="sum(/貨品列表/貨品/重量)" Double sum=(Double)xPath.evaluate(sumPath,source,XPathConstants.NUMBER); double total=sum.doubleValue(); System.out.println("總重量:"+total); catch(Exception exp) Sy

33、stem.out.println(exp); 9答:Student.xml<?xml version="1.0" encoding="UTF-8" ?><學(xué)生列表> <學(xué)生 學(xué)號='72349876'> <姓名>張三</姓名> <性別> 男 </性別> <年齡> 29 </年齡> </學(xué)生> <學(xué)生 學(xué)號='12345678'> <姓名>李張林</姓名> <性別&

34、gt; 男 </性別> <年齡> 18 </年齡> </學(xué)生> <學(xué)生 學(xué)號='000'> <姓名>張李號</姓名> <性別> 女 </性別> <年齡> 25 </年齡> </學(xué)生></學(xué)生列表>Aplication.javapublic class Application public static void main(String args) new StudentConditionWindow(); StudentCo

35、nditionWindow.javaimport java.awt.*;import javax.swing.*;public class StudentConditionWindow extends JFrame JTextField inputName,inputNumber; JTextArea showResult; JButton button; Box baseBox ,boxV1,boxV2; StudentConditionWindow() inputName=new JTextField(10); inputNumber=new JTextField(10); boxV1=B

36、ox.createVerticalBox(); boxV1.add(new Label("姓名中包含:"); boxV1.add(new Label("學(xué)號中包含"); boxV2=Box.createVerticalBox(); boxV2.add(inputName); boxV2.add(inputNumber); baseBox=Box.createHorizontalBox(); baseBox.add(boxV1); baseBox.add(boxV2); JPanel west=new JPanel(); west.add(baseBox)

37、; button=new JButton("確定"); west.add(button); add(west,BorderLayout.WEST); showResult=new JTextArea(10,10); showResult.setFont(new Font("宋體",Font.PLAIN,12); add(new JScrollPane(showResult),BorderLayout.CENTER); FindStudentByXPath findStudent; /負(fù)責(zé)使用XPath查詢學(xué)生的對象 findStudent= new Fi

38、ndStudentByXPath(inputName,inputNumber,showResult,"student.xml"); button.addActionListener(findStudent); setBounds(10,10,900,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FindStudentByXPath.javaimport javax.xml.xpath.*;import org.w3c.dom.*; import javax.swing.*;im

39、port java.awt.event.*;import org.xml.sax.*;public class FindStudentByXPath implements ActionListener XPathFactory xPathFactory; XPath xPath; String positionPath; InputSource source; JTextField inputName,inputNumber; JTextArea showResult; FindStudentByXPath(JTextField inputName,JTextField inputNumber

40、,JTextArea showResult,String fileName) this.inputName=inputName; this.inputNumber=inputNumber; this.showResult=showResult; xPathFactory=XPathFactory.newInstance(); xPath=xPathFactory.newXPath(); source=new InputSource(fileName); this.positionPath=positionPath; public void actionPerformed(ActionEvent

41、 e) showResult.setText(null); String studentName=inputName.getText().trim(); String studentNumber=inputNumber.getText().trim(); String predicates="contains(姓名,'"+studentName+"') and "+ "contains(學(xué)號,'"+studentNumber+"')" String path="/學(xué)生列表/

42、學(xué)生"+predicates+"/*" /XPath路徑表達式 try NodeList nodelist= (NodeList)xPath.evaluate(path,source,XPathConstants.NODESET); int size=nodelist.getLength(); for(int k=0;k<size;k+) Node node=nodelist.item(k); String name=node.getNodeName(); showResult.append(" "+name+":"); String content=node.getTextContent(); showResult.append(content+"n"); if(name.starts

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論