saxパーサーを使用して1つのxml解析アプリを開発する必要があります。
私は以下のxmlフィードを持っています:
<root>
<Categories>
<Category name="Photos">
<article articleid="4537" title="Sonam-Steals-the-Mai-Show"/>
<article articleid="4560" title="Big-B-Croons-For-World-Peace"/>
<article articleid="4585" title="Yami-Paints-The-Town-Red"/>
</Category>
<Category name="Style">
<article articleid="266" title="Dita wows us in a sari"/>
<article articleid="268" title="Frieda is a natural"/>
<article articleid="269" title="Demi finds love in Jodhpur"/>
</Category>
</Categories>
</root>
ここでは、getterとsetter用に1つのクラスを作成する必要があります。
public class Laptop {
private String brand;
private List<String> model;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public List<String> getModel() {
return model;
}
public void setModel(List<String> string) {
this.model = string;
}
私のxmlハンドラーは次のコードのようになります:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
current = true;
if (localName.equals("Category")) {
laptop = new Laptop();
laptop.setBrand(attributes.getValue("name"));
}
else if (localName.equals("article")) {
laptop.setModel(attributes.getValue("title"));
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(current)
{
currentValue = new String(ch, start, length);
current=false;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
current = false;
if (localName.equals("Category")) {
// add it to the list
laptops.add(laptop);
}
if (localName.equals("article")) {
laptop.getModel().add(currentValue);
}
これらの行で以下のエラーが発生しています:
laptop.setModel(attributes.getValue("title"));
タイプLaptopのメソッドsetModel(List)は、引数(String)には適用できません。
どうすればそのエラーを解決できますか。これらの解決策を教えてください...