Java SAX Parser persisting attributes

I am trying to save the current position of the document on the stack by clicking on startElement, and exit on endElement. Now I am using:

public void startElement(String namespaceURI, String elname,
                         String qName, Attributes atts) throws SAXException {
    original.append(innerText);
    original.append("<");
    original.append(elname);
    original.append(">");
    docStack.push(new StackElement(elname,atts));
....

Unfortunately, when he tries to read atts later, he gives an error: Called: java.lang.IllegalStateException: Attributes can only be used within the scope of startElement ().

Is there a quick and reliable way to store attributes? Also, is there a better way to do this than to create a new custom StackElement object for each start tag?

+3
source share
2 answers

When you click Attributes on your stack of custom objects, you take the actual Attributes object, which, according to the documentation , says the following:

atts - , . , Attributes. startElement - undefined ( )

startElement (...) Map < String, String > . , .

+4

Attributes -, , StackElement ( ).

- :

public class StackElement {

    private Map<String, String> map = new HashMap<String, String>();

    public StackElement(String elname, Attributes atts) {

        for (int i = 0; i <  atts.getLength(); i++) {
            map.put(atts.getQName(i),atts.getValue(i));
        }
    }
}

p.s. , @nicholas , , , , .

+3

All Articles