1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.ElementFilter;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
javax.xml.parsers.DocumentBuilder builder =factory.newDocumentBuilder();
Document doc =(Document) parser.build("malab.xml");
// SAXBuilder parser = new SAXBuilder();
// Document doc = (Document) parser.build("malab.xml");
System.out.println("------------------------------------");
List<String> evtnames= new java.util.ArrayList<>();
Element root = doc.getRootElement();
String evt;
ElementFilter filter = new org.jdom2.filter.ElementFilter("evt");
for(Element c : root.getDescendants(filter)) {
evtnames.add( c.getAttributeValue("name") );}
for (Iterator it=evtnames.iterator();it.hasNext();){
System.out.println(it.next());
}
}} |