Ryan
Ryan

Reputation: 1221

element.getAttribute() returns empty String

I'm trying to access all of the elements in an XML file. Here is a portion of the file (the rest is just more chord elements)

<?xml version="1.0" encoding="UTF-8"?>
<Chord-List>
<Chord name = "I">
<Note1>C</Note1>
<Note2>E</Note2>
<Note3>G</Note3>
</Chord>

<Chord name = "ii">
<Note1>D</Note1>
<Note2>F</Note2>
<Note3>A</Note3>
</Chord>
</Chord-List>

Here's my java code. It will print out everything except the attribute values, so I think that getAttribute is returning an empty string, but I'm not sure why

public static void main(String [] args){
    try{
        File fXmlFile = new File("/home/Ryan/workspace/ChoraleGenerator/Chords.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        System.out.println("Root element: " + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("Chord");
        System.out.println("----------------------------");

        for(int i = 0; i < nList.getLength(); i++){
            Node nNode = nList.item(i);
            System.out.println("\nCurrent Element:" + nNode.getNodeName());
            if(nNode.getNodeType() == Node.ELEMENT_NODE){
                Element eElement = (Element)nNode;

                System.out.println("Chord name: " + eElement.getAttribute("name"));
                System.out.println("Note 1: " + eElement.getAttribute("Note1"));
                System.out.println("Note 2: " + eElement.getAttribute("Note2"));
                System.out.println("Note 3: " + eElement.getAttribute("Note3"));

            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

Finally, here is my output: Root element: Chord-List


Current Element:Chord

Chord name: I

Note 1:

Note 2:

Note 3:

Current Element:Chord

Chord name: ii

Note 1:

Note 2:

Note 3:

Any suggestions? Thanks in advance!

Upvotes: 1

Views: 2879

Answers (3)

JLRishe
JLRishe

Reputation: 101652

Of course this doesn't work. Note1, Note2, and Note3 aren't attributes. They're elements.

How about this:

// Define a helper method
private static String getChildElementContent(Element e, String childName) {
    NodeList children = e.getElementsByTagName(childName);
    if(children.getLength() > 0) {
        return children.item(0).getTextContent();
    }
    return "";
}

Then do:

System.out.println("Note 1: " + getChildElementContent(eElement, "Note1"));
System.out.println("Note 2: " + getChildElementContent(eElement, "Note2"));
System.out.println("Note 3: " + getChildElementContent(eElement, "Note3"));

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272207

eElement.getAttribute("Note1")

is wrong, since Note1 isn't an attribute but rather an element below the <Chord> element. You will want to get this element, and then the text node within that element.

Upvotes: 0

Mena
Mena

Reputation: 48404

You're using the getAttribute method with nodes that have no attributes.

What you probably want to use is the getTextContent() method instead.

Here's an example of attributes vs text:

<nodeName attribute0="value0" attribute1="value1">textContent</nodeName>

Upvotes: 1

Related Questions