- From: Eric Lawson <vxdman1@home.com>
- Date: Thu, 31 May 2001 07:46:59 -0400 (EDT)
- To: Tom Leuntjens <tom.leuntjens@bricsnet.com>, www-dom@w3.org
You are using a error prone technique to access a text node by assuming that the companyName element will always have one text node as its content and no more. Unfortunately, there are several error conditions that can make this technique crash. Below is an effective way to iterate through all children of a node, and grabbing the content of their text nodes(and text nodes only): Element companyName = ..... NodeList childNodes = companyName.getChildNodes(); String name = ""; for (int i=0; i < name.getLength(); i++) { if (childNodes.item(i).getNodeType() == TEXT_NODE) { name = name + childNodes.item(i).getNodeValue(); } } At this point the 'name' variable will contain the content of all of the text nodes, or will be empty if no text nodes were present. Remember: Application developers using the DOM should never assume anything. You should *always* do Node type checking, and *never* rely on the position of a node in context for anything(as there is no guarantee that it will be constant). Hope this helps, Eric Lawson Isogen International Tom Leuntjens wrote: > Fairly Simple Question, > > How do I make sure I don't get a null pointer exception (when the XML > document has <companyname></companyname> using this code. > Documentation states this should be a null value > > But > > if (companyname.item(i).getFirstChild().getNodeValue() != null) { > > NodeList companyname = doc.getElementsByTagName("companyname"); > String CompanyName = > companyname.item(i).getFirstChild().getNodeValue(); > out.print(CompanyName + "<br>"); > > } > > Doesn't do the trick > > And all resources on internet with good tutorials using JSP/DOM/XML are > welcome. > > regards, > Tom > > -----Original Message----- > From: www-dom-request@w3.org [mailto:www-dom-request@w3.org]On Behalf Of > Dylan Schiemann > Sent: Wednesday, May 30, 2001 11:19 PM > To: www-dom@w3.org > Subject: range insertNode, what exception should be thrown? > > Consider the following JavaScript syntax: > > var x = document.createRange(); > x.setStart(document.getElementsByTagName("p")[0],0); > x.setEnd(document.getElementsByTagName("p")[0],1); > x.insertNode(document.getElementsByTagName("div")[0]); > > Suppose that the document does not contain any div > elements. What exception should be thrown? Do you > have to check for existence of the node before you > insert it? > > Thanks, > > Dylan Schiemann > http://www.sitepen.com/ > http://www.dylanschiemann.com/ > > __________________________________________________ > Do You Yahoo!? > Get personalized email addresses from Yahoo! Mail - only $35 > a year! http://personal.mail.yahoo.com/
Received on Monday, 4 June 2001 15:48:03 UTC