Java Sax parser, storing properties

I'm trying to store the current document position on the stack, push the startelement, and pop it up on the endelement Now I'm 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 it tries to read ATTS later, it gives an error: cause: Java Lang. IllegalStateException: attribute can only be used within the scope of startelement()

Is there a fast and reliable way to store properties? In addition, is there a better way than to build a new custom object stackelement for each start tag?

Solution

When you push attributes to the custom object stack, you will get the actual attributes object. According to the document:

ATTS – attributes attached to the element If there is no attribute, it should be an empty attributes object The value of this object is undefined after returned by startelement (emphasize my)

You should capture the attributes in map < string, string > in the startelement (...) method So you can use them anywhere

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>