简体   繁体   中英

How to add an attribute to an XML node in Java 1.4

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.

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

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");

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...

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM