W3C home > Mailing lists > Public > www-dom@w3.org > April to June 2001

RE: guidance needed

From: Tom Leuntjens <tom.leuntjens@bricsnet.com>
Date: Thu, 31 May 2001 10:59:00 +0200
To: <www-dom@w3.org>
Message-ID: <CCEMJKFFPGIIBIDNEKHICENICDAA.tom.leuntjens@bricsnet.com>
thanx for help so far , I see what you mean and thats what I need.
The example didn't work like coded. But I can't seem to get it to work if i
fool around with it as well :)

My XML looks like this


   <companyname>Sam test</companyname>

   <companyname>Tom test</companyname>


and code

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("");
NodeList companylist = doc.getElementsByTagName("company");

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

		// so here i need to take the company name  item(0) (if it exist, had some
problems catching a nullpointer execpetion as well)

		    Node company = NodeList.item(i);
		    // also tried  Node company = companylist.item(i) ;
		    // thought that you need the company node i to be able to get the
companyname or something
    		    Node name = company.getElementsByTagName("companyname").item(0);
					// there is always only one company name ==> item(0) ??!!


exception : Method getElementsByTagName(java.lang.String) not found in
interface org.w3c.dom.Node

So what is going wrong here ?
I know I need to do it like this

If I'll use the following code in my loop I know I'll be in trouble

NodeList companyname = doc.getElementsByTagName("companyname");
String CompanyName =
out.print(CompanyName + "<br>");

Regards and thanx for any help you might be able to give,


-----Original Message-----
From: www-dom-request@w3.org [mailto:www-dom-request@w3.org]On Behalf Of
James Melton
Sent: Tuesday, May 29, 2001 9:04 PM
To: Joseph Kesselman
Cc: Barbara Samson; Tom Leuntjens; www-dom@w3.org
Subject: Re: guidance needed

Joe: I agree with your assessment, and understand your point of view.
The problem is that Tom didn't really get an answer, which may by itself
be reason for him to start looking elsewhere.

Tom: Your code looks pretty good. The outermost loop though should be
looking for the "company" nodelist:
  NodeList name = doc.getElementsByTagName("company");

Then you get each of the elements inside that you want:
  for (i=0; i<name.length(); i++)
    Node company = NodeList.item(i);
    Node name = company.getElementsByTagName("name").item(1);
    Node phone = company.getElementsByTagName("phone").item(1);

    out.print(name.getFirstChild().getNodeValue() + "<br>");
    out.print(phone.getFirstChild().getNodeValue() + "<br>");

Disclaimer: This is very off the cuff code meant only to show that you
iterate over "company" elements rather than "name" elements. It may not
work exactly as coded.

Received on Thursday, 31 May 2001 05:00:05 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:08 UTC