Still now we learn how to read XMLfile using DOM, SAX and STAX parser and we know that only DOM and STAX have the ability to write XML file.Here we learn how to write XML file using DOM and later we know how to write XML File using STAX parser.
Step 1:
Create DocumentBuilderFactory object .
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
Step 2:
Create DocumentBuilder object .
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Step 3:
Create Document object .
Document dom = domBuilder.newDocument();
Step 4:
Create Element object .
Element rootStart = dom.createElement("elementName")
Step 5:
Add this object to dom .
dom.appendChild(rootStart);
If this node is under any node then add this under any element object
rootStart.appendChild(rootStart)
Step 6:
Create Attribute Object and set value
Attr attribte = dom.createAttribute("attributeName");
attribte.setValue("attributeValue");
Add Attribute object with appropriate node
root.setAttributeNode(attribte);
Step 7:
Create TransformerFactory object.
TransformerFactory transFactory = TransformerFactory.newInstance();
Step 8:
Create Transformer object.
Transformer transformer = transFactory.newTransformer();
Step 9:
Create DOMSource Object .
DOMSource domSource = new DOMSource(dom);
Step 10:
Create StreamResult object and show the file into the console or save in file.
//Show the file in console
StreamResult console = new StreamResult(System.out);
transformer.transform(domSource, console);
//Save the XML File
StreamResult xmlFile = new StreamResult(new File("File Path"));
transformer.transform(domSource, xmlFile);
Example Code Given Below :
package domparsing;
import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class DOMWriterExample {
public static void main(String[] args) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
/**
* Create DocumentBuilderFactory Object.
*/
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
/**
* Create DocumentBuilder object.
*/
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
/**
* Create Document object.
*/
Document dom = domBuilder.newDocument();
/**
* Create root element students and add to DOM .
*/
Element rootStart = dom.createElement("students");
dom.appendChild(rootStart);
/**
* Add student node under students node.
*/
Element root = dom.createElement("student");
rootStart.appendChild(root);
/**
* Adding Attribute in student node.
*/
Attr attribte = dom.createAttribute("id");
attribte.setValue("1");
root.setAttributeNode(attribte);
/**
* Add fname,lname and section under student node and attribute also.
*/
Element childNodeFirstName = dom.createElement("fname");
childNodeFirstName.setTextContent("TestFirstName");
root.appendChild(childNodeFirstName);
Element childNodeLastName = dom.createElement("lname");
childNodeLastName.setTextContent("TestLastName");
root.appendChild(childNodeLastName);
Element childNodeSectionName = dom.createElement("sectionname");
childNodeSectionName.setAttribute("rollno", "1");
childNodeSectionName.setTextContent("A");
root.appendChild(childNodeSectionName);
/**
* Create TransformerFactory and set property.
*/
TransformerFactory transFactory = TransformerFactory.newInstance();
//transFactory.setAttribute("indent-number", 4);
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
/**
* Create DOMSource object and pass DOM object as a parameter.
*/
DOMSource domSource = new DOMSource(dom);
/**
* Showing output on Console.
*/
StreamResult console = new StreamResult(System.out);
transformer.transform(domSource, console);
/*
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(domSource, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
*/
/**
* Storing XML into the file.
*/
StreamResult xmlFile = new StreamResult(new File("C:\\file.xml"));
transformer.transform(domSource, xmlFile);
}
}
Step 1:
Create DocumentBuilderFactory object .
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
Step 2:
Create DocumentBuilder object .
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Step 3:
Create Document object .
Document dom = domBuilder.newDocument();
Step 4:
Create Element object .
Element rootStart = dom.createElement("elementName")
Step 5:
Add this object to dom .
dom.appendChild(rootStart);
If this node is under any node then add this under any element object
rootStart.appendChild(rootStart)
Step 6:
Create Attribute Object and set value
Attr attribte = dom.createAttribute("attributeName");
attribte.setValue("attributeValue");
Add Attribute object with appropriate node
root.setAttributeNode(attribte);
Step 7:
Create TransformerFactory object.
TransformerFactory transFactory = TransformerFactory.newInstance();
Step 8:
Create Transformer object.
Transformer transformer = transFactory.newTransformer();
Step 9:
Create DOMSource Object .
DOMSource domSource = new DOMSource(dom);
Step 10:
Create StreamResult object and show the file into the console or save in file.
//Show the file in console
StreamResult console = new StreamResult(System.out);
transformer.transform(domSource, console);
//Save the XML File
StreamResult xmlFile = new StreamResult(new File("File Path"));
transformer.transform(domSource, xmlFile);
Example Code Given Below :
package domparsing;
import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class DOMWriterExample {
public static void main(String[] args) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
/**
* Create DocumentBuilderFactory Object.
*/
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
/**
* Create DocumentBuilder object.
*/
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
/**
* Create Document object.
*/
Document dom = domBuilder.newDocument();
/**
* Create root element students and add to DOM .
*/
Element rootStart = dom.createElement("students");
dom.appendChild(rootStart);
/**
* Add student node under students node.
*/
Element root = dom.createElement("student");
rootStart.appendChild(root);
/**
* Adding Attribute in student node.
*/
Attr attribte = dom.createAttribute("id");
attribte.setValue("1");
root.setAttributeNode(attribte);
/**
* Add fname,lname and section under student node and attribute also.
*/
Element childNodeFirstName = dom.createElement("fname");
childNodeFirstName.setTextContent("TestFirstName");
root.appendChild(childNodeFirstName);
Element childNodeLastName = dom.createElement("lname");
childNodeLastName.setTextContent("TestLastName");
root.appendChild(childNodeLastName);
Element childNodeSectionName = dom.createElement("sectionname");
childNodeSectionName.setAttribute("rollno", "1");
childNodeSectionName.setTextContent("A");
root.appendChild(childNodeSectionName);
/**
* Create TransformerFactory and set property.
*/
TransformerFactory transFactory = TransformerFactory.newInstance();
//transFactory.setAttribute("indent-number", 4);
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
/**
* Create DOMSource object and pass DOM object as a parameter.
*/
DOMSource domSource = new DOMSource(dom);
/**
* Showing output on Console.
*/
StreamResult console = new StreamResult(System.out);
transformer.transform(domSource, console);
/*
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(domSource, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
*/
/**
* Storing XML into the file.
*/
StreamResult xmlFile = new StreamResult(new File("C:\\file.xml"));
transformer.transform(domSource, xmlFile);
}
}
No comments:
Post a Comment