
- 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 setName() Method
The Java JDOM setName() method of Element class is used to modify the local name of an already existing XML Element. This method throws an IllegalNameException if the given local name is illegal to be the name of an XML Element.
Syntax
Following is the syntax of the Java JDOM Element setName() method −
Element.setName(name);
Parameters
The Java Element.setName() method accepts a single parameter −
name − represents the local name of an Element to set.
Return Value
The Java setName() method returns the modified XML Element.
Example 1
Here is the basic example of using the Java JDOM Element setName() method −
import org.jdom2.Element; public class SetName { public static void main(String args[]) { try { //Create Document and add root Element root = new Element("book"); Element upadatedRoot = root.setName("college"); System.out.println(upadatedRoot); } catch (Exception e) { e.printStackTrace(); } } }
The XML element after updating the local name is displayed.
[Element: <college/>]
Example 2
We need to parse the following book.xml file −
<book> <name>War and Peace</name> <cost>1400</cost> </book>
The setName() method is used in the following example to change the local name of the first child from name to bookName.
import java.io.File; 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 SetName { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("book.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); Element name = root.getChild("name"); name.setName("bookName"); //print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The XML document after updating the local name of the element is displayed.
<?xml version="1.0" encoding="UTF-8"?> <book> <bookName>War and Peace</bookName> <cost>1400</cost> </book>