I have this data in the xml file:
<manufacturer>Audi</manufacturer>
<model>Q3</model>
<production-year>2011</production-year>
<horsepower>150</horsepower>
<consumption type="fuel">7.6</consumption>
<price>36430</price>
And I wrote the following code that lets me read all the nodes from the file.
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new File(fileName));
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
Element car = (Element) node;
String id = car.getAttribute("id");
String manufacturer = car.getElementsByTagName("manufacturer").item(0).getTextContent();
String model = car.getElementsByTagName("model").item(0).getTextContent();
String production_year = car.getElementsByTagName("production-year").item(0).getTextContent();
String hp = car.getElementsByTagName("horsepower").item(0).getTextContent();
**String type = "";**
String fuelConsm = car.getElementsByTagName("consumption").item(0).getTextContent();
String price = car.getElementsByTagName("price").item(0).getTextContent();
My question is how do I read the value of the attribute type="" in the xml file and get it as a String?
Thanks in advance.