i am updating one xml using dom4j as below.
SAXReader reader = new SAXReader();
document = reader.read( xmlFileName );
but it removes all namespaces from the elements so wanna add manually but it does not work when i tried the following code.
Element e1 = root.addElement("jmsProducer");
e1.addNamespace("AEService", "http://www.tibco.com/xmlns/aemeta/services/2002");
my xml looks like
<AEService:jmsProducer objectType="endpoint.JMSPublisher" name="Pub1EndPoint">
<AEService:wireFormat>aeXml</AEService:wireFormat>
which sud look like
<AEService:jmsProducer xmlns:AEService="http://www.tibco.com/xmlns/aemeta/services /2002" objectType="endpoint.JMSPublisher" name="Pub1EndPoint">
<AEService:wireFormat>aeXml</AEService:wireFormat>
any help is highly appriciated. banging on this for two days tried using documentfactory method still no use.
I realize that this is an old thread, and dom4j may not have had adequate namespace capabilities at the time the answers were written, but it looks like dom4j is now able to accomplish this quite easily with the version I am using (1.6.1). I came here looking for help on how to build a namespace aware XML with dom4j, posting my Java code (to build the XML snippet in the original post) in case it helps someone.
Here is the Java code to build it.
produces this output:
Hope this helps.
I found that in order for the namespace to be correctly set it needed to be recursively set on the entire hierarchy of nodes.
these two methods do the trick:
then I am able to add the namespace like so:
Hope this helps
I suggest you dump dom4j and use JAXB or StAX. If you can't do that then try the below (I didn't verify this so post follow up questions as comments)
I am able to add a namespace at the child element instead of the root element with the below steps:
find the Node using xpath from created document.
create name space prefix name and value.
Add as element into node as below:
Using the library dom4j:2.1.1
After initializing the document:
I can prefix with the namespace
x:
the XPath expression.Note: an empty namespace is not allowed. You need to specify a prefix.