Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WriteXMLFile.java
- ~~~~~~~~~~~~~~~~~~~~
- package javat;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.jdom2.Attribute;
- import org.jdom2.Document;
- import org.jdom2.Element;
- import org.jdom2.output.Format;
- import org.jdom2.output.XMLOutputter;
- public class WriteXMLFile {
- public static void main(String[] args) {
- try {
- Element company = new Element("companys");
- Document doc = new Document(company);
- Element[] staff = new Element[10];
- staff[2] = new Element("staff");
- staff[2].setAttribute(new Attribute("id", "1"));
- staff[2].addContent(new Element("firstname").setText("yong"));
- staff[2].addContent(new Element("lastname").setText("mook kim"));
- staff[2].addContent(new Element("nickname").setText("mkyong"));
- staff[2].addContent(new Element("salary").setText("199999"));
- doc.getRootElement().addContent(staff[2]);
- Element staff3 = new Element("staff");
- staff3.setAttribute(new Attribute("id", "2"));
- staff3.addContent(new Element("firstname").setText("low"));
- staff3.addContent(new Element("lastname").setText("yin fong"));
- staff3.addContent(new Element("nickname").setText("fong fong"));
- staff3.addContent(new Element("salary").setText("188888"));
- doc.getRootElement().addContent(staff3);
- // new XMLOutputter().output(doc, System.out);
- XMLOutputter xmlOutput = new XMLOutputter();
- // display nice nice
- xmlOutput.setFormat(Format.getPrettyFormat());
- xmlOutput.output(doc, new FileWriter("d:\\file.xml"));
- System.out.println("File Saved!");
- } catch (IOException io) {
- System.out.println(io.getMessage());
- }
- }
- }
- MOdified.
- ~~~~~~~~~~~~~~~~
- http://examples.javacodegeeks.com/core-java/xml/parsers/documentbuilderfactory/modify-xml-file-in-java-using-dom-parser-example/
- ~~~~~~~~~~~~~~~~~~~~~~~~~~
- testFile.xml:
- ~~~~~~~~~~~~~~~
- <?xml version="1.0"?>
- <company>
- <employee id="1">
- <firstname>James</firstname>
- <lastname>Harley</lastname>
- <email>james@example.org</email>
- <department>Human Resources</department>
- <salary>1000</salary>
- </employee>
- <employee id="2">
- <firstname>John</firstname>
- <lastname>May</lastname>
- <email>john@example.org</email>
- <department>Logistics</department>
- <salary>400</salary>
- </employee>
- </company>
- ReadAndModifyXMLFile.java:
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- package com.javacodegeeks.java.core;
- import java.io.File;
- import java.io.IOException;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.Transformer;
- 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.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class ReadAndModifyXMLFile {
- public static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\files\\testFile.xml";
- public static void main(String argv[]) {
- try {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- Document document = documentBuilder.parse(xmlFilePath);
- // Get employee by tag name
- //use item(0) to get the first node with tage name "employee"
- Node employee = document.getElementsByTagName("employee").item(0);
- // update employee , set the id to 10
- NamedNodeMap attribute = employee.getAttributes();
- Node nodeAttr = attribute.getNamedItem("id");
- nodeAttr.setTextContent("10");
- // append a new node to the first employee
- Element address = document.createElement("address");
- address.appendChild(document.createTextNode("34 Stanley St."));
- employee.appendChild(address);
- // loop the employee node and update salary value, and delete a node
- NodeList nodes = employee.getChildNodes();
- for (int i = 0; i < nodes.getLength(); i++) {
- Node element = nodes.item(i);
- if ("salary".equals(element.getNodeName())) {
- element.setTextContent("2000000");
- }
- // remove firstname
- if ("firstname".equals(element.getNodeName())) {
- employee.removeChild(element);
- }
- }
- // write the DOM object to the file
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- DOMSource domSource = new DOMSource(document);
- StreamResult streamResult = new StreamResult(new File(xmlFilePath));
- transformer.transform(domSource, streamResult);
- System.out.println("The XML File was ");
- } catch (ParserConfigurationException pce) {
- pce.printStackTrace();
- } catch (TransformerException tfe) {
- tfe.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } catch (SAXException sae) {
- sae.printStackTrace();
- }
- }
- }
- testFile.xml:
- ~~~~~~~~~~~~~~~~~~~~~
- <?xml version="1.0" encoding="UTF-8" standalone="no"?><company>
- <employee id="10">
- <lastname>Harley</lastname>
- <email>james@example.org</email>
- <department>Human Resources</department>
- <salary>2000000</salary>
- <address>34 Stanley St.</address><
- /employee>
- <employee id="2">
- <firstname>John</firstname>
- <lastname>May</lastname>
- <email>john@example.org</email>
- <department>Logistics</department>
- <salary>400</salary>
- </employee>
- </company>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement