7

I tried:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
Node mapNode = getMapNode(doc);
System.out.print("\r\n elementName "+ mapNode.getNodeName());//This works fine.

Element e = (Element) mapNode; //This is where the error occurs
//it seems to work on my machine, but not on the server.
e.setAttribute("objectId", "OBJ123");

But this throws a java.lang.ClassCastException error on the line that casts it to Element. mapNode is a valid node. I already have it printing out

I think maybe this code does not work in Java 1.4. What I really need is an alternative to using Element. I tried doing

NamedNodeMap atts = mapNode.getAttributes();
    Attr att = doc.createAttribute("objId");
    att.setValue(docId);    
    atts.setNamedItem(att);

But getAttributes() returns null on the server. Even though its not and I am using the same document locally as on the server. And it can print out the getNodeName() its just that the getAttributes() does not work.

6
  • Could you provide more details? What is the exact stack trace? Commented Sep 30, 2008 at 18:12
  • The only useful info the stack trace says is java.lang.ClassCastException Commented Sep 30, 2008 at 18:16
  • Throws it at the Element e = (Element) doc.getFirstChild() line Commented Sep 30, 2008 at 18:17
  • What does mapNode.getClass().getName() report? If it's not an Element, knowing what it really is will help you solve your problem. Commented Sep 30, 2008 at 19:35
  • Being a valid node does not mean it is an element. What does 'getNodeName' print on both machines? Commented Sep 30, 2008 at 19:44

4 Answers 4

1

I was using a different dtd file on the server. That was causing the issue.

Sign up to request clarification or add additional context in comments.

Comments

0

Might the first child be a whitespace only text node or suchlike?

Try:

System.out.println(doc.getFirstChild().getClass().getName());

EDIT:

Just looked it up in my own code, you need:

doc.getDocumentElement().getChildNodes();

Or:

NodeList nodes = doc.getElementsByTagName("MyTag");

Comments

0

As already noted, the ClassCastException is probably not being thrown in setAttribute. Check the line number in the stack. My guess is that getFirstChild() is returning a DocumentType, not an Element.

Try this:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);

Element e = (Element) doc.getDocumentElement().getFirstChild();
e.setAttribute("objectId", "OBJ123");

Update:

It seems like you are confusing Node and Element. Element is an implementation of Node, but certainly not the only one. So, not all Node's are castable to Element. If the cast is working on one machine and not on another, it's because you're getting something else back from getMapNode() because the parsers are behaving differently. The XML parser is pluggable in Java 1.4, so you could be getting an entirely different implementation, from a different vendor, with different bugs even.

Since you're not posting getMapNode() we cannot see what it's doing, but you should be explicit about what node you want it to return (using getElementsByTagName or otherwise).

1 Comment

Totally agree with your update, if it is a different parser on the server it could be handling whitespace only text nodes differently, which would cause the error (depending on how getMapNode is written)
0

I think your cast of the output of doc.getFirstChild() is where you're getting your exception -- you're getting some non-Element Node object. Does the line number on the stack trace point to that line? You might need to do a doc.getChildNodes() and iterate to find the first Element child (doc root), skipping non-Element Nodes.

Your e.setAttribute() call looks sensible. Assuming e is an Element and you actually get to that line...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.