【问题标题】:Loop Through List and Find if String Exists遍历列表并查找字符串是否存在
【发布时间】:2020-03-12 03:03:08
【问题描述】:

我有一个要循环遍历的列表。本质上,我有一个 XML 文件,上面有一些航班。我要做的是查看用户搜索的内容是否在 XML 文件中,如果是,则在 JSP 页面上将其显示回给他们。发生的情况是,只有我的 XML 中的第一个元素存储在变量中。我需要能够查看整个 XML 文件并查看用户输入是否存在。例如,根据我目前的情况,如果我使用if (startLocation.equals(flight.getStartLocation()) && endLocation.equals(flight.getEndLocation())),它将只返回加利福尼亚州洛杉矶的 startLocation 和新罕布什尔州曼彻斯特的 endLocation。如果用户切换选择框中的内容,则不起作用;正如我所说,它只是收集第一个飞行元素。

FlightController.Java

@RequestMapping(value = "/submit-search", method = RequestMethod.POST)
    public String submitSearch(@RequestParam("name") String name, @RequestParam("startLocation") String startLocation, @RequestParam("endLocation") String endLocation,
            @RequestParam("date") String date, @RequestParam("passengers") Integer passengers, Model model) throws JAXBException, IOException {


        generateFlights();


        File file = ResourceUtils.getFile("classpath:flights.xml");

        try {
            JAXBContext context = JAXBContext.newInstance(FlightList.class);
            Unmarshaller un = context.createUnmarshaller();
            FlightList flightList = (FlightList) un.unmarshal(file);
            List < Flight > list = flightList.getFlights();
            for (Flight flight: list) {

                if (startLocation.equals(flight.getStartLocation()) && endLocation.equals(flight.getEndLocation()))
                {
                    model.addAttribute("startLocation", flight.getStartLocation());
                    model.addAttribute("endLocation", flight.getEndLocation());
                    model.addAttribute("date", flight.getDate());
                    return "ticketInfo";
                }
                else {
                    // Does not exist
                    return "invalidTicket";
                }


            }



        } catch (JAXBException e) {
            model.addAttribute("error", e.toString());
            return "error";
        }

        return null;
}

Flight.Java

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="flight")
@XmlType(propOrder = { "startLocation", "endLocation", "date" })
public class Flight {
    // Create variables

    String startLocation;
    String endLocation;
    String date;


    // Getters and setters for Flight class

    @XmlElement
    public String getStartLocation() {
        return startLocation;
    }
    public void setStartLocation(String startLocation) {
        this.startLocation = startLocation;
    }
    @XmlElement
    public String getEndLocation() {
        return endLocation;
    }
    public void setEndLocation(String endLocation) {
        this.endLocation = endLocation;
    }
    @XmlElement
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }


}

FlightList.java

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightList {

    // XmlElement sets the name of the entities
    @XmlElement(name = "flight")
    private List<Flight> flights;


    public void setFlights(List<Flight> flights) {
        this.flights = flights;
    }

    public List<Flight> getFlights() {
        return flights;
    }
}

航班.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<flightList>
    <flight>
        <startLocation>Los Angeles, CA</startLocation>
        <endLocation>Manchester, NH</endLocation>
        <date>03-22-2020</date>
    </flight>
    <flight>
        <startLocation>Boston, MA</startLocation>
        <endLocation>Albany, NY</endLocation>
        <date>03-20-2020</date>
    </flight>
</flightList>

【问题讨论】:

  • startLocation.equals(flight.getStartLocation()) &amp;&amp; endLocation.equals(flight.getEndLocation()) 这属于 Flight 类的域,应该在这个类中(可能是 equals 的实现,或者只是 isSameConnection(Flight other) 或其他东西。

标签: java xml spring loops


【解决方案1】:

我试过了,它对我来说很好 - 我的意思是解析。

你的意思

发生的情况是,只有我的 XML 中的第一个元素存储在变量中。

这是我认为可以按预期工作的代码:https://github.com/Betlista/SpringTests/tree/master/FlightsXmlParsing

...就像我在控制台中所做的那样:

b.s.f.SpringBootConsoleApplication       : Los Angeles, CA -> Manchester, NH [03-22-2020]
b.s.f.SpringBootConsoleApplication       : Boston, MA -> Albany, NY [03-20-2020]

【讨论】:

  • 当我执行 system.out.println 时,它会打印我在我的 XML 中拥有的每个单独的航班,但是当我试图通过输入框搜索它时,它只会找到第一个航班,即使用户在列表中选择了第二个航班。
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多