Hello. I am trying to get children to printout for my elements.
Could someone tell me how my class should approach this procedure?
My class is suposed to find a certain element name, then from that find the element with the specific attribute and from that element with the specific attribute list the children. Is that possible?
Thanks
Code:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<key>
<keyName keyname="cmaj">
<![CDATA[C Major]]>
</keyName>
<sigtxt>
<![CDATA[NO_#_NO_b]]>
</sigtxt>
<relatonetxt>
<![CDATA[g#]]>
</relatonetxt>
<paratonetxt>
<![CDATA[Bn]]>
</paratonetxt>
<relasigtxt>
<![CDATA[NO_#_NO_b]]>
</relasigtxt>
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keycards;
/**
*
* @author depot
*/
import keycards.KeyCards;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
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;
private String name;
public Document doc;
public Element e;
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.name = name;
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = (Document) builder.build(file);
} catch (JDOMException ex) {
ex.printStackTrace();
}
listElements(doc.getRootElement());
}
public void listElements(Element e) {
List c = e.getChildren();
for (Iterator i = c.iterator(); i.hasNext();) {
Element n = (Element) i.next();
//System.out.println(n.getName());
if(n.getName().equals("keyName")){
List attr=n.getAttributes();
for(Iterator it =attr.iterator();it.hasNext();){
Attribute a = (Attribute) it.next();
if(a.getValue().equals(name)){
System.out.println("element: "+n.getName());
List ec=n.getChildren("keyName");
int s=ec.size();
System.out.println("there is: "+s+" in the ec list"+
" childElements:");
Iterator x=ec.iterator();
while(x.hasNext()){
Element keyName=(Element)i.next();
System.out.println("\t "+ keyName.getChildren());
}
//listChildElements(n);
}
}
}
listElements(n);
}
}//listElements()
public void listChildElements(Element x) {
System.out.println("In ListChildren");
List c = x.getChildren();
childCount++;
childCount = c.size();
System.out.println("Found something: " + childCount);
for (Iterator i = c.iterator(); i.hasNext();) {
Element n = (Element) i.next();
listChildElements(n);
}
}//end listChildElements()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}