这是一个采用sax方式读取xml的程序:import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;import java.util.*;
import java.io.*;
public class SAXTagCount extends DefaultHandler {
    static String path="";
static String tag=".";
    public void startDocument() throws SAXException {
        
    }

    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)
throws SAXException{
      if(path.equals(""))
path=qName;
  else
path=path+tag+qName;  
    }    public void endDocument() throws SAXException {
    } public void characters(char[] ch, int start, int length){
System.setProperty(path,new String(ch,start,length));
} public void endElement(String uri, String localName, String qName){        
if(path.length()==qName.length())
  path="";
else
  path=path.substring(0,path.length()-((qName+tag).length()));
}
    static public void main(String[] args) {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
        XMLReader xmlReader = null;
        try {
            SAXParser saxParser = spf.newSAXParser();
           // xmlReader = saxParser.getXMLReader();
saxParser.parse(new File(args[0]),new SAXTagCount());
        } catch (Exception ex) {
            System.err.println(ex.toString());
        }
        //xmlReader.setContentHandler(new SAXTagCount());
System.getProperties().list(System.out);
}
}
运行环境 jdk1.4 你看看对你有没有帮助祝你好运!

解决方案 »

  1.   

    JDom就可以!
    http://www.cafeconleche.org/books/xmljava/这又一本树
    就写的这个,里面有你要的!
      

  2.   

    抱歉抱歉,没有测试我的程序,这个程序我测试了,我觉得可以满足你的要求:
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;import java.util.*;
    import java.io.*;
    public class SAXTagCount extends DefaultHandler {
        static String path="";
    static String tag=".";
        public void startDocument() throws SAXException {
            
        }

        public void startElement(String namespaceURI, String localName,
                                 String qName, Attributes atts)
    throws SAXException{
          if(path.equals(""))
    path=qName;
      else
    path=path+tag+qName;  
        }    public void endDocument() throws SAXException {
        } public void characters(char[] ch, int start, int length){
        System.out.println(path+"="+new String(ch,start,length).trim());
    } public void endElement(String uri, String localName, String qName){        
    if(path.length()==qName.length())
      path="";
    else{
    try{
    path=path.substring(0,path.length()-((qName+tag).length()));
    }catch(Exception e){}
    }
    }
    static public void main(String[] args) {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            spf.setValidating(true);
            try {
                SAXParser saxParser = spf.newSAXParser();
    saxParser.parse(new File(args[0]),new SAXTagCount());
            } catch (Exception ex) {
                System.err.println(ex.toString());
            }
    }
    }祝你好运!
      

  3.   

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.apache.crimson.tree.XmlDocument;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;public void test(){XmlDocument xdoc;
    String filename = "";
    Document doc = null;
    File docFile = new File("c:\\SystemConfig.xml");try {
    //first Create DocumentBuilderFactory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //dbf.setValidating(true); //second Create  DocumentBuilder
    DocumentBuilder db = dbf.newDocumentBuilder(); //third Create Dom
    doc = db.parse(docFile);} catch (Exception e) {
    System.out.print("Problem parsing the file.");
    }//STEP 1:  Get the root element
    Element root = doc.getDocumentElement();//STEP 2:  Get the children
    NodeList orders = root.getElementsByTagName("here wirte your param name");
    for (int orderNum = 0; orderNum < orders.getLength(); orderNum++) { filename = orders.item(orderNum).getFirstChild().getNodeValue();
    System.out.println(orders.item(orderNum).getFirstChild().getNodeValue()); }
    }
      

  4.   

    kknd97(绝地风暴>
    没有xml文件呀!