Parsing code called twice

Somehow my code is being called twice with the result that everything has the same name twice (multiple names are legal according to the DTD).

My parsing code looks like this:

public Channel getChannel(Node channelnode)

{

Channel channel;

NodeList channelchildren;

Element channelelement;

channelelement = (Element)channelnode;

if(channelelement.hasAttribute("id"))

{

channel = new Channel(channelelement.getAttribute("id"));

channelchildren = channelnode.getChildNodes();

for(int i = 0 ; i < channelchildren.getLength() ; i++)

{

if(channelchildren.item(i).getNodeType() == Node.ELEMENT_NODE)

{

if(channelchildren.item(i).getNodeName().equals(("display-name")))

{

channel.addDisplayname(getDisplayname(channelchildren.item(i)));

}

if(channelchildren.item(i).getNodeName().equals(("icon")))

{

}

if(channelchildren.item(i).getNodeName().equals(("url")))

{

}

}

}

}

else

{

channel = null;

}

return channel;

}

public String getDisplayname(Node displaynamenode)

{

Element displaynameelement;

Node displayname;

displaynameelement = (Element)displaynamenode;

displayname = displaynamenode.getFirstChild();

if(displaynameelement.hasAttribute("lang"))

{

return displayname.getNodeValue();

}

else

{

return displayname.getNodeValue();

}

}

The code above is used to parse part of this tag in my XML file:

<channel id="001.tv.tv2.dk">

<display-name>TV 2</display-name>

<icon src="http://tv.tv2.dk/images/logo/1.gif" />

</channel>

The problem is that the addDisplayname in the Channel class is somehow called twice even though there is only one <display-name> tag.

Any help figuring out what is happening will be greatly appreciated.

[2024 byte] By [grovea] at [2007-11-26 15:43:28]
# 1

1. Since the loop deals with each node in the "channelchildren" list only once, the cause of the addDisplayname() method being called twice can only be the consequence of the getChannel() method itself being called twice, provided the node passed to the getChannel() method really contains only one <display-name> element.

2. The getDisplayname() method always returns displayname.getNodeValue() (the value of the <display-name> element in this context) so the if-then-else statement is unnecessary.

prgguya at 2007-7-8 22:02:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

I know about the second point you are making. The if-then-else statement is actually the start of further development since the <display-name> element can have a lang attribute and I have to deal with it, but for now I ignore it since it is extremely rarely used and other things are more important.

I had thought about the case that you mention with multiple calls of getChannel() but my debug output suggests that it is not the case. The method getChannel() returns a Channel object, which has a toString() method, and that object is given as argument to System.out.println() and never saved anywhere.

The toString() method of the Channel object looks like this:

public String toString()

{

String tmp = new String();

tmp += "display-name: " + displaynames.get(0);

for(String name : displaynames)

{

tmp += ", " + name;

}

return tmp;

}

With an output that look like this for the XML example given in my original post: display-name: TV 2, TV 2

With this output I don't see how multiple calls of getChannel() can be the cause, since multiple calls should give multiple Channel objects and multiple lines of output.

grovea at 2007-7-8 22:02:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

The displaynames.get(0) element is assigned twice to the "tmp" variable, hence the "TV 2, TV 2" content of the string. If it is the double occurrence of "TV 2" in the return value of the toString() method that you say is your problem, I suggest rewriting the toString() method as follows:public String toString() {

String tmp = new String();

tmp += "display-name: " + displaynames.get(0);

for (int i = 1; i < displaynames.size(); i++) {

tmp += ", " + displaynames.get(i);

}

return tmp;

}

prgguya at 2007-7-8 22:02:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
You would think that 8?years of programming Java would teach you not to make mistakes like that one, but I did. As you might figure out from the statement above, your suggestion did the trick for me, thanks for the help.
grovea at 2007-7-8 22:02:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...