使用下面的代码,我想为节点内的每个数据从 SAX 解析器返回一个数据数组。我有一个数组列表,它将保存每个叶节点下的数据。如何在处理程序之外使用这些数据或返回它?-
public class ReadXMLFile {
public static xmldataparser(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean burl = false;
boolean bip = false;
List<String> a = new ArrayList<String>();
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("URL")) {
burl = true;
}
if (qName.equalsIgnoreCase("IP")) {
bip = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (burl) {
System.out.println("URL : " + new String(ch, start, length));
a.add(new String(ch, start, length));
burl = false;
}
if (bip) {
System.out.println("IP : " + new String(ch, start, length));
bip = false;
a.add(new String(ch, start, length));
}
}
};
}
return <the array from the handler(this is where I need help)>
}
}