Hello David,
in case you know parent node is element, you can change type of variable:
Element parentElement = (Element) parentNode;
But with dom4j you should better do this:
private static void updateNode(Document document, String elementName, String
text) {
Namespace xdp = Namespace.get("xdp", "http://ns.adobe.com/xdp/");
Namespace xdp = Namespace.get("xfa", "
http://www.xfa.org/schema/xfa-data/1.0/");
Element parent = document.getRootElement().element(QName.get("xdp",
xdp)).element(QName.get("datasets", xfa)).element(QName.get("data",
xfa)).element("form1");
Element dataNode = parent.element(elementName);
if (dataNode == null) {
dataNode = parent.addElement(elementName);
}
dataNode.setText(text);
}
Sincerely,
Filip Jirsák
2010/4/22 David Bates <[email protected]>
> Hey guys,
>
>
>
> This is driving me nuts.
>
> I’m trying to write a method which will update the an element’s text.
>
> However, if the element doesn’t yet exist, I want to create it. How are
> you meant to do this??!
>
>
>
> *private* *static* *void* updateNode(Document d, String elementName,
> String text) {
>
> Map map = *new* HashMap();
>
> map.put("xdp", "http://ns.adobe.com/xdp/");
>
> map.put("xfa", "http://www.xfa.org/schema/xfa-data/1.0/");
>
>
>
> *final* String PARENT_NODE =
> "/xdp:xdp/xfa:datasets/xfa:data/form1";
>
> String fullyQualified = PARENT_NODE + "/" + elementName;
>
> XPath xPath = DocumentHelper.*createXPath*(fullyQualified);
>
> xPath.setNamespaceURIs(map);
>
>
>
> Node dataNode = xPath.selectSingleNode(d);
>
> *if* (dataNode == *null*) {
>
> // *Find the parent node to which we will add the new element…*
>
> xPath = DocumentHelper.*createXPath*(PARENT_NODE);
>
> xPath.setNamespaceURIs(map);
>
>
>
> Node parentNode = xPath.selectSingleNode(d);
>
> // *TODO*: Add new element elementName below
> parentNode...
>
> }
>
> dataNode.setText(text);
>
> }
>
>
>
> It doesn’t seem possible with the Node interface which is returned. Help!
>
>
>
> Many thanks,
>
>
>
> David.
>
> Note: The information contained in this message and any attachment to it
> is privileged, confidential and protected from disclosure. If the reader of
> this message is not the intended recipient, or an employee or agent
> responsible for delivering this message to the intended recipient, you are
> hereby notified that any dissemination, distribution or copying of this
> communication is strictly prohibited. If you have received this
> communication in error, please notify the sender immediately by replying to
> the message, and please delete it from your system. Thank you. Cynergy
> Systems.
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> dom4j-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>
>
------------------------------------------------------------------------------
_______________________________________________
dom4j-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dom4j-user