网上有很多解析XML文件的第三方包,自己也可以写个简单的.

解决方案 »

  1.   

    可以根据一些java xml中的包来实现自己对xml文件的解析!
      

  2.   

    用SAX 和 Jdom都行,下面给出源码------SAX-----import org.xml.sax.Attributes;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.SAXException;
    import java.util.Properties;public class ConfigurationParser extends DefaultHandler {  private Properties props;  private String currentSet;
      private String currentName;
      private StringBuffer currentValue = new StringBuffer();  public ConfigurationParser() {
        this.props = new Properties();
      }  public Properties getProps() {
        return this.props;
      }//定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
      public void startElement(String uri, String localName,
                               String qName, Attributes attributes)
          throws SAXException {
        currentValue.delete(0, currentValue.length());
        this.currentName =qName;
      }//这里是将<xxx></xxx>之间的值加入到currentValue
      public void characters(char[] ch, int start, int length)
          throws SAXException {
        currentValue.append(ch, start, length);
      }//在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
      public void endElement(String uri, String localName,
                             String qName) throws SAXException {    props.put(qName.toLowerCase(), currentValue.toString().trim());
      }
    }
    import java.util.Properties;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;public class ParserConXML {  public ParserConXML() {
      }  public Properties getProps() {
        Properties props = new Properties();
        SAXParser parser;
        ConfigurationParser handler = new ConfigurationParser();    //获取SAX工厂对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);    try
        {
          //获取SAX解析
          parser = factory.newSAXParser();
          //将解析器和解析对象联系起来,开始解析
          parser.parse("configuration.xml", handler);
          props = handler.getProps();
        }catch(Exception e){
        }
        finally{
          factory=null;
          parser=null;
          handler=null;
        }
        return props;
      }
    /*
      public static void main(String[] args) {
        ParserConXML test = new ParserConXML();
        Properties prop = test.getProps();
        System.out.println("-------"+prop.getProperty("db_url"));
      }
    */
    }
    ---------JDOM-----------import org.jdom.*;
    import org.jdom.input.*;public class XMLConfigParser {
      private Element elementRoot;
      public XMLConfigParser() {
        try {
          SAXBuilder saxbuilder = new SAXBuilder();
          Document doc = saxbuilder.build("configration.xml");
          elementRoot = doc.getRootElement();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }    public Element getChildElement(String elementName){
          return elementRoot.getChild(elementName);
        }
    }import org.jdom.*;public class InitParameter {
      private static XMLConfigParser xmlConfigParser = new XMLConfigParser();
      private static Element connPoolE =
          xmlConfigParser.getChildElement("connectionpool");  public InitParameter() {
      }  public static String getUrl() {
        return connPoolE.getChildTextTrim("url");
      }  public static String getDatasource() {
        return connPoolE.getChildTextTrim("datasource");
      }
    }