Problème de lecture d'un fichier XML avec Java
Bonjour,
J'ai un petit probleme en Java, je n'arrive pas à afficher ce que je veux pour un fichier XML.
Le but étant d'avoir le temps de toutes les station à toutes les stations :
Départ STN10 : arrivée STN11 --- Temps : ...
Départ STN10 : Arrivée STN12 --- Temps : ...
etc//
Départ STN11 : Arrivée STN10 --- Temps : ...
Je travaille sur un fichier XML mais je n'arrive pas à afficher tous les élément internes d'une liste . voici mon fichier XML : http://dl.free.fr/mXmBGlPpN
Et voila mon programme :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
public class TimeCalcul
{
private static final String xmlroutes = "C://Users//Desktop//routes.xml";
static Element root;
public static void main(String[] args) throws JDOMException, IOException
{
// the SAXBuilder is the easiest way to create the TimeCalcul objects.
SAXBuilder jdomBuilder = new SAXBuilder();
// jdomDocument is the TimeCalcul Object
Document jdomDocument = jdomBuilder.build(xmlroutes);
root = jdomDocument.getRootElement();
rootiteration();
public static List<Element> getNextPropChildren(Element loc){
Element segment_properties = loc.getChild("SEGMENT_PROPERTIES");
List <Element> next_location = segment_properties.getChildren("NEXT_LOCATION");
return next_location;
}
public static Element getElembyName(final String name){
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
for (Element loc : location_properties)
{
if(loc.getAttributeValue("NAME").equals(name))
return loc;
}
return null;
}
public static int Timetochild (Element next)
{
int L = Integer.parseInt(next.getAttributeValue("LENGTH"));
int S = Integer.parseInt(next.getAttributeValue("SPEED"));
int time = L/S;
return time;
}
public static void rootiteration() throws IOException
{
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
for (Element loc : location_properties)
{
if(loc.getAttributeValue("NAME").startsWith("STN")== true)
{
System.out.print("From : "+loc.getAttributeValue("NAME"));
List <Element> next_location = getNextPropChildren(loc);
for (Element next: next_location)
{
int time = Timetochild(next);
System.out.print(" to " +next.getAttributeValue("NAME"));
System.out.println(" --- Time to go : "+time+" seconds");
if(next.getAttributeValue("NAME").startsWith("STN")== true)
{
System.out.print("Arrival : " +next.getAttributeValue("NAME"));
System.out.println(" --- Total Time : "+time+" seconds");
}
if(next.getAttributeValue("NAME").startsWith("STN")== false)
{
Element next_elem = getElembyName(next.getAttributeValue("NAME"));
//System.out.println(next_elem);
recursive(loc.getAttributeValue("NAME"),time, next_elem);
}
}
}
}
}
public static void recursive(String parent, int t0, Element child) throws IOException
{
List <Element> listnext_location = getNextPropChildren(child);
for (Element next : listnext_location )
{
int t1 = Timetochild(next);
if (next.getAttributeValue("NAME").startsWith("STN")== true)
{
// while(next!=null){
System.out.println("Here1");
int time = t0+t1;
System.out.println("From : "+parent+" to "+next.getAttributeValue("NAME")+" --- Total Time : "+time+" seconds");
break;
// }
}
if (next.getAttributeValue("NAME").startsWith("STN")== false) // child is not STN, recurse
{
//System.out.println("Here2");
int time = t0+t1;
System.out.print("From : "+parent+" to "+next.getAttributeValue("NAME"));
System.out.println(" --- Time to go : "+time+" seconds");
Element elem = getElembyName(next.getAttributeValue("NAME"));
recursive(parent, time,elem);
}
}
}
} |
je n'obtient que cela..
From : STN10 to X535 --- Time to go : 15 seconds
From : STN10 to X536 --- Time to go : 17 seconds
From : STN10 to X537 --- Time to go : 19 seconds
Here1
From : STN10 to STN26 --- Total Time : 30 seconds
Here1
From : STN10 to STN23 --- Total Time : 29 seconds
Here1
From : STN10 to STN19 --- Total Time : 26 seconds
From : STN11 to X535 --- Time to go : 15 seconds
From : STN11 to X536 --- Time to go : 17 seconds
From : STN11 to X537 --- Time to go : 19 seconds
Here1
From : STN11 to STN26 --- Total Time : 30 seconds
Here1
From : STN11 to STN23 --- Total Time : 29 seconds
Here1
From : STN11 to STN19 --- Total Time : 26 seconds
From : STN12 to X534 --- Time to go : 7 seconds
From : STN12 to X535 --- Time to go : 9 seconds
From : STN12 to X536 --- Time to go : 11 seconds
ETC//