• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sax Parsing startElement() method automatically create different object to avoid overwrite variable

 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant understand the problem regarding when following startElement method is called then it's start to read from< laptop> tag and crate object Laptop laptop1=new Laptop() and set the value into endElement method to Bean class. but what hapeen in Line 1 I marked at below means second <Lenovo> tag it's reached to read. the Another Different object must be crated i.e Laptop laptop2=new Laptop() to avoid over wright variable value. thanks in advance.


Laptop.xml

<?xml version="1.0" encoding="UTF-8"?>
<laptops>
<laptop model = "Dell Inspiron i13z-3181PNK">
<brand>Dell</brand>
<price>$699.99</price>
<description>Switch your lid to match your mood.
A 13.3" laptop with 3rd Gen Intel Core processor power and optional SWITCH lids
</description>
<technical-details>
Intel 2nd gen Core i3-2367 1.40GHz 1.40 GHz (6MB Cache)
6 GB DIMM
500GB 5400 rpm SATA Hard Drive
13-Inch Screen
Windows 7 Home Premium 64-bit
</technical-details>
<image-url>http://10.0.2.2:8080/laptop-images/Dell-3181PNK.jpg</image-url>
</laptop>
<laptop model = "Dell XPS XPS13-9001sLV"> --- Line1>>>>>
<brand>Dell</brand>
<price>$1,399.99</price>
<description>Strikingly thin, with more room to view.
</description>
<technical-details>
Intel Core i7-2637M (1.70GHz, 4MB Cache)
4 GB DIMM
256GB Solid State Drive
13-Inch Screen
Windows 7 Home Premium 64-bit
</technical-details>
<image-url>http://10.0.2.2:8080/laptop-images/Dell-XPS13.jpg</image-url>
</laptop>
</laptops>

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal = "";
if (qName.equalsIgnoreCase("laptop")) {
// create a new instance of Laptop
Laptop laptop1 = new Laptop();
laptop1.setModel(attributes.getValue("model"));
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {

if (qName.equalsIgnoreCase("laptop")) {
// add it to the list
laptops1.add(laptop);
} else if (qName.equalsIgnoreCase("brand")) {
laptop1.setBrand(tempVal);
} else if (qName.equalsIgnoreCase("description")) {
laptop1.setDescription(tempVal);
} else if (qName.equalsIgnoreCase("technical-details")) {
laptop1.setTechDetails(tempVal);
} else if (qName.equalsIgnoreCase("image-url")) {
laptop1.setImageURL(tempVal);
} else if (qName.equalsIgnoreCase("price")) {
laptop1.setPrice(tempVal);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic