试图从一个XML文件中读取ID到不同的列表?

时间:2018-03-15 16:11:21

标签: java xml list

我正在尝试做的是读取每个Station,Track,Light和switch的ID,然后创建它们的新实例,然后将它们添加到各自的列表中,然后将所有这些组件添加到主列表中这叫做Rails。 但似乎在我的方法中,当进入时没有事件进入if,else语句的主体。

基本上,我想要阅读,有一个轨道,让它循环通过它,如果站点制作一个站点,将它添加到站点列表,然后添加到主列表,然后如果轨道,创建轨道将其添加到轨道列表,然后到主列表,依此类推其他对象。

XML文件

<?xml version="1.0"?>

<Rails>
    <Rail>  <!--Rail 1-->
        <Station> ST00 </Station>   <!--Station are type of track-->
        <Track> T01 </Track>
        <Track> T02 </Track>
        <Light> L01 </Light>
        <Track> T03 </Track>
        <Switch>S01 </Switch>
        <Track> T04 </Track>
        <Track> T05 </Track>
        <Light> L02 </Light>
        <Track> T06 </Track>
        <Station> ST01 </Station>
    </Rail>
    <!--Rail intermidate(here we will add the intermidate rail for the switch)-->
</Rails>

这是arrayList和方法(读取并存储它们),我正在尝试添加它们。

    public ArrayList<Object> rail = new ArrayList<>();
    public ArrayList<Track> stations = new ArrayList<>();  //Stations are type of track
    public ArrayList<Switch> setSwitches = new ArrayList<>();
    public ArrayList<Track> tracks = new ArrayList<>();
    public ArrayList<Light> lights = new ArrayList<>();


public void readXML(){
        String id;
        boolean doneLoading = false;


        try{
            File xmlFile = new File("/Users/luisgarcia-maldonado/Desktop/TDJ/cs351/smartRail/src/mapping.xml");
            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbfactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Rails");


            Track station = null;
            Track trackOn = null;
            for(int i = 0; i < nList.getLength(); i++){
                Node nNode = nList.item(i);

                if(nNode.getNodeType() == Node.ELEMENT_NODE){
                    Element element = (Element) nNode;
                    id = element.getElementsByTagName("Station").item(0).getTextContent();
                    if(element.getNodeName().equals("Station")){
                        //id = elemento.getElementsByTagName("Station").item(0).getTextContent();
                        station = new Track(id);
                        stations.add(station);
                        rail.add(stations);
                    }else if(element.getNodeName().equals("Track")){
                        //id = elemento.getElementsByTagName("Track").item(0).getTextContent();
                        trackOn = new Track(id);
                        tracks.add(trackOn);
                        rail.add(tracks);
                    }else if(element.getNodeName().equals("Light")){
                        //id = elemento.getElementsByTagName("Light").item(0).getTextContent();
                        Light luz = new Light(id);
                        lights.add(luz);
                        rail.add(lights);
                    }else if(element.getNodeName().equals("Switch")){
                        //id = elemento.getElementsByTagName("Switch").item(0).getTextContent();
                        Switch sw = new Switch(id,trackOn,null,null);
                        setSwitches.add(sw);
                        rail.add(setSwitches);
                    }

                }
            }
            doneLoading = true;

        }catch (Exception e){
            e.printStackTrace();
        }


        linkComponents();

    }

0 个答案:

没有答案