Hello.

I am building a XML reader class DOM.

Does anyone have a link that has some code where

the text from a JComboBox.selectedItem()("text")

for example or any text is used to find a Node in a the DOM document

and then work with that nodes siblings?

Hello.

I am building a XML reader class DOM.

Does anyone have a link that has some code where

the text from a JComboBox.selectedItem()("text")

for example or any text is used to find a Node in a the DOM document

and then work with that nodes siblings?

Hello.
How can I print out the values of all the tags below the <keyName key="cmaj">
I can only get the first one.

Is it possible to retrieve each of the name of the <element> of each of the elements below <keyName="cmaj"> ?

<?xml version="1.0" encoding="utf-8" ?>
<data>
  <keyName key="cmaj">
    <name>
      <![CDATA[C Major]]>
    </name>
    <sigtxt>
      <![CDATA[NO_#_NO_b]]>
    </sigtxt>
    <relatonetxt>
      <![CDATA[g#]]>
    </relatonetxt>
    ......25 more

[/code]
[code]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package keycardsxml;

/**
 *
 * @author depot

 */
import java.io.File;
import javax.swing.JFrame;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReaderProgressions extends KeyCards {

    private String name;
    public Document doc;

    public XMLReaderProgressions() {
    }

    public void readAll(String name) {
        this.name = name;
        File docFile = new File("keycards.xml");

        doc = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(docFile);
        } catch (java.io.IOException e) {
            System.out.println("Can't find the file");
        } catch (Exception e) {
            System.out.print("Problem parsing the file.");
        }

        Element root = doc.getDocumentElement();
        System.out.println("The root element is " + root.getNodeName() + ".\n");
        NodeList listOfKeys = root.getElementsByTagName("keyName");
        for (int x = 0; x < listOfKeys.getLength(); x++) {
            Node keyNode = listOfKeys.item(x);
            if (keyNode.getNodeType() == Node.ELEMENT_NODE) {
                Element keyElement = (Element) keyNode;
                Attr keyNameAttributes = keyElement.getAttributeNode("keyname");
                if (keyNameAttributes.getNodeValue().equals(name)) {
                    System.out.println("The param sent to class: " + name);
                    //this finds ->cmaj
                    System.out.println("The attr retrieved: " + keyNameAttributes);
                    //this finds the attr i need ->keyname="cmaj"

                    //I thought this list would contain all the elements below my
                    //keyElement I stopped on
                    NodeList list = listOfKeys.item(x).getChildNodes();

                    String keyNameSiblingValue = null;
                    for (int i = 0; i < list.getLength(); i++) {
                        String value = ((Node) list.item(i)).getNodeValue().trim();
                        if (value.equals("") || value.equals("/r")) {
                            continue;
                        } else {
                            keyNameSiblingValue = value;
                            //this only outputs the first element "sibling"
                            System.out.println("The keyNameSiblingValues are: " + keyNameSiblingValue);
                            // ->C Major

                        }
                    }
                }


                /*
                NodeList list = keyNameElement.getChildNodes();               
                String keyname = null;
                for (int i = 0; i < list.getLength(); i++) {
                String value = ((Node) list.item(i)).getNodeValue().trim();
                if (value.equals("") || value.equals("/r")) {
                continue;
                } else {
                keyname = value;
                System.out.println("The keynames are: " + keyname);

                }
                }
                 */
            }
        }




    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


output:
The root element is data.

The param sent to class: cmaj
The attr retrieved: keyname="cmaj"
The keyNameSiblingValues are: C Major
The root element is data.

The param sent to class: cmaj
The attr retrieved: keyname="cmaj"
The keyNameSiblingValues are: C Major

I don't understand why I can only get the first child below the element I am working with.

There are actually 51 in the XML doc.

Is there a way to retrieve these children?

//I thought this list would contain all the elements below my
                    //keyElement I stopped on
                    NodeList list = (NodeList) listOfKeys.item(x).getChildNodes();
                    Element elementFound = (Element) listOfKeys.item(x);
                    //51 elements below elementFound

                    if (elementFound.getNodeType() == Node.ELEMENT_NODE) {
                        NodeList children = (NodeList) elementFound.getChildNodes();
                        int childrenCount = children.getLength();
                        int charCount = list.item(x).getNodeValue().length();
                        for (int z = 0; z < childrenCount; z++) {
                            System.out.println("children: " + childrenCount);

                            /*
                            String keyNameSiblingValue = null;                            
                            for (int i = 0; i < charCount; i++) {
                                String value = ((Node) list.item(z)).getNodeValue().trim();
                                if (value.equals("") || value.equals("/r")) {
                                    continue;
                                } else {
                                    keyNameSiblingValue = value;
                                    //this only outputs the first element "sibling"
                                    System.out.println("The keyNameSiblingValues are: " + keyNameSiblingValue);
                                    // ->C Major

                                }
                            }
                             */

                        }
                    }
                }

output
run:
The root element is data.

The param sent to class: cmaj
The attr retrieved: keyname="cmaj"
children: 3
children: 3
children: 3
The root element is data.

The param sent to class: cmaj
The attr retrieved: keyname="cmaj"
children: 3
children: 3
children: 3

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.