DOM教程
刚才我10分钟就看玩了
http://resource.xml.org.cn/tutorial/ibm/x-udom/x-udom-2-3.html

解决方案 »

  1.   

    public class XMLReader { public void list() throws Exception {
    SAXParser parser = new SAXParser();
    parser.setContentHandler(new PeopleHandler());
    parser.parse("people.xml"); } public static void main(String[] args) throws Exception {
    new XMLReader().list();
    }}class PeopleHandler extends DefaultHandler { private boolean name = false;
    private boolean mail = false;
    private Properties property = null;
    private StringBuffer sb = new StringBuffer(); public PeopleHandler() {
    property = new Properties();
    } public void startElement(
    String nsURI,
    String stripName,
    String tagName,
    Attributes attributes)
    throws SAXException {
    sb.delete(0, sb.length());
    } public void characters(char[] ch, int start, int length)
    throws SAXException {
    sb.append(ch, start, length);
    } public void endElement(String nsURI, String stripName, String tagName)
    throws SAXException {
    property.put(tagName, sb.toString().trim());
    sb.delete(0, sb.length());
    System.out.println( property.getProperty(tagName));
    } public void printXML() {
    Enumeration propertyNames = property.propertyNames();
    while (propertyNames.hasMoreElements()) {
    String propertyName = (String) propertyNames.nextElement();
    System.out.println("PropertyName" + propertyName);
    System.out.println("Value" + property.getProperty(propertyName));
    }
    }
    }
      

  2.   

    <?xml version="1.0"?>
    <people>
    <person>
    <name>fantasyCoder</name>
    <email>[email protected]</email>
    </person>
    </people>
      

  3.   


     *
     *******************************************************************/import org.w3c.dom.*;public class XmlUtils{    /**
         Return an Element given an XML document, tag name, and index
         
         @param     doc     XML docuemnt
         @param     tagName a tag name
         @param     index   a index
         @return    an Element
         */
        public static Element getElement( Document doc , String tagName ,
                                          int index ){
            //given an XML document and a tag
            //return an Element at a given index
            NodeList rows = doc.getDocumentElement().getElementsByTagName(
                    tagName );
            return (Element)rows.item( index );
        }    /**
         Return the number of person in an XML document
         
         @param     doc     XML document
         @param     tagName a tag name
         @return    the number of person in an XML document
         */
        public static int getSize( Document doc , String tagName ){
            //given an XML document and a tag name
            //return the number of ocurances 
            NodeList rows = doc.getDocumentElement().getElementsByTagName(
                    tagName );
            return rows.getLength();
        }    /**
          Given a person element, must get the element specified
          by the tagName, then must traverse that Node to get the
          value.
          Step1) get Element of name tagName from e
          Step2) cast element to Node and then traverse it for its
                   non-whitespace, cr/lf value.
          Step3) return it!
            
          NOTE: Element is a subclass of Node
          
          @param    e   an Element
          @param    tagName a tag name
          @return   s   the value of a Node 
        */
        public static String getValue( Element e , String tagName ){
        try{
            //get node lists of a tag name from a Element
            NodeList elements = e.getElementsByTagName( tagName );        Node node = elements.item( 0 );
            NodeList nodes = node.getChildNodes();
            
            //find a value whose value is non-whitespace
            String s;
            for( int i=0; i<nodes.getLength(); i++){
                s = ((Node)nodes.item( i )).getNodeValue().trim();
                if(s.equals("") || s.equals("\r")) {
                    continue;
                }
                else return s;
            }    }
        catch(Exception ex){
            System.out.println( ex );
            ex.printStackTrace();
        }
            
        return null;    }    /**
         For testing purpose, it print out Node list
         
         @param     rows    a Nodelist
         */
        public static void printNodeTypes( NodeList rows ){
            System.out.println( "\tenumerating NodeList (of Elements):");
            System.out.println( "\tClass\tNT\tNV" );
            //iterate a given Node list
            for( int ri = 0 ; ri < rows.getLength() ; ri++){
                Node n = (Node)rows.item( ri );
                if( n instanceof Element) {
                    System.out.print( "\tElement" );
                }
                else System.out.print( "\tNode" );
        
                //print out Node type and Node value
                System.out.println(
                    "\t"+
                    n.getNodeType() + "\t" +
                    n.getNodeValue()
                    );
            }
            System.out.println();
        }
    }