HashTable auslesen und in createDocument ausgeben

Status
Nicht offen für weitere Antworten.

pik0r

Mitglied
Hallo, ich hab eine gefüllte HashTable

this.table = new Hashtable<String, Object>();
this.table.put("Modelversion", new POMHeader().getPModelVersion());
this.table.put("groupID", new String(""));
this.table.put("packaging", new String(""));
this.table.put("Version", new String(""));

als Beispiel.

jetzt möchte ich es in eine DOM bauen und weiss leider nicht wie=(

einer ne Idee? Der Grund ist nachdem ich den String(XMLString) erstellt habe wie ich es in eine XML-Datei abspeichern... sozusagen eine xml generieren.

hier das document doc:

document doc = this.createDocument();
Node rootnode = doc.createElement("Project");
doc.appendChild(rootnode);

this.addEntry(doc, "pikor", "male", "bla");
this.addEntry(doc, "du", "nüx", "was?");
this.addEntry(doc, "sie", "female", "sagtsienich");

MyXML = XMLGeneration.getXMLString(doc);

System.out.println("Test-Ausgabe");
//System.out.println(MyXML);
wenn ich den dom so baue, dann klappt alles, aber wie trage ich nun die daten aus der hash ein? anstatt "pikor male bla" etc?!

danke für eure hilfe

p.s. schön waeren codebeispiele
 
ausgeben in die syso kann ich, ich möchte die daten der hashtable zum beispiel:

Code:
Hashtable<String, Object> table;

//..
//...

//dann :

public Hashtable createTable() {
		this.table = new Hashtable<String, Object>();
		this.table.put("Modelversion", new String("TEST"));
		this.table.put("groupID", new String("SlaterB-FAN"));
		this.table.put("packaging", new String("jar"));
		this.table.put("Version", new String("4.0.0"));
		
		return table;
	} 

//mit der Methode gibt er es aus:

public void putData(){
		
		Enumeration e = this.table.keys();
	    Object obj;
	    while (e.hasMoreElements()) {
	      obj = e.nextElement();
	      System.out.println(obj + ": " + this.table.get(obj));
	    }
		
	}


jetzt will es nicht in syso ausgeben, sondern in die dom legen...

Frage wie?

hier die andere class:

Code:
public String MyXML;
	
	
	
    Document createDocument() throws Exception {
        DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
        DocumentBuilder        builder  = factory.newDocumentBuilder();
        Document               document = builder.newDocument();
        if (document == null){ 
             throw new Exception("Kein Document erstellt");
        }
       
        return document;
    }
    
  
	public XMLGeneration() // String XMLhelp
    {
	
        try
        {
            Document doc = this.createDocument();
                      // Rootnode erstellen
            Node rootnode = doc.createElement("Project");
            doc.appendChild(rootnode);
            
            
            // tag - typ - inhalt
            this.addEntry(doc, "pik0r", "male", "blub");
            this.addEntry(doc, "du", "nüx", "was?");
            this.addEntry(doc, "sie", "female", "sagtsienich");
            
            MyXML = XMLGeneration.getXMLString(doc);
        
            System.out.println("Test-Ausgabe");
            //System.out.println(MyXML);
    
                
        } catch (Exception e)
        {
            // TODO Automatisch erstellter Catch-Block
            e.printStackTrace();
        } 
    }


// ...

public static String getXMLString(Document pDocument)
    {
        try
        {
            
            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            DOMSource source = new DOMSource(pDocument);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);

            return os.toString();
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return "";
    }

//...
private void addEntry(Document doc, String pName, String pGender, String pTel)
    {
        Node rootnode = doc.getFirstChild(); 
      
        Node name = doc.createElement(pName);
        ((Element)name).setAttribute("gender", pGender);
        Node tel = doc.createElement("telefon");
        Node telNr = doc.createTextNode(pTel);
        tel.appendChild(telNr);
        name.appendChild(tel);

        rootnode.appendChild(name);
    }

so wie bau ich das um, das es mir die hash ausgibt? also anstatt gender oder telefon den namen aus der hashtable "pik0r" etc !!

danke für deine help
 
verstehe ich nicht,
statt
this.addEntry(doc, "pik0r", "male", "blub");
eben
this.addEntry(doc, eintrag aus map);
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben