xml文件
favorite.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><PhoneInfo name="手機(jī)品牌"> <Brand name="華為"> <Type name="U8650"> </Type> </Brand> <Brand name="三星"> <Type name="i9100G"> </Type> <Type name="I9108"> </Type> </Brand> <Brand name="Apple"> <Type name="解析
public static void getItemXML() { try { // 1.得到DOM解析器的工廠 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2.得到DOM工廠獲得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3.解析XML文檔,得到一個(gè)Document即DOM樹(shù) Document doc = db.parse("xml/favorite.xml"); // 4.得到所有<Brand>節(jié)點(diǎn)列表信息 NodeList brandList = doc.getElementsByTagName("Brand"); // 5.循環(huán)Brand信息 for (int i = 0; i < brandList.getLength(); i++) { Node brandNode = brandList.item(i); // 類(lèi)型轉(zhuǎn)換 Element brandElement = (Element) brandNode; // 獲取Brand的Name屬性 String brandName = brandElement.getAttribute("name"); System.out.有文本內(nèi)容的解析
網(wǎng)易手機(jī)各地行情.xml--一部分
<?xml version="1.0" encoding="GBK"?><?xml-stylesheet type="text/CSS" href="http://news.163.com/css/allrss.css"?><rss version="2.0"> <channel> <title>網(wǎng)易手機(jī)各地行情</title> <link>http://tech.163.com/mobile/special/00111SJM/sjhq_gd.html</link> <description>全國(guó)其他地區(qū)手機(jī)行情</description> <item> <title><![CDATA[超高性價(jià)比智能機(jī) 華為 U8650 僅1280元]]></title> <link>http://tech.163.com/mobile/11/1115/12/7ITDEDDA00112K8C.html</link> <description><![CDATA[U8650是華為今年推出的一款3G智能手機(jī),該機(jī)搭載Android OS 2.3系統(tǒng),采用3.5英寸480×320像素的觸控屏,內(nèi)置320萬(wàn)像素CMOS攝像頭,還具備雙512MB內(nèi)存,整機(jī)性能非常不錯(cuò)。其輕,薄的特點(diǎn),很適合女性使用。感興趣的朋友不妨來(lái)關(guān)注一下吧。近日該機(jī)在經(jīng)銷(xiāo)商處有售,售價(jià)為880元。 手機(jī)最新價(jià)格變動(dòng) ]]>......</description> <pubDate>2011-11-25 12:50:46</pubDate> </item> <item> <title><![CDATA[超高性價(jià)比智能機(jī) 華為 U8650 僅1800元]]></title> <link>http://tech.163.com/mobile/11/1115/12/7ITDEDDA00112K8C.html</link> <description><![CDATA[U8650是華為今年推出的一款3G智能手機(jī),該機(jī)搭載Android OS 2.3系統(tǒng),采用3.5英寸480×320像素的觸控屏,內(nèi)置320萬(wàn)像素CMOS攝像頭,還具備雙512MB內(nèi)存,整機(jī)性能非常不錯(cuò)。其輕,薄的特點(diǎn),很適合女性使用。感興趣的朋友不妨來(lái)關(guān)注一下吧。近日該機(jī)在經(jīng)銷(xiāo)商處有售,售價(jià)為880元。 手機(jī)最新價(jià)格變動(dòng) ]]>......</description> <pubDate>2011-11-20 12:50:46</pubDate> </item> <item id="1"> <title><![CDATA[800萬(wàn)雙網(wǎng)雙待摩托羅拉XT882賣(mài)3950]]></title> <link>http://tech.163.com/mobile/11/1115/13/7ITGR17S00112K8C.html</link> <description><![CDATA[摩托羅拉XT882是一款支持3G雙網(wǎng)雙核的智能手機(jī),配備了1G雙核Tegra 2處理器、全新的Android 2.3操作系統(tǒng)、4.0英寸qHD高清晰電容屏幕、1080P高清視頻播放及輸出、8GB超大機(jī)身內(nèi)存……今天小編從賣(mài)場(chǎng)商家處獲悉,800萬(wàn)雙網(wǎng)雙待摩托羅拉XT882賣(mài)3950元,喜歡的朋友不妨來(lái)圍觀一下吧。在外觀上,摩托XT882在外形方面和Atrix 4G基 ]]>......</description> <pubDate>2011-11-15 13:50:45</pubDate> </item> <item id="GUID" read="true"> <title><![CDATA[感恩節(jié)又放價(jià) 三星i9100G行貨3999元]]></title> <link>http://tech.163.com/mobile/11/1115/13/7ITGQV8V00112K8C.html</link> <description><![CDATA[三星i9100G是近期上市的一款智能手機(jī),1.2GHz主頻雙核處理器的加入為整機(jī)流暢運(yùn)行提供了最有利保證,整體表現(xiàn)十分出色。三星i9100G正面搭載一塊4.3英寸分辨率為480×800像素觸控屏,并且采用了Super AMOLED PLUS材質(zhì),無(wú)論是色彩飽和度以及艷麗程度都比I9000的Super AMOLED出色。此外,在機(jī)身背面它還內(nèi)置一枚800萬(wàn)像素的攝 ]]>......</description> <pubDate>2011-11-15 13:50:45</pubDate> </item> <item id="3"> <title><![CDATA[刺激享受 大屏雙核機(jī) HTC G19僅3500]]></title> <link>http://tech.163.com/mobile/11/1115/12/7ITDEFIP00112K8C.html</link> <description><![CDATA[HTC的G系列一直是人們最為關(guān)注的,在性能上不斷的更新讓人們享受到了更為強(qiáng)勁的使用性能。G19是HTC前段時(shí)間推出的一款4G智能機(jī),搭載Android OS 2.3系統(tǒng),采用1536MHz的雙核處理器,還配有4.5英寸的觸控屏,800萬(wàn)像素?cái)z像頭,絕對(duì)是一款目前最為強(qiáng)勁的實(shí)力機(jī)型。感興趣的朋友不妨來(lái)關(guān)注一下吧。 ]]>......</description> <pubDate>2011-11-15 12:50:46</pubDate> </item> <item id="4"> <title><![CDATA[典雅典范 為時(shí)尚代言 索愛(ài)X8僅980元]]></title> <link>http://tech.163.com/mobile/11/1115/12/7ITDEE6U00112K8C.html</link> <description><![CDATA[時(shí)尚幾乎就是專(zhuān)門(mén)為X8而設(shè)的詞語(yǔ),它是索尼愛(ài)立信推出的一款智能手機(jī),小巧的機(jī)身,時(shí)尚又不失典雅高端的氣質(zhì),搭載主流的Android 2.1系統(tǒng),采用600MHz的高通 MSM7227處理器,還內(nèi)置了320萬(wàn)像素的攝像頭,帶來(lái)不錯(cuò)的使用體驗(yàn)。感興趣的朋友不妨來(lái)關(guān)注一下吧。 ]]>......</description> <pubDate>2011-11-15 12:50:46</pubDate> </item> <item id="5"> <title><![CDATA[高性價(jià)比智能機(jī) 華為 U8650 僅880元]]></title> <link>http://tech.163.com/mobile/11/1115/12/7ITDEDDA00112K8C.html</link> <description><![CDATA[U8650是華為今年推出的一款3G智能手機(jī),該機(jī)搭載Android OS 2.3系統(tǒng),采用3.5英寸480×320像素的觸控屏,內(nèi)置320萬(wàn)像素CMOS攝像頭,還具備雙512MB內(nèi)存,整機(jī)性能非常不錯(cuò)。其輕,薄的特點(diǎn),很適合女性使用。感興趣的朋友不妨來(lái)關(guān)注一下吧。近日該機(jī)在經(jīng)銷(xiāo)商處有售,售價(jià)為880元。 手機(jī)最新價(jià)格變動(dòng) ]]>......</description> <pubDate>2011-11-15 12:50:46</pubDate> </item> </channel></rss>解析
public static List<Item> getItems(){ try { //創(chuàng)建DocumentBuilderFactory DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //創(chuàng)建DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder(); //創(chuàng)建Documnet Document doc=db.parse("xml/網(wǎng)易手機(jī)各地行情.xml"); //獲取節(jié)點(diǎn) NodeList itemList=doc.getElementsByTagName("item"); for(int i=0;i<itemList.getLength();i++){ Element item=(Element)itemList.item(i); String id=item.getAttribute("id"); //System.out.println("編號(hào)"+id); String title=item.getElementsByTagName("title").item(0).getFirstChild().getNodeValue(); String link=item.getElementsByTagName("link").item(0).getFirstChild().getNodeValue(); String description=item.getElementsByTagName("description").item(0).getFirstChild().getNodeValue(); String pubDate=item.getElementsByTagName("pubDate").item(0).getFirstChild().getNodeValue(); //System.out.println("標(biāo)題: "+title+"/t/t連接: "+link+"/t/t描述: "+description+"/t/t 發(fā)布日期:"+pubDate); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }二、增加X(jué)ML// 增加新的節(jié)點(diǎn) 增加小米 public static void savePhone() { try { // 1.創(chuàng)建DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2.創(chuàng)建DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder(); // 3.創(chuàng)建Document Document doc = db.parse("xml/favorite.xml"); // 創(chuàng)建新的節(jié)點(diǎn) Element brandElement = doc.createElement("Brand"); brandElement.setAttribute("name", "小米"); // 創(chuàng)建type子節(jié)點(diǎn) Element typeElement1 = doc.createElement("Type"); Element typeElement2 = doc.createElement("Type"); typeElement1.setAttribute("name", "紅米"); typeElement2.setAttribute("name", "米4"); brandElement.appendChild(typeElement1); brandElement.appendChild(typeElement2); // 找到根節(jié)點(diǎn) Element PhoneInfo = (Element) doc.getElementsByTagName("PhoneInfo") .item(0); PhoneInfo.appendChild(brandElement); // 保存XML TransformerFactory trf = TransformerFactory.newInstance(); Transformer tf = trf.newTransformer(); // 數(shù)據(jù)源 DOMSource ds = new DOMSource(doc); // 設(shè)置編碼類(lèi)型 tf.setOutputProperty(OutputKeys.ENCODING, "utf-8"); // 設(shè)置流 FileOutputStream fos = new FileOutputStream("xml/favorite.xml"); StreamResult result = new StreamResult(fos); // 把dom樹(shù)轉(zhuǎn)換為XML tf.transform(ds, result); fos.close(); System.out.println("-=========寫(xiě)入完成!=========="); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }三、修改節(jié)點(diǎn)// 修改節(jié)點(diǎn)將蘋(píng)果修改為Apple public static void modify() { try { // 創(chuàng)建DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 創(chuàng)建DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder(); // 創(chuàng)建Document Document doc = db.parse("xml/favorite.xml"); NodeList brandList = doc.getElementsByTagName("Brand"); for (int i = 0; i < brandList.getLength(); i++) { Element brand = (Element) brandList.item(i); String brandName = brand.getAttribute("name"); // 查找 if (brandName.equals("蘋(píng)果")) { // 修改屬性 brand.setAttribute("name", "Apple"); } } // 寫(xiě)入XML TransformerFactory trf = TransformerFactory.newInstance(); Transformer tf = trf.newTransformer(); DOMSource ds = new DOMSource(doc); // 設(shè)置流 FileOutputStream fos = new FileOutputStream("xml/favorite.xml"); StreamResult result = new StreamResult(fos); // 設(shè)置編碼類(lèi)型 tf.setOutputProperty(OutputKeys.ENCODING, "utf-8"); // 寫(xiě)入文件 tf.transform(ds, result); fos.close(); System.out.println("-=========修改完成!=========="); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }四、刪除節(jié)點(diǎn)// 刪除Brand name是華為的子節(jié)點(diǎn) public static void delete(){ try { //創(chuàng)建DocumentBuilerFactory DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //DocumentBuilder DocumentBuilder db=dbf.newDocumentBuilder(); //創(chuàng)建Document Document doc=db.parse("xml/favorite.xml"); //創(chuàng)建Brand節(jié)點(diǎn)集合 NodeList brandList=doc.getElementsByTagName("Brand"); //遍歷 for (int i = 0; i < brandList.getLength(); i++) { Element brandElement=(Element) brandList.item(i); String name=brandElement.getAttribute("name"); if(name.equals("華為")){ //獲取父節(jié)點(diǎn),使用父節(jié)點(diǎn)刪除這個(gè)節(jié)點(diǎn) brandElement.getParentNode().removeChild(brandElement); } } //寫(xiě)入XML TransformerFactory trf=TransformerFactory.newInstance(); Transformer tf=trf.newTransformer(); //數(shù)據(jù)源 DOMSource ds=new DOMSource(doc); //設(shè)置流 FileOutputStream fos=new FileOutputStream("xml/favorite.xml"); StreamResult result=new StreamResult(fos); //設(shè)置編碼格式 tf.setOutputProperty(OutputKeys.ENCODING,"utf-8"); //寫(xiě)入文件 tf.transform(ds, result); fos.close(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }五、更新RSSpackage com.pb.biz;import java.io.BufferedReader;import java.io.FileWriter;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;public class DownloadRSSBiz { /** * 下載RSS * @param url */ public void downloadRSS(String url){ try { // 發(fā)帖機(jī)原理,模擬瀏覽器 // final String SERVER_URL = // "http://10.0.0.131/mobile/special/001144R8/gedihangqing.xml"; // // 定義需要獲取的內(nèi)容來(lái)源地址 URL uri = new URL(url); URLConnection con = uri.openConnection(); con.setDoOutput(true); //設(shè)置請(qǐng)求參數(shù) con.setRequestProperty("Pragma:", "no-cache"); con.setRequestProperty("Cache-Control", "no-cache"); con.setRequestProperty("Content-Type", "text/xml"); //取返回值 BufferedReader br = new BufferedReader(new InputStreamReader(con .getInputStream(), "GBK")); StringBuilder sBuilder = new StringBuilder(); String line = ""; for (line = br.readLine(); line != null; line = br.readLine()) { sBuilder.append(line); } //存入文件 FileWriter writer = new FileWriter("src/網(wǎng)易手機(jī)各地行情.xml", false); writer.write(sBuilder.toString()); //關(guān)閉流 writer.close(); br.close(); } catch (Exception e) { String str = e.getMessage(); System.out.println(str); } } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注