
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
Java JDOM Element setAttributes() Method
The Java JDOM setAttributes() method of Element class is used to set a list of attributes to an XML Element. If an Element already has attributes, then those old attributes list is replaced with this supplied new attributes list.
The setAttributes() method removes the old attributes list when an empty list is passed as a parameter to it.If the list contains duplicate attributes, then the last attribute alone gets attached to the element. It behaves the same way as calling the setAttribute() method multiple times on a specific attribute and only the attribute that is passed in the last call gets retained.
Syntax
Following is the syntax of the Java JDOM Element setAttributes() method −
Element.setAttributes(newAttributes);
Parameters
The Java setAttributes() method accepts a single parameter −
newAttributes − represents a list of attributes that need to be set to an XML Element.
Return Value
The Java setAttributes() method returns the modified XML Element after adding attributes.
Example 1
Here is the basic example of using the Java JDOM Element setAttributes() method −
import java.util.ArrayList; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class SetAttributes { public static void main(String args[]) { try { //Create Document and add root Document doc = new Document(); Element root = new Element("book"); doc.setRootElement(root); //Create attributes list Attribute attr1 = new Attribute("type", "single rule"); Attribute attr2 = new Attribute("company", "Camlin"); List<Attribute> attrList = new ArrayList<Attribute>(); attrList.add(attr1); attrList.add(attr2); //Add attribute list to root root.setAttributes(attrList); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The XML document after adding new attributes is displayed.
<?xml version="1.0" encoding="UTF-8"?> <book type="single rule" company="Camlin" />
Example 2
We need to parse the following book.xml file −
<?xml version="1.0" encoding="UTF-8"?> <book type="single rule" company="Camlin" />
The setAttributes() method clears the attributes of an XML Element when an empty list is passed as an argument to it.
import java.io.File; import java.util.ArrayList; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class SetAttributes { public static void main(String args[]) { try { //Reading the document and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("book.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Create an empty attributes list List<Attribute> attrList = new ArrayList<Attribute>(); //Add attribute list to root root.setAttributes(attrList); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The XML document after clearing the attributes is displayed.
<?xml version="1.0" encoding="UTF-8"?> <book />