Veröffentlicht 20. November 200915 j Ich habe ein Application.dtd <!ELEMENT Applications (Application*)> <!ELEMENT Application (Name,Pfad,JarFileName,MainClass,Subclasses,Application*)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Pfad (#PCDATA)> <!ELEMENT JarFileName (#PCDATA)> <!ELEMENT MainClass (#PCDATA)> <!ELEMENT Subclasses (Subclass*) > <!ELEMENT Subclass (#PCDATA)> ein Application.xml <?xml version="1.0" encoding='ISO-8859-1'?> <!DOCTYPE Applications SYSTEM "Application.dtd"> <!-- <!DOCTYPE Application SYSTEM "Application.dtd"> --> <!-- Wurzelelement--> <Applications> <Application> <Name>MainApplication</Name> <Pfad>.</Pfad> <JarFileName>test.jar</JarFileName> <MainClass>Main</MainClass> <Subclasses> <Subclass>Howdy</Subclass> <Subclass>TestClass</Subclass> </Subclasses> <Application> <Name>MainApplication</Name> <Pfad>.</Pfad> <JarFileName>test.jar</JarFileName> <MainClass>Main</MainClass> <Subclasses> <Subclass>Howdy</Subclass> <Subclass>TestClass</Subclass> </Subclasses> </Application> </Application> </Applications> mein Code sieht wie folgt aus: import org.jdom.input.SAXBuilder; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.*; String DateiName = "Application.xml"; Document doc = null; SAXBuilder b = new SAXBuilder(true); // validierenden Parser nutzen try { doc = b.build(new File(DateiName)); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Element root= new Element("Applications"); System.out.println(root.toString()); Element t = root.getChild("Application"); // Name der Application als String speichern String name = t.getChild("Name").getText(); System.out.println(name); Element tnext = root.getChild("Application"); name = t.getChild("Name").getText(); System.out.println(name); } catch (Exception e) { // Ausnahmebehandlung hier nur rudimentär System.out.println(e.toString()); } Mein Problem: root, sieht so aus als ob es leer wäre? Wenn ich Element t = root.getChild("Application"); aufrufe, ist t null. Was mache ich falsch? Ich danke für eure Hilfe. Georg
20. November 200915 j Das ist doch auch vollkommen klar wieso dein Element t null ist. Denn du erzeugst ein neues Element willst aber eigentlich mit dem Docuement arbeiten. Element root = doc.getRootElement(); // jetzt hast du das root Element und kannst damit weiter arbeiten
20. November 200915 j Ich danke recht herzlich, oft sieht man den Wald vor lauter Bäumen nicht. Danke für die Hilfe. Georg
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.