且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将XML标签存储为Java中的数组

更新时间:2023-01-12 10:06:34

公共类CustomHandler扩展了DefaultHandler {

public class CustomHandler extends DefaultHandler {

private ArrayList<String> questionList;
private boolean questionBuffering;
private StringBuilder sb;



@Override
public void startDocument() throws SAXException {
questionList = new ArrayList<String>();
} 

@Override
public void endDocument() throws SAXException {
} 

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("question")) {
        questionBuffering = true;
    }

}


@Override
public void characters(char ch[], int start, int length) {
    if(questionBuffering) {
        sb.append(ch, start, length);
    }

} 

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    if (localName.equals("question")) {
        questionBuffering = false;
    questionList.add(sb.toString());
    }
}

public ArrayList<String> getResult() {
    return questionList;
    };
}

}