java 读取XML

这是其中之一,附件更多。。。
 
SAX版
java 代码
  1. package com.mobi.bean;   
  2.   
  3. import org.xml.sax.helpers.DefaultHandler;   
  4. import javax.xml.parsers.*;   
  5. import org.xml.sax.*;   
  6. import org.xml.sax.helpers.*;   
  7. import java.util.*;   
  8. import java.io.*;   
  9.   
  10. public class SAXReader extends DefaultHandler   
  11. {   
  12.     java.util.Stack tags = new java.util.Stack();   
  13.   
  14.     // --------------XML Content-------------   
  15.     String text = null;   
  16.   
  17.     String url = null;   
  18.   
  19.     String author = null;   
  20.   
  21.     String description = null;   
  22.   
  23.     String day = null;   
  24.   
  25.     String year = null;   
  26.   
  27.     String month = null;   
  28.   
  29.     // ----------------------------------------------   
  30.   
  31.     public void endDocument() throws SAXException   
  32.     {   
  33.         System.out.println("------Parse End--------");   
  34.     }   
  35.   
  36.     public void startDocument() throws SAXException   
  37.     {   
  38.         System.out.println("------Parse Begin--------");   
  39.   
  40.     }   
  41.   
  42.     public void startElement(String p0, String p1, String p2, Attributes p3)   
  43.             throws SAXException   
  44.     {   
  45.         tags.push(p1);   
  46.     }   
  47.   
  48.     public void endElement(String p0, String p1, String p2) throws SAXException   
  49.     {   
  50.         tags.pop();   
  51.         if (p1.equals("link")) printout();   
  52.     }   
  53.   
  54.     public void characters(char[] p0, int p1, int p2) throws SAXException   
  55.     {   
  56.         String tag = (String) tags.peek();   
  57.         if (tag.equals("text"))   
  58.             text = new String(p0, p1, p2);   
  59.         else  
  60.             if (tag.equals("url"))   
  61.                 url = new String(p0, p1, p2);   
  62.             else  
  63.                 if (tag.equals("author"))   
  64.                     author = new String(p0, p1, p2);   
  65.                 else  
  66.                     if (tag.equals("day"))   
  67.                         day = new String(p0, p1, p2);   
  68.                     else  
  69.                         if (tag.equals("month"))   
  70.                             month = new String(p0, p1, p2);   
  71.                         else  
  72.                             if (tag.equals("year"))   
  73.                                 year = new String(p0, p1, p2);   
  74.                             else  
  75.                                 if (tag.equals("description"))   
  76.                                     year = new String(p0, p1, p2);   
  77.     }   
  78.   
  79.     private void printout()   
  80.     {   
  81.         System.out.print("Content: ");   
  82.         System.out.println(text);   
  83.         System.out.print("URL: ");   
  84.         System.out.println(url);   
  85.         System.out.print("Author: ");   
  86.         System.out.println(author);   
  87.         System.out.print("Date: ");   
  88.         System.out.println(day + "-" + month + "-" + year);   
  89.         System.out.print("Description: ");   
  90.         System.out.println(description);   
  91.         System.out.println();   
  92.     }   
  93.   
  94.     static public void main(String[] args)   
  95.     {   
  96.         String filename = null;   
  97.         boolean validation = false;   
  98.         filename = "links.xml";   
  99.         SAXParserFactory spf = SAXParserFactory.newInstance();   
  100.         SAXParser saxParser = null;   
  101.   
  102.         try  
  103.         {   
  104.             saxParser = spf.newSAXParser();   
  105.         }   
  106.         catch (Exception ex)   
  107.         {   
  108.             System.err.println(ex);   
  109.             System.exit(1);   
  110.         }   
  111.         try  
  112.         {   
  113.             saxParser.parse(new File(filename), new SAXReader());   
  114.         }   
  115.         catch (SAXException se)   
  116.         {   
  117.             System.err.println(se.getMessage());   
  118.             System.exit(1);   
  119.         }   
  120.         catch (IOException ioe)   
  121.         {   
  122.             System.err.println(ioe);   
  123.             System.exit(1);   
  124.         }   
  125.     }   
  126.   
  127. }  

 

Factory版

  1. package com.mobi.bean;   
  2.   
  3. import javax.xml.parsers.*;   
  4. import org.w3c.dom.*;   
  5.   
  6. public class xmldisplay   
  7. {   
  8.     public static void main(String args[])   
  9.     {   
  10.         try  
  11.         {   
  12.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
  13.             DocumentBuilder builder = factory.newDocumentBuilder();   
  14.             Document doc = builder.parse("links.xml");   
  15.             doc.normalize();   
  16.             NodeList links = doc.getElementsByTagName("link");   
  17.             for (int i = 0; i < links.getLength(); i++)   
  18.             {   
  19.                 Element link = (Element) links.item(i);   
  20.                 System.out.print("Content: ");   
  21.                 System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue());   
  22.                 System.out.print("URL: ");   
  23.                 System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());   
  24.                 System.out.print("Author: ");   
  25.                 System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());   
  26.                 System.out.print("Date: ");   
  27.                 Element linkdate = (Element) link.getElementsByTagName("date").item(0);   
  28.                 String day = linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();   
  29.                 String month = linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();   
  30.                 String year = linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();   
  31.                 System.out.println(day + "-" + month + "-" + year);   
  32.                 System.out.print("Description: ");   
  33.                 System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());   
  34.                 System.out.println();   
  35.             }   
  36.         }   
  37.         catch (Exception e)   
  38.         {   
  39.             e.printStackTrace();   
  40.         }   
  41.   
  42.     }   
  43. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值