这是其中之一,附件更多。。。
SAX版
java 代码
- package com.mobi.bean;
- import org.xml.sax.helpers.DefaultHandler;
- import javax.xml.parsers.*;
- import org.xml.sax.*;
- import org.xml.sax.helpers.*;
- import java.util.*;
- import java.io.*;
- public class SAXReader extends DefaultHandler
- {
- java.util.Stack tags = new java.util.Stack();
- // --------------XML Content-------------
- String text = null;
- String url = null;
- String author = null;
- String description = null;
- String day = null;
- String year = null;
- String month = null;
- // ----------------------------------------------
- public void endDocument() throws SAXException
- {
- System.out.println("------Parse End--------");
- }
- public void startDocument() throws SAXException
- {
- System.out.println("------Parse Begin--------");
- }
- public void startElement(String p0, String p1, String p2, Attributes p3)
- throws SAXException
- {
- tags.push(p1);
- }
- public void endElement(String p0, String p1, String p2) throws SAXException
- {
- tags.pop();
- if (p1.equals("link")) printout();
- }
- public void characters(char[] p0, int p1, int p2) throws SAXException
- {
- String tag = (String) tags.peek();
- if (tag.equals("text"))
- text = new String(p0, p1, p2);
- else
- if (tag.equals("url"))
- url = new String(p0, p1, p2);
- else
- if (tag.equals("author"))
- author = new String(p0, p1, p2);
- else
- if (tag.equals("day"))
- day = new String(p0, p1, p2);
- else
- if (tag.equals("month"))
- month = new String(p0, p1, p2);
- else
- if (tag.equals("year"))
- year = new String(p0, p1, p2);
- else
- if (tag.equals("description"))
- year = new String(p0, p1, p2);
- }
- private void printout()
- {
- System.out.print("Content: ");
- System.out.println(text);
- System.out.print("URL: ");
- System.out.println(url);
- System.out.print("Author: ");
- System.out.println(author);
- System.out.print("Date: ");
- System.out.println(day + "-" + month + "-" + year);
- System.out.print("Description: ");
- System.out.println(description);
- System.out.println();
- }
- static public void main(String[] args)
- {
- String filename = null;
- boolean validation = false;
- filename = "links.xml";
- SAXParserFactory spf = SAXParserFactory.newInstance();
- SAXParser saxParser = null;
- try
- {
- saxParser = spf.newSAXParser();
- }
- catch (Exception ex)
- {
- System.err.println(ex);
- System.exit(1);
- }
- try
- {
- saxParser.parse(new File(filename), new SAXReader());
- }
- catch (SAXException se)
- {
- System.err.println(se.getMessage());
- System.exit(1);
- }
- catch (IOException ioe)
- {
- System.err.println(ioe);
- System.exit(1);
- }
- }
- }
Factory版
- package com.mobi.bean;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- public class xmldisplay
- {
- public static void main(String args[])
- {
- try
- {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse("links.xml");
- doc.normalize();
- NodeList links = doc.getElementsByTagName("link");
- for (int i = 0; i < links.getLength(); i++)
- {
- Element link = (Element) links.item(i);
- System.out.print("Content: ");
- System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue());
- System.out.print("URL: ");
- System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
- System.out.print("Author: ");
- System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
- System.out.print("Date: ");
- Element linkdate = (Element) link.getElementsByTagName("date").item(0);
- String day = linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
- String month = linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
- String year = linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
- System.out.println(day + "-" + month + "-" + year);
- System.out.print("Description: ");
- System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
- System.out.println();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }