27. november 2002 - 16:11Der er
16 kommentarer og 1 løsning
XML - HashMap
Er der nogen kan fortælle mig hvorfor: At inde i min handler består min HashMap ganske rigtigt af alle elementer i XML filen... Men i min main består HashMap'en kun af det sidste element i XML filen????
public class HashMapHandler extends DefaultHandler { public HashMapHandler(HashMap map) { this.map = map; }
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { map = new HashMap();
if(rawName.equals("statement")) { for(int i = 0; i < atts.getLength(); i++) { String name = atts.getValue("name"); String sql = atts.getValue("SQL"); map.put(name, sql); System.out.println(map.values()); } } }
public class Test { public static void main(String []args) throws Exception { HashMap map = new HashMap(); HashMapHandler handler = new HashMapHandler(map);
Jeg ved ikke om jeg er helt med, men dette er den redigerede kode!
public class HashMapHandler extends DefaultHandler { public HashMapHandler(HashMap map) { this.map = map; }
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { if(rawName.equals("statement")) { for(int i = 0; i < atts.getLength(); i++) { String name = atts.getValue("name"); String sql = atts.getValue("SQL"); map.put(name, sql); //System.out.println(map.values()); } } }
private HashMap map; }
Main
public class Test { public static void main(String []args) throws Exception { HashMap map = new HashMap(); HashMapHandler handler = new HashMapHandler(map);
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.