Hello all,
I'm trying to insert a new node using xpath.
This is easy to do, but when i'm trying to add attributes to that node I get an exception. Here is my code:
reservationExpression = xpath.compile("/hotels/hotel/room[@id='" + roomId + "']");
Node room = (Node) reservationExpression.evaluate(doc, XPathConstants.NODE);
Node reservation =(Node) doc.createElement("reservation");
Attr toDateAttr = doc.createAttribute("toDate");
toDateAttr.setNodeValue(toDate);
Attr fromDateAttr = doc.createAttribute("fromDate");
fromDateAttr.setNodeValue(fromDate);
Attr idAttr = doc.createAttribute("id");
idAttr.setNodeValue("123");
reservation.appendChild(toDateAttr);
reservation.appendChild(fromDateAttr);
reservation.appendChild(idAttr);
room.appendChild(reservation);
Source source = new DOMSource(doc);
Result result = new StreamResult(filePath);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
The exception that I get is the following:
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementImpl cannot be cast to org.jdom.Element
I'm guessing this is not the right way to add an attribute to a node?
Any ideas would be appreciated!!
Thanks!!