Zum Inhalt springen

Empfohlene Beiträge

Geschrieben

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

Geschrieben

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


Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...