XML mit Apache Commons Configuration

MrCastle

Mitglied
Hi community,

ich stehe mal wieder vor einem Problem, dieses Mal geht es, wie der Titel bereits sagt, um das Auslesen von XML Dateien mit Apache Commons Configuration v1.6.

Folgende XML-Datei liegt vor:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<tformats>
	<formats>
		<format description="f2" pattern="abc"/>
		<format description="f1" pattern="abc"/>
	</formats>
</tformats>

Mit folgendem Code habe ich bisher versucht die einzelnen Elemente, bzw. die Größe der Liste zu bekommen:

Code:
                XMLConfiguration tsfconfig = new XMLConfiguration("conf\\tformats.xml");
                List tsfPropList = tsfconfig.getList("formats");
	    	int listSize = tsfPropList.size();
	    	int tsfCount = 0;
	    	
	    	while(tsfCount < listSize){
    			String name = tsfconfig.getString("formats.format("+tsfCount+")[@description]");
    			String pattern = tsfconfig.getString("formats.format("+tsfCount+")[@pattern]");
    			
    			tsfList.add(new TFormat(name, pattern));    			
    			tsfCount++;
    		}

und

Code:
                XMLConfiguration tsfconfig = new XMLConfiguration("conf\\tformats.xml");
                Object tsfProp = tsfconfig.getProperty("formats");
	    	if(tsfProp instanceof Collection)
	    	{
	    		int size = ((Collection) tsfProp).size()-1;
	    		System.out.println("Size: " + size);
	    		int count = 0;
	    		while(count < size){
	    			String name = tsfconfig.getString("format("+count+")[@description]");
	    			String pattern = tsfconfig.getString("format("+count+")[@pattern]");
	    			
	    			tsfList.add(new TFormat(name, pattern));
	    			
	    			count++;
	    		}
	    	}

Beides funktioniert aber nicht, ich bekomme mit getProperty() nur "null" und mit getList() eine leere Liste zurück. Änderung des Pfads auf "tformats.formats" hat auch nicht funktioniert.

Ich finde auch irgendwie kaum Beispiele und falls ich welche finde, dann sehen sie aus wie der oben beschriebene Code, der ja nicht funktioniert.

Was übersehe ich? Habe mich dabei an den Beispielen auf der Apache Seite orientiert.
Vielen Dank im Voraus

Gruß
MrCastle
 
Zuletzt bearbeitet:
Hallo,

die get-Methoden (getProperty, getList, getString) beziehen sich auf Attributs- und Elementinhalte, nicht auf die eigentlichen Elemente.

folgendes sollte funktionieren:
Code:
List tsfPropList = tsfconfig.getList("formats.format.description");

// oder
Object prop = tsfconfig.getProperty("formats.format.description");
if(prop instanceof Collection)
{
    System.out.println("Number of descriptions: " + ((Collection) prop).size());

}
 

Neue Beiträge

Zurück