- From: Nigel Byrnes <byrnes@prl.research.philips.com>
- Date: Thu, 18 Feb 1999 16:30:34 +0000
- To: www-dom@w3.org
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 Thursday, 18 February 1999 11:32:20 UTC