Xmlmatrix in java umwandeln

nasi60

Grünschnabel
matrix in XML

Hallo
Ich habe folgende xml-Dateiund muss in Java umgewandelt werden.
<?xml version="1.0"?>
<vector type="column">
<cn type="real">1</cn>
<vector type="column">
<cn type="real"> 2</cn>
<cn type="real"> 3 </cn>
<cn type="real"> 4</cn>
<cn type="real"> 5</cn>
</vector>
</vector>




Ich habe folgende Javacode geschrieben aber es wird nur die erste untervector zurückgegeben.
public void readXMLFile(String strFile)
{
try
{
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(strFile));
root = doc.getRootElement();
Element e= root.getChild("vector");

}
catch( Exception ex )
{
ex.printStackTrace();
System.out.println("Error: Could not read XML file!");
System.exit(-1);
}


for(int i = 0; i < root.getChildren().size(); i++) {

org.jdom.Element ele = (Element) root.getChildren().get(i);

System.out.println(ele.getChild("vector").getValue());

}

}
Kann jemend mir bitte helfen,wie ich es weiter machen kann?
 
Zuletzt bearbeitet:
Hallo,

zuerst einmal, schreibe deinen Code bitte das nächste mal in Java-Tags.

Nun zu deinem Problem, probiere es mal mit Rekursion, denn auch Childrens können Children haben. Wenn du deinen Baum mal aufzeichnest siehst du genau, dass dein Root nur ein Children hat.
 
Danke für deine Antwort.
Aber wie kann ich Childrens von die Childrensknoten zurück bekommen?
Ich habe zwei for-Schleifen geschlachtet ineinander geschrieben aber ich bekomme falsche Ergebnis!:confused:
 
Da brauchen wir wohl wieder Code.
Deine Lösung ist auf jeden Fall nicht allgemein einsetzbar, da du 2 Schleifen verwendest.
Eine rekursive Lösung ist daher wohl die besser Wahl.
 
Etwas her, dass ich mich mal mit XML beschäftigt hab ^^ aber ich glaube es müsste in etwa so lauten (ich garantiere nicht auf Fehlerfreiheit, dass ist ohne Compiler und so alles runtergeschrieben ^^):

Java:
public boolean sucheKinder(Element node, String attribut)
{
if(node.getChildren().size()==0) return false;
for(int i = 0; i < node.getChildren().size(); i++)
{
if(sucheKinder(node.getChildren().get(i),attribut)==false)
{
System.out.println(node.getValue()); 
}
return true;
}
}

public void readXMLFile(String strFile)
{
// Reading the content of XML file:
try
{
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(strFile));
root = doc.getRootElement();
boolean hatKinder = sucheKinder(root,"vector");
}
catch( Exception ex )
{
ex.printStackTrace();
System.out.println("Error: Could not read XML file!");
System.exit(-1);
}
}
 
Zuletzt bearbeitet:
Danke.Es hat teilweise richtig funktioniert.
Beim ersten Durchlauf funktioniert sauber aber beim zweiten Durchlauf gibt jedes Vector viermal zurück.Z.B. gibt vielmal
-10.1847164272739032
-.674094233530314391
.448235243498043973e-1
1.0 zurück und dann viermal
99.1439370925200336
8.04204519919789895
0
0 und soweiter.[
Code:
 public boolean sucheKinder(Element node, String attribut)
	    {
	    if(node.getChildren().size()==0) return false;
	    for(int i = 0; i < node.getChildren().size(); i++)
	    {
	
	    if(sucheKinder((Element)node.getChildren().get(i),attribut)==false)
	   {
	    System.out.println(node.getValue());
	    } 
	   
	   }
	    return true;
	    }
  public void readXMLFile(String strFile)
	  {
	  // Reading the content of XML file:
	  try
	  {
	  SAXBuilder builder = new SAXBuilder();
	  Document doc = builder.build(new File(strFile));
	  root = doc.getRootElement();
	  boolean hatKinder = sucheKinder(root,"vector");
	  }
	  catch( Exception ex )
	  {
	  ex.printStackTrace();
	  System.out.println("Error: Could not read XML file!");
	  System.exit(-1);
	  }
	  }
  public void parseFile(String fileName) throws Exception
	    {
		    file = new File(fileName);  
		    // Does specified file exist?
		    if( !file.exists() )
	            throw new Exception("Specified file does not exist!");
		    // Is file readable?
		    if( !file.canRead() )
		    	throw new Exception("Can not read specified file!");
		    // Is file well formed?
		    if( !checkXMLFile(fileName) )
	            throw new Exception("Specified file is not an XML file!");
	        return;
	    }
	    /**
	     *  Returns the root node, 
	     *  determined by ascending to the all parents of the root element.
	     * @return
	     */
	    public Element getRoot()
	    {
	        return root;
	    }
 
Arg ja wo da sProblem für den Fehler liegt ist klar ^^
Probier es mal so:

Java:
public void sucheKinder(Element node, String attribut)
{
	if(node.getChildren().size()==0) 
	{
		System.out.println(node.getValue());
	}
	else
	{
		for(int i = 0; i < node.getChildren().size(); i++)
		{
			sucheKinder(node.getChildren().get(i),attribut);
		}
	}
}

public void readXMLFile(String strFile}
{
	// Reading the content of XML file:
	try
	{
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File(strFile));
		root = doc.getRootElement();
		sucheKinder(root,"vector");
	}
	catch( Exception ex )
	{
		ex.printStackTrace();
		System.out.println("Error: Could not read XML file!");
		System.exit(-1);
	}
}
 
Zurück