public boolean loadFromXML ( File file ) throws ParserConfigurationException , SAXException , IOException { Configuration newConf = new Configuration ( this . conf ) ; DocumentBuilder builder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ; Document document = builder . parse ( file ) ; Element root = document . getDocumentElement ( ) ; if ( ! "configuration" . equals ( root . getTagName ( ) ) ) return false ; NodeList props = root . getChildNodes ( ) ; for ( int i = 0 ; i < props . getLength ( ) ; i ++ ) { Node propNode = props . item ( i ) ; if ( ! ( propNode instanceof Element ) ) continue ; Element prop = ( Element ) propNode ; if ( ! "property" . equals ( prop . getTagName ( ) ) ) return false ; NodeList fields = prop . getChildNodes ( ) ; String attr = null ; String value = null ; for ( int j = 0 ; j < fields . getLength ( ) ; j ++ ) { Node fieldNode = fields . item ( j ) ; if ( ! ( fieldNode instanceof Element ) ) continue ; Element field = ( Element ) fieldNode ; if ( "name" . equals ( field . getTagName ( ) ) ) attr = ( ( Text ) field . getFirstChild ( ) ) . getData ( ) ; if ( "value" . equals ( field . getTagName ( ) ) && field . hasChildNodes ( ) ) value = ( ( Text ) field . getFirstChild ( ) ) . getData ( ) ; } if ( attr != null && value != null ) newConf . set ( attr , value ) ; } this . conf = newConf ; return true ; }
Overwrite this location with settings available in the given XML file . The existing configuration is preserved if the XML file is invalid .