What do I need to learn to stop on a certain element which this class can do. But I would like to work with the children of the element is finds?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keycards;
/**
*
* @author depot
*/
import javax.xml.parsers.ParserConfigurationException;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.xml.sax.SAXException;
public class XMLReaderProgressions extends KeyCards {
public Progression p;
public String attrName;
public String attrValue;
public String name;
public String value;
public Document doc;
public Element e;
public Element element;
public int count = 0;
public int childCount = 0;
//experimenting with jdom when the combox is clicked
public XMLReaderProgressions() {
}
public void readAll(String name) throws ParserConfigurationException,
SAXException, IOException {
this.attrName = name;
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = (Document) builder.build(file);
process(doc.getRootElement());
} catch (JDOMException ex) {
System.out.println(file + " is not well-formed.");
ex.printStackTrace();
} catch (IOException ioe) {
System.out.println(ioe);
}
}//end readAll()
//recursively descend the tree
public void process(Element element) {
inspect(element);
List content = element.getContent();
Iterator iterator = content.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Element) {
Element child = (Element) o;
process(child);
}
}
}
//Print the properties of each element
public void inspect(Element element) {
if (!element.isRootElement()) {
//Print a blank line to separate it from the previous
//element
System.out.println();
}
List attributes = element.getAttributes();
if (!attributes.isEmpty()) {
Iterator iterator = attributes.iterator();
while (iterator.hasNext()) {
Attribute attribute = (Attribute) iterator.next();
String name = attribute.getName();
String value = attribute.getValue();
////////////////////////////////////////////////////////////////
//trying to list the children of this element
/////////////////////////////////////////////////////////////
if (value.equals(attrName)) {
//found the <keyName keyname=?name> ?name is the combobox selection
processSelectedElement(element);
}
}
}
}
public void processSelectedElement(Element element) {
inspectSelectedElement(element);
String qualifiedName = element.getQualifiedName();
System.out.println(qualifiedName + ":");
printText(element);
System.out.println(qualifiedName + ":");
List selectedElementContent = element.getChildren();
Iterator iterator = selectedElementContent.iterator();
while (iterator.hasNext()) {
Object s = iterator.next();
if (s instanceof Element) {
Element child = (Element) s;
processSelectedElement(child);
}
}
}
//Print the properties of each element
private void inspectSelectedElement(Element element) {
if (!element.isRootElement()) {
//Print a blank line to separate it from the previous
//element
System.out.println("inspecting so I can list elements below");
}
}
public void printText(Element e) {
System.out.println("in printText " + e.getTextTrim());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}