Hello everyone, (Hi Sylvain),

Being a newcomer to JDOM, I was surprised to see that some classes in
org.dom4j.dom and org.w3c.dom have exactly similar methods as Microsoft DOM
(as in name and semantics). Since it seemed too good to be true (I was
looking to convert my VB code to Java in the first place), I gave it a try.
Thanks to Sylvain, I've learnt a way to use DocumentHelper to do the work
that I need. But I just tried out the following code out of curiosity. Could
anyone please tell me why the code fails at the line indicated?

import org.w3c.dom.*;
import org.dom4j.dom.*;

DOMDocument oDocument = new DOMDocument("Root");
//Add two subnodes with attributes "name" and "id"
Element oParent = oDocument.createElement("Parent"); //<-- Fails here, Error
message is below
oParent.setAttribute("name", "N01");
oParent.setAttribute("id", "ID01");
oDocument.appendChild(oParent);
oParent = oDocument.createElement("Parent");
oParent.setAttribute("name", "N02");
oParent.setAttribute("id", "ID02");
oDocument.appendChild(oParent);

Element oSearch = (Element)oDocument.selectSingleNode("//Parent[@name =
'N01']");
System.out.println(oSearch.getAttribute("id"));

It compiles fine, but gives an error at runtime:

java.lang.ClassCastException: org.dom4j.tree.DefaultElement
        at org.dom4j.dom.DOMDocument.createElement(DOMDocument.java:209)
        at com.interrait.requesttracker.Dump.main(Dump.java:70)
Exception in thread "main"

Anything wrong with the code? I'd appreciate any suggestions.

Thanks
Soumanjoy

_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to