I want to parse any XML file into a list of objects of "XMLNode" class using "SAXParser"
XMLNode Class
public class XMLNode {
private String nodeName;
private String nodeValue;
private List<XMLNodeAttribute> attributes;
private boolean isParentNode;
private List<XMLNode> childNodes;
//.... getters and setters ....
}
XMLNodeAttribute Class
public class XMLNodeAttribute {
private String name;
private String value;
//.... getters and setters ....
}
Please help me in writing the parser class which can take input as a XML file and output the List.
Thank you in advance.
I am able to write some code..
public class XmlProcesser extends DefaultHandler {
XMLResponse xmlResponse = null;
List<XMLNode> resplist = new ArrayList();
List<XMLNode> temp = new ArrayList();
boolean endtag = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String elementName = localName;
if ("".equals(elementName)) {
elementName = qName;
}
System.out.println(" Start Ele - " + elementName );
//Each attribute
if (elementName!=null) {
if (attributes!=null) {
for (int pos=0; pos<attributes.getLength(); pos++) {
String name = attributes.getLocalName(pos)==null || attributes.getLocalName(pos).trim().length()==0 ? attributes.getQName(pos) : attributes.getLocalName(pos);
String value= attributes.getValue(pos);
System.out.println(" name - " + name + " value - " + value );
}
}
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
String s = new String(ch, start, length);
System.out.println(" value - " + s );
endtag = false;
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
endtag = true;
String elementName = localName;
if ("".equals(elementName)) {
elementName = qName;
}
System.out.println(" End Ele - " + elementName );
}
}