Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Not able parse Xml using SAX parser in java. While fetching the end Element getting EmptyStackException.

Dummy code ....

Dummy Java code below and also the xml is in below trail. package new.parser;

import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.Iterator; import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLFileUsingSaxparser extends DefaultHandler {

//private Account acct;
//private String temp;
//private ArrayList<Account> accList = new ArrayList<Account>();

private Stack<String> elementStack = new Stack<String>();
private Stack<Object> objectStack = new Stack<Object>();
private int recordLevel=1;
String name="";
String fileName ="Jaguar";

public static void main(String[] args) throws IOException, SAXException,
        ParserConfigurationException {

     SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            InputStream    xmlInput  =
                new FileInputStream("D:\\host\\240\\exa206.XML");

            SAXParser      saxParser = factory.newSAXParser();
            ReadXMLFileUsingSaxparser handler   = new ReadXMLFileUsingSaxparser();
            saxParser.parse(xmlInput, handler);


        } catch (Throwable err) {
            err.printStackTrace ();
        }
}

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    recordLevel++;

    name = localName;

    if(recordLevel ==2){
    this.elementStack.push(qName);

    int length = attributes.getLength();
    // process each attribute

    for (int i = 0; i < length; i++) {

        String name = attributes.getQName(i);

        String value = attributes.getValue(i);
        System.out.println("name IS :"+ name+"  Value:" + value);

    }
    }
    if(!name.equals(qName)){

          int length = attributes.getLength();
          // process each attribute


                    for (int i=0; i<length; i++) {

                          String name = attributes.getQName(i);

                          String value = attributes.getValue(i);
                          System.out.println("Value:" + value);

                    }
    }

}

public void endElement(String uri, String localName, String qName) throws SAXException {

     try{
if(!name.equals(qName)){

    System.out.println("uri : " +uri +"localName : " +localName +"qName : " + qName);
    this.elementStack.pop();
     }
     }
     catch(EmptyStackException e){
     e.printStackTrace();
     }
}

public void characters(char ch[], int start, int length) throws SAXException {

    String value = new String(ch, start, length).trim();
    if (value.length() == 0)
        return; // ignore white space

}

private String currentElement() {
    return this.elementStack.peek();
}

private String currentElementParent() {
    if (this.elementStack.size() < 2)
        return null;
    return this.elementStack.get(this.elementStack.size() - 2);
}

}

Below given is the sapmple xml:


   <?xml version="1.0" encoding="UTF-8" ?>
    <Record recordType="2"> 
    <Mnt_action>A</Mnt_action>
    <CUSTID>50001989</CUSTID>
    <COD_NO>123123123123</COD_NO>
    <COD_LINK_ACCT>50200000003859</COD_LINK_ACCT>      
    </Record>