In a Java program, i am processing an xml using dom4j.
Now, I want to update an attribute of an element.
This is the code I am using to obtain that element--
SAXReader reader = new SAXReader();
doc = reader.read(new StringReader(xmlinput));
Element root = doc.getRootElement();
for ( Iterator i = root.elementIterator( "cloudwhile" ); i.hasNext(); ) {
Element foo = (Element) i.next();
Now, I want to update the value of an attribute of element 'foo'--
For this I am trying to use the following code--
foo.setAttributeValue("indexstart", (String) newstart );
However the above method is deprecated... how do I update the attribute now? Also, I want to take the string representation of the modified xml, immediately after updating the attribute of element 'foo'- how do I do that?
JavaDoc says to use addAttribute(...)
instead. The name is somewhat misleading, as it will replace the content of an existing attribute - what is equal to updating a value.
Adds the attribute value of the given fully qualified name. If an attribute already exists for the given name it will be replaced. Attributes with null values are silently ignored. If the value of the attribute is null then this method call will remove any attributes with the given name.