get elements and attributes
Hello everyone,
i have this xml file:
<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item name="New" id="1">
<tab name="Main" id="1">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
<tab name="Images" id="2">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
<tab name="Attachments" id="3">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
</item>
<item name="Video" id="2">
<tab name="Main" id="1">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
<tab name="Images" id="2">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
<tab name="Attachments" id="3">
<input type="text" name="textfield" value="" size="10" maxlength="20" description="" />
<input type="hidden" name="hiddenfield" value="" />
<input type="textarea" name="textarea" value="" cols="100" rows="100" />
<input type="checkbox" name="checkbox" value="checkbox" />
<input type="radio" name="radiobutton" value="radiobutton" />
<input type="select" name="select" value="" />
<input type="file" name="file" value="" />
</tab>
</item>
</items>
What i want to do is to get the attributes for each "item" node adding them to a Map and then to a List.
This is what i got so far:
package com.gacms.operations;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
publicclass XMLConfigurationReader{
private DocumentBuilderFactory dbf =null;
private Document doc =null;
private Map<String, String> attributesMap =null;
private List<Map> elementsList =null;
public XMLConfigurationReader(String xmlFile){
dbf = DocumentBuilderFactory.newInstance();
try{
doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
}catch (SAXException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (ParserConfigurationException e){
e.printStackTrace();
}
}
publicvoid getNodes(String elementName){
NodeList elements = doc.getElementsByTagName(elementName);
for(int i = 0; i < elements.getLength(); i++){
Node element = elements.item(i);
listAttributes(element);
}
}
publicvoid listAttributes(Node element){
attributesMap =new HashMap<String, String>();
NamedNodeMap attributes = element.getAttributes();
for(int i = 0; i < attributes.getLength(); i++){
Node attribute = attributes.item(i);
attributesMap.put(attribute.getNodeName(), attribute.getNodeValue());
}
elementsList.add(attributesMap);// Here is where the code fails
}
public List<Map> getElementsList(){
return this.elementsList;
}
}
i am getting this exception:
java.lang.NullPointerException
at com.gacms.operations.XMLConfigurationReader.listAttributes(XMLConfigurationReader.java:54)
at com.gacms.operations.XMLConfigurationReader.getNodes(XMLConfigurationReader.java:42)
at com.gacms.controller.MainController.service(MainController.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)
Can someone tell me what am i doing wrong?
Thanks in advance.

