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>
Jim,
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

<companies>

 <company>
   <companyname>Sam test</companyname>
   <email>sam@bricsnet.Be</email>
 </company>

 <company>
   <companyname>Tom test</companyname>
   <email>tom@bricsnet.Be</email>
 </company>

</companies>

and code

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("http://127.0.0.1:81/company.xml");
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 =
companyname.item(i).getFirstChild().getNodeValue().toString();
out.print(CompanyName + "<br>");



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

Tom







-----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);
    //etc...

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

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.

Jim.
Received on Thursday, 31 May 2001 05:00:05 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:48 GMT