import java.io.IOException; import java.io.StringReader; import java.util.Hashtable; import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class XMLParser { public XMLParser() { // default constructor } protected Document makeDOM( String pstr_xml ) throws makeDOMException { try { DocumentBuilderFactory ldbf_factory = DocumentBuilderFactory.newInstance(); DocumentBuilder ldb_builder = ldbf_factory.newDocumentBuilder(); Document ldom_document = ldb_builder.parse( new InputSource( new StringReader( pstr_xml.trim() ) ) ); return ldom_document; } catch( SAXException sxe ){ // parser failed Exception x = sxe; if( sxe.getException() != null ) x = sxe.getException(); //x.printStackTrace(); throw new makeDOMException( sxe.getLocalizedMessage() ); } catch( ParserConfigurationException pce ){ // user setup parser wrong //pce.printStackTrace(); throw new makeDOMException( pce.getLocalizedMessage() ); } catch( IOException ioe ){ throw new makeDOMException( ioe.getLocalizedMessage() ); } //return null; } /** * This function converts node lists of the form * * * value * * * * into hash tables with key => value pairs like * * FOO.BAR.FUD => value * * * @param pnl_nodes Input * @param phsh_hashtable The key->values pairs will be stored here. * @param pstr_prefix The prefix that created the dotted form. * @param pstr_attribute The name of a particular attribute to look for. * If the attribute exists, it add's it's value to the prefix. */ protected void nodeListTree2Hash( NodeList pnl_nodes, Hashtable phsh_hashtable, String pstr_prefix, String pstr_attribute ) { String lstr_dot = "."; try{ for( int i=0; i < pnl_nodes.getLength(); i++ ){ if( pnl_nodes.item(i).hasChildNodes() ){ // if the prefix is blank, don't put a dot in front if( pstr_prefix.length() < 1 ){ lstr_dot = ""; } if( pnl_nodes.item(i).getAttributes().getNamedItem( pstr_attribute ) != null ){ nodeListTree2Hash( pnl_nodes.item(i).getChildNodes(), phsh_hashtable, pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName() + pnl_nodes.item(i).getAttributes().getNamedItem( pstr_attribute ).getNodeValue().trim(), pstr_attribute ); } else { nodeListTree2Hash( pnl_nodes.item(i).getChildNodes(), phsh_hashtable, pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName(), pstr_attribute ); } } else { if( pnl_nodes.item(i).getNodeValue() != null && pnl_nodes.item(i).getNodeValue().trim().length() > 0 ){ phsh_hashtable.put( pstr_prefix, pnl_nodes.item(i).getNodeValue().trim() + "" ); lstr_dot = "."; } } } // end for } catch( NullPointerException e ){ // do nothing //System.out.println( "error " + e.getLocalizedMessage() ); //e.printStackTrace(); } } /** * This version of nodeListTree2Hash will add the value of the first attribute to the prefix. * * @param pnl_nodes * @param phsh_hashtable * @param pstr_prefix */ protected void nodeListTree2Hash( NodeList pnl_nodes, Hashtable phsh_hashtable, String pstr_prefix ) { String lstr_dot = "."; try{ for( int i=0; i < pnl_nodes.getLength(); i++ ){ if( pnl_nodes.item(i).hasChildNodes() ){ if( pstr_prefix.length() < 1 ){ // if the prefix is blank, don't put a dot in front lstr_dot = ""; } if( pnl_nodes.item(i).hasAttributes() ){ nodeListTree2Hash( pnl_nodes.item(i).getChildNodes(), phsh_hashtable, pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName() + pnl_nodes.item(i).getAttributes().toString().trim() ); } else { nodeListTree2Hash( pnl_nodes.item(i).getChildNodes(), phsh_hashtable, pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName() ); } } else { if( pnl_nodes.item(i).getNodeValue() != null && pnl_nodes.item(i).getNodeValue().trim().length() > 0 ){ phsh_hashtable.put( pstr_prefix, pnl_nodes.item(i).getNodeValue().trim() + "" ); lstr_dot = "."; } } } // end for } catch( NullPointerException e ){ // do nothing } } /** Just a convenience method */ protected void nodeListTree2Hash( NodeList pnl_nodes, Hashtable phsh_hashtable ) { nodeListTree2Hash( pnl_nodes, phsh_hashtable, "" ); } /** * Much like NodeListTree2Hash, but it prints the result in a nicely human readable form. * You could also use printHash( nodeListTree2Hash( hashtable ) ), but this prints more cleanly. * * @param pnl_nodes * @param pstr_prefix */ protected void printNodeListTree( NodeList pnl_nodes, String pstr_prefix ) { String lstr_dot = "."; try{ for( int i=0; i < pnl_nodes.getLength(); i++ ){ if( pnl_nodes.item(i).hasChildNodes() ){ if( pstr_prefix.length() < 1 ){ // if the prefix is blank, don't put a dot in front lstr_dot = ""; } if( pnl_nodes.item(i).hasAttributes() ){ printNodeListTree( pnl_nodes.item(i).getChildNodes(), pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName() //+ pnl_nodes.item(i).getAttributes().toString().trim() + pnl_nodes.item(i).getAttributes().item(0).getNodeValue() ); } else { printNodeListTree( pnl_nodes.item(i).getChildNodes(), pstr_prefix + lstr_dot + pnl_nodes.item(i).getNodeName() ); } } else { if( pnl_nodes.item(i).getNodeValue() != null && pnl_nodes.item(i).getNodeValue().trim().length() > 0 ){ System.out.println( pstr_prefix + "" + "\t" + pnl_nodes.item(i).getNodeValue().trim() + "" ); lstr_dot = "."; } } } } catch( NullPointerException e ){ // do nothing System.out.println( pstr_prefix + " Same error" ); e.printStackTrace(); } } protected String getFromHash( Hashtable phsh_hash, String pstr_key ) { if( phsh_hash.containsKey( pstr_key ) ){ return phsh_hash.get(pstr_key).toString(); } else { return null; } } public static String hash2String( Hashtable phsh_hashtable ) { Object lobj_key; String tmp = ""; Iterator litr_valueIter = phsh_hashtable.values().iterator(); Iterator litr_keyIter = phsh_hashtable.keySet().iterator(); while( litr_keyIter.hasNext() ){ tmp += litr_keyIter.next() + "\t\t" + litr_valueIter.next() + "\n"; } return tmp; } public static void printHash( Hashtable phsh_hashtable ) { System.out.println( hash2String(phsh_hashtable) ); } // EXCEPTIONS protected class makeDOMException extends Exception { /* (non-Javadoc) * @see java.lang.Throwable#getLocalizedMessage() */ private String gstr_message = "Error Creating DOM"; public makeDOMException(){} public makeDOMException( String pstr_message ) { gstr_message = pstr_message; } public String getLocalizedMessage() { return gstr_message; } } }