I have to develop one xml parsing app using sax parser.
I have follows below xml feed:
<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>
Here i have to create one class for getter and setter:
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;
}
My xml handler look like below code:
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);
}
Here am getting below error on these line:
laptop.setModel(attributes.getValue("title"));
The method setModel(List) in the type Laptop is not applicable for the arguments (String)
How can i resolve that error.please give me solution for these...
The error says yon can not assing a String
instance to a List
instance. And it is quite reasonable. Since
public void setModel(List<String> string) {
this.model = string;
}
takes a List as parameter.
As possible fix you can try in this way, if you want to keep a List
of String
(article) for every Laptop instance
public void setModel(String string) {
if (this.model == null)
this.model = new List<String>();
this.model.add(string);
}