Java XML Tutorial
Selected Reading
- Java XML - Discussion
- Java XML - Useful Resources
- Java XML - Quick Guide
- Java XML - Questions and Answers
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM4J Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java XPath Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java StAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- JDOM XML Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java SAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM Parser
- Java XML Parsers
- Java XML Overview
- Java XML Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Modify XML Document
Java DOM4J Parser - Modify XML Document
Demo Example
Here is the XML that we need to modify −
<?xml version = "1.0"?> <class> <student rollno = "393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>
DOM4jModifyXMLDemo.java
package com.tutorialspoint.xml; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; pubpc class DOM4jModifyXMLDemo { pubpc static void main(String[] args) { try { File inputFile = new File("input.txt"); SAXReader reader = new SAXReader(); Document document = reader.read( inputFile ); Element classElement = document.getRootElement(); List<Node> nodes = document.selectNodes("/class/student[@rollno = 493 ]" ); for (Node node : nodes) { Element element = (Element)node; Iterator<Element> iterator = element.elementIterator("marks"); while(iterator.hasNext()) { Element marksElement = (Element)iterator.next(); marksElement.setText("80"); } } // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer; writer = new XMLWriter( System.out, format ); writer.write( document ); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
This would produce the following result −
<?xml version = "1.0" encoding = "UTF-8"?> <class> <student rollno = "393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>80</marks> </student> <student rollno = "593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>Advertisements