實驗四 DOM技術(shù)應(yīng)用.doc_第1頁
實驗四 DOM技術(shù)應(yīng)用.doc_第2頁
實驗四 DOM技術(shù)應(yīng)用.doc_第3頁
實驗四 DOM技術(shù)應(yīng)用.doc_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡介

實驗四 DOM技術(shù)應(yīng)用一、實驗?zāi)康?、了解DOM技術(shù)的基本對象;2、掌握DOM技術(shù)的常用對象的基本屬性及方法;3、掌握DOM技術(shù)在Java、C#語言里的簡單使用。二、實驗要求1、能使用DOM接口對XML文檔進行解析;2、能使用DOM接口創(chuàng)建XML文檔。三、實驗原理1、DOM模型在內(nèi)存中為XML文檔建立邏輯形式的節(jié)點樹;2、DOM接口是W3C組織提供的操作XML文檔的接口。四、實驗內(nèi)容/ DOMSample.javaimport java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import org.w3c.dom.Attr;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.InputSource;import org.xml.sax.SAXException;import ernal.parsers.DOMParser;public class DOMSample public static void main(String argv) try if (argv.length != 1) System.err.println(Usage: java DOMSample filename);System.exit(1);BufferedReader in = new BufferedReader(new FileReader(argv0);DOMParser parser = new DOMParser();parser.parse(new InputSource(in); Document doc = parser.getDocument();System.out.println(元素是: );printElements(doc);System.out.println();System.out.println(每個元素的屬性是: );printElementAttributes(doc); catch (SAXException s) System.out.println(s.toString(); catch (IOException e) System.out.println(e.toString();static void printElements(Document doc) NodeList nl = doc.getElementsByTagName(*);/ “*”表示匹配所有標記。Node n;for (int i = 0; i nl.getLength(); i+) n = nl.item(i);System.out.print(n.getNodeName() + );static void printElementAttributes(Document doc) NodeList nl = doc.getElementsByTagName(*);Element e;Attr attr;NamedNodeMap nnm;String attrname;String attrval;int i, len;len = nl.getLength();for (int j = 0; j len; j+) e = (Element) nl.item(j);System.out.println(e.getTagName() + :+ e.getFirstChild().getNodeValue();nnm = e.getAttributes();if (nnm != null) for (i = 0; i nnm.getLength(); i+) attr = (Attr) nnm.item(i);attrname = attr.getName();attrval = attr.getValue();System.out.println( 屬性是: + attrname + = + attrval); /DOMGenerate.java/實驗6_2在Eclipse平臺編譯時會產(chǎn)生部分類或接口無法解析的問題,/現(xiàn)將指導書上的代碼更正如下,主要改動了前面import的內(nèi)容import org.w3c.dom.*;import ernal.dom.DocumentImpl;import ernal.serialize.OutputFormat;import ernal.serialize.XMLSerializer;import java.io.*;public class DOMGenerate public static void main( String argv ) try Document doc= new DocumentImpl(); Element root = doc.createElement(person); / Create Root Element Element item = doc.createElement(name); / Create element item.appendChild( doc.createTextNode(Jeff) ); root.appendChild( item ); / atach element to Root element item = doc.createElement(age); / Create another Element item.appendChild( doc.createTextNode(28 ) ); root.appendChild( item ); / Attach Element to previous element down tree item = doc.createElement(height); item.appendChild( doc.createTextNode(1.80 ) ); root.appendChild( item ); / Attach another Element - grandaugther doc.appendChild( root ); / Add Root to Document OutputFormat format = new OutputFormat( doc ); /Serialize DOM/ 創(chuàng)建一個File對象,代表DOM Tree所包含的數(shù)據(jù)的輸出介質(zhì),這是一個XML文件。 File f = new File (xuser.xml);/ 創(chuàng)建文件輸出流對象fos,請留意構(gòu)造函數(shù)的參數(shù)。 FileOutputStream fos=new FileOutputStream(f); XMLSerializer serial = new XMLSerializer(format ); serial.setOutputByteStream(fos); / 串行化輸出結(jié)果。 serial.asDOMSerializer().serialize(doc); catch ( Exception ex ) ex.printStackTrace(); 五、思考題(1)如何為題目2所建立的XML文檔增加學生節(jié)點的子節(jié)點“專業(yè)”?在跟結(jié)點下

溫馨提示

  • 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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論