- From: Rajiv Mordani [CONTRACTOR] <mode@wedel.Eng.Sun.COM>
- Date: Thu, 18 Feb 1999 15:40:28 -0500 (EST)
- To: www-dom@w3.org
Hi Nigel, You need to use NodeList and not NodeBase for getElementsByTagName. NodeBase is not meant to be used by developers and is package private in the upcoming version. Here is what you need to do to get this working:. import java.io.File; import com.sun.xml.parser.Resolver; import com.sun.xml.tree.XmlDocument; import com.sun.xml.tree.XmlDocumentBuilder; ***** import org.w3c.dom.NodeList; ***** and not com.sun.xml.tree.NodeBase. getElementsByTagName returns NodeList type and not NodeBase. import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public static void main (String args []) { InputSource input; XmlDocument doc; ***** NodeList nl; ***** and not NodeBase if (args.length != 1) { System.err.println ("Usage: cmd filename"); System.exit (1); } try { // turn the filename into an input source input = Resolver.getSource (new File (args [0])); **** renamed in upcoming version to createInputSource to follow standard factory naming convention **** // turn it into an in-memory object // ... the "false" flag says not to validate doc = XmlDocumentBuilder.createXmlDocument (input, false); // normalize text representation doc.getDocumentElement ().normalize (); // collect child nodes of myDoc nl = doc.getElementsByTagName("myDoc"); } catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); // print stack trace as below } catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); } catch (Throwable t) { t.printStackTrace (); } System.exit (0); } } Rajiv > > Hi guys > > I have just started using Sun's Java xml-ea2 DOM API (with some > success). That was until I started messing around with nodes & > nodelists. I checked out Dom Hamson's original post > http://lists.w3.org/Archives/Public/www-dom/1998OctDec/0129.html "An > observation about "live" NodeLists" and thought compiling a nodelist of > a xml document would be quite simple. so I tried to use his code [2] to > operate on a simple xml document [1] of mine. However, I get some > problems casting from type Node to NodeBase. > > I think the cause of my problem is confusion about (abstract) interfaces > and (concrete) classes, as i'm not a computer coder by trade. So any > assistance regarding my utilisation of DOM will be gratefully received. > > Thanks > > Nigel > > [1] The XML document > > <?xml version="1.0" encoding="UTF-8"?> > <myDoc lenth="short"> > <title>This is Nigel's document</title> > <para>This is a <em>short</em> document.</para> > <para>It only exists to <em>demonstrate<em> a simple</em></em> XML > document.</para> > <figure> > <title>My figure</title> > <graphic fileref="l:\sis\sea\15.jpeg"/> > </figure> > </myDoc> > > [2] Java file > > import java.io.File; > import com.sun.xml.parser.Resolver; > import com.sun.xml.tree.XmlDocument; > import com.sun.xml.tree.XmlDocumentBuilder; > import com.sun.xml.tree.NodeBase; > import org.xml.sax.InputSource; > import org.xml.sax.SAXException; > import org.xml.sax.SAXParseException; > > public class freeDom > { > // > // Reading and writing an XML document stored in a file. > // > public static void main (String args []) > { > InputSource input; > XmlDocument doc; > NodeBase nl; > > > if (args.length != 1) { > System.err.println ("Usage: cmd filename"); > System.exit (1); > } > > try { > // turn the filename into an input source > input = Resolver.getSource (new File (args [0])); > > // turn it into an in-memory object > // ... the "false" flag says not to validate > doc = XmlDocumentBuilder.createXmlDocument (input, false); > > // normalize text representation > doc.getDocumentElement ().normalize (); > > // collect child nodes of myDoc > nl = doc.getElementsByTagName("myDoc"); > > } catch (SAXParseException err) { > System.out.println ("** Parsing error" > + ", line " + err.getLineNumber () > + ", uri " + err.getSystemId ()); > System.out.println(" " + err.getMessage ()); > // print stack trace as below > > } catch (SAXException e) { > Exception x = e.getException (); > > ((x == null) ? e : x).printStackTrace (); > > } catch (Throwable t) { > t.printStackTrace (); > } > > System.exit (0); > } > } > > > > -- > Nigel Byrnes > > "We continue..." Pete Tong > > Software Engineering and Applications Group, > Philips Research Labs, > Redhill. Tel: +44 (0)1293 815578 > RH1 5HA. Fax: +44 (0)1293 815024 > UK. GSM: +44 (0)7899 940391 > Email: byrnes@prl.research.philips.com > > >
Received on Friday, 19 February 1999 02:50:24 UTC