最不通用的做法
public Class yg
{
  public int id = -1;
  public String name = null;
  pubcli String password = null;
  public yg(){}
}
public function getYG(Document doc)
{
   yg myyg = new yg()
   myyg.id = ....;
   myyg.name = ...;
   myyg.password = ...;
   return myyg;
}

解决方案 »

  1.   

    如果你的意思是有某些函数,如
    public Object getObject(Document doc)
    {
      ..
    }只需传入一个doc就可以得到一个对象,不需预先定义。我劝你不要发梦,因为你不知道他的型(type)。你必须要造型。得到Object >> 想取属性 >> 需造形 >> 这个形你怎么来????用集合代替,如map
    key - value,近乎 Object。较好解决方案
      

  2.   

    C:\JBuilder8\samples\Tutorials\XML\saxparser里面有现成的例子
    package com.borland.samples.xml.saxparser;import java.io.IOException;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import javax.xml.parsers.*;public class MySaxParser
        extends DefaultHandler {
      private static int INDENT = 2;
      private static String attList = "";
      public static void main(String[] argv) {
        if (argv.length != 1) {
          System.out.println(
              "Usage: java com.borland.samples.xml.saxparser.MySaxParser [URI]");
          System.exit(0);
        }
        System.setProperty("javax.xml.parsers.SAXParserFactory",
                           "org.apache.xerces.jaxp.SAXParserFactoryImpl");
        String uri = argv[0];
        try {
          SAXParserFactory parserFactory = SAXParserFactory.newInstance();
          parserFactory.setValidating(false);
          parserFactory.setNamespaceAware(false);
          MySaxParser MySaxParserInstance = new MySaxParser();
          SAXParser parser = parserFactory.newSAXParser();
          parser.parse(uri, MySaxParserInstance);
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
        catch (SAXException ex) {
          ex.printStackTrace();
        }
        catch (ParserConfigurationException ex) {
          ex.printStackTrace();
        }
        catch (FactoryConfigurationError ex) {
          ex.printStackTrace();
        }  }  private int idx = 0; //indent
      public void characters(char[] ch, int start, int length) throws SAXException {
        //instantiates s, indents output, prints character values in element
        String s = new String(ch, start, length);
        if (!s.startsWith("\n")) {
          System.out.println(getIndent() + " Value: " + s);
        }
      }  public void endDocument() throws SAXException {
        idx -= INDENT;
        System.out.println(getIndent() + "end document");
        System.out.println("...PARSING ends");  }  public void endElement(String uri, String localName, String qName) throws
          SAXException {
        if (!attList.equals("")) {
          System.out.println(getIndent() + " Attributes: " + attList);
        }
        attList = "";    System.out.println(getIndent() + "end element");
        idx -= INDENT;
      }  public void startDocument() throws SAXException {
        idx += INDENT;
        System.out.println("PARSING begins...");
        System.out.println(getIndent() + "start document: ");  }  private String getIndent() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < idx; i++) {
          sb.append(" ");
        }
        return sb.toString();
      }  public void startElement(String uri, String localName, String qName,
                               Attributes attributes) throws SAXException {
        idx += INDENT;
        System.out.println('\n' + getIndent() + "start element: " + qName);
        if (attributes.getLength() > 0) {
          idx += INDENT;
          for (int i = 0; i < attributes.getLength(); i++) {
            attList = attList + attributes.getQName(i) + " = " +
                attributes.getValue(i);
            if (i < (attributes.getLength() - 1)) {
              attList = attList + ", ";
            }
          }
          idx -= INDENT;
        }  }}