Could someone tell me what has to be changed to get the element name and text of my targetElement?
I just get
internal frame openFrameCount= 1
start obj() type= guide
cmaj
name: null value: null
name: null value: null
name: null value: null
cmaj
name: null value: null
name: null value: null
name: null value: null
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keycards;
/**
*
* @author depot
*/
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLReaderProgressions extends KeyCards {
public Progression p;
public String name;
public Document doc;
public DocumentBuilder db;
public String[] nameArray;
public String[] valueArray;
public XMLReaderProgressions() {
}
public void readAll(String name) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
try {
doc = db.parse("keycards.xml");
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
parseDocument(name);
}
public void parseDocument(String name) {
System.out.println(name);
Element root = doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("keyName");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String attr = e.getAttribute("keyname");
if (attr.equals(name)) {
Element targetElement = e;
NodeList child = (NodeList) targetElement.getChildNodes();
nameArray = new String[child.getLength()];
valueArray = new String[child.getLength()];
for (int j = 0; j < child.getLength(); j++) {
Object o = child.item(i);
if (o instanceof Element) {
Element targetChild = (Element) o;
valueArray[j] = targetChild.getNodeName();
nameArray[j] = targetChild.getTextContent();
}
}
}
}
for (int j = 0; j < nameArray.length; j++) {
System.out.println("name: " + valueArray[j] + " value: " + valueArray[j]);
}
}
}
}