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]);
            }
        }
    }
}

I validated my xml with a DTD. Will that make it easier to solve my little problem finding data?

Well I got the siblings I need but need a good method to produce the text node. Is there a standard method for this?

import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import application1.Main;
import org.w3c.dom.Node;
public class XMLReaderProgressions extends KeyCards {

    public Progression p;
    public String name;
    public Document doc;
    public DocumentBuilder db;
    public String[] nameArray;
    public String[] valueArray;
    public String xmlName="keycards.xml";
    public Element t;
    public Element r;


    public XMLReaderProgressions() {
    }

    public void readAll(String name) {
        Main m =new Main();
        Document doc= m.readXML(xmlName);
        parseDocument(name,doc);

    }

    public void parseDocument(String name,Document doc) {
        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("key");
                if (attr.equals(name)) {
                    
                    Element r =doc.getElementById(name);
                    System.out.println("desired element: "+r.getNodeName()+" "+attr);
                    t=(Element)r.getNextSibling();
                    
                    //System.out.println("first sibling : "+t.getNodeName());
                    while(t!=null){
                        String pString=getTextValue(t).trim();
                         t=(Element)t.getNextSibling();
                         if(t!=null){
                             

                        System.out.println(" "+t.getNodeName());
                         System.out.println(" "+pString);
                         System.out.println(" "+t.getNodeName());
                         }
                    }
                    
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.