我解释您的评论,您希望循环遍历列表并打印当前所选实体的每个标题链接对,其中标题包含单词car(或在您的代码中aBuscar)。如果这是正确的,这是实现此行为的代码:
存储单个标题-链接对的类:
public class TitleLink {
private String title;
private String link;
public TitleLink() {
this.title = "";
this.link = "";
}
public TitleLink(String title) {
this.title = title;
this.link = "";
}
public TitleLink(String title, String link) {
this.title = title;
this.link = link;
}
public boolean titleContains(String title) {
return this.title.contains(title);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@Override
public String toString() {
return "TitleLink {title=" + title + ", link=" + link + "}";
}
}
测试类:
public class Test {
private static String aBuscar = "car";
public static void main(String[] args) {
List<TitleLink> example = new LinkedList<>();
example.add(new TitleLink("carTitle", "firstCarLink"));
example.add(new TitleLink("carrotTitle", "firstCarrotLink"));
example.add(new TitleLink("bikeTitle", "bikeCarLink"));
example.add(new TitleLink("boatTitle", "boatCarLink"));
example.add(new TitleLink("carTitle", "secondCarLink"));
example.add(new TitleLink("carrotTitle", "secondCarrotLink"));
example.add(new TitleLink("bikeTitle", "secondCarLink"));
example.add(new TitleLink("boatTitle", "secondCarLink"));
setDataBuscar(example);
}
private static void setDataBuscar(List<TitleLink> data) {
System.out.println("------");
System.out.println(data); // -> probe ... show whole content of the map
System.out.println("------");
for (TitleLink titleLink : data) {
if (titleLink.titleContains(aBuscar)) {
System.out.println(titleLink);
}
}
}
/*
* This version stores the current title and link in separate variable inside the loop
* and prints them if the condition is met.
private static void setDataBuscar(List<TitleLink> data) {
System.out.println("------");
System.out.println(data); // -> probe ... show whole content of the map
System.out.println("------");
for (TitleLink titleLink : data) {
String title = titleLink.getTitle();
String link = titleLink.getLink();
if (titleLink.titleContains(aBuscar)) {
System.out.println(title);
System.out.println(link);
}
}
}*/
}
输出:
------
// all entries
------
TitleLink {title=carTitle, link=firstCarLink}
TitleLink {title=carrotTitle, link=firstCarrotLink}
TitleLink {title=carTitle, link=secondCarLink}
TitleLink {title=carrotTitle, link=secondCarrotLink}
更改解析器类:
public class Parser {
private URL url;
static TitleLink entry2;
public Parser(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public List<TitleLink> parse() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
List<TitleLink> entries = new LinkedList<>();
TitleLink entry;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.url.openConnection().getInputStream());
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");
for (int i=0;i<items.getLength();i++){
entry = new TitleLink();
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j=0;j<properties.getLength();j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("title")){
entry.setTitle(property.getFirstChild().getNodeValue());
} else if (name.equalsIgnoreCase("link")){
entry.setTitle(property.getFirstChild().getNodeValue());
entries.add(entry);
entry = new TitleLink();
}
}
//entries.add(entry);
entry2=entry;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return entries;
}
}
我无法测试解析器类的变化,因为我没有包含该类应该解析的数据的网页。如果您测试它并发现任何错误,您可以尝试修复它们(不应该太难)或发表评论。