Advertisement
Guest User

read

a guest
Jul 13th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. WriteXMLFile.java
  2. ~~~~~~~~~~~~~~~~~~~~
  3. package javat;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import org.jdom2.Attribute;
  7. import org.jdom2.Document;
  8. import org.jdom2.Element;
  9. import org.jdom2.output.Format;
  10. import org.jdom2.output.XMLOutputter;
  11.  
  12. public class WriteXMLFile {
  13. public static void main(String[] args) {
  14.  
  15. try {
  16.  
  17. Element company = new Element("companys");
  18. Document doc = new Document(company);
  19.  
  20. Element[] staff = new Element[10];
  21. staff[2] = new Element("staff");
  22.  
  23.  
  24. staff[2].setAttribute(new Attribute("id", "1"));
  25. staff[2].addContent(new Element("firstname").setText("yong"));
  26. staff[2].addContent(new Element("lastname").setText("mook kim"));
  27. staff[2].addContent(new Element("nickname").setText("mkyong"));
  28. staff[2].addContent(new Element("salary").setText("199999"));
  29.  
  30. doc.getRootElement().addContent(staff[2]);
  31.  
  32. Element staff3 = new Element("staff");
  33. staff3.setAttribute(new Attribute("id", "2"));
  34. staff3.addContent(new Element("firstname").setText("low"));
  35. staff3.addContent(new Element("lastname").setText("yin fong"));
  36. staff3.addContent(new Element("nickname").setText("fong fong"));
  37. staff3.addContent(new Element("salary").setText("188888"));
  38.  
  39. doc.getRootElement().addContent(staff3);
  40.  
  41. // new XMLOutputter().output(doc, System.out);
  42. XMLOutputter xmlOutput = new XMLOutputter();
  43.  
  44. // display nice nice
  45. xmlOutput.setFormat(Format.getPrettyFormat());
  46. xmlOutput.output(doc, new FileWriter("d:\\file.xml"));
  47.  
  48. System.out.println("File Saved!");
  49. } catch (IOException io) {
  50. System.out.println(io.getMessage());
  51. }
  52. }
  53. }
  54.  
  55.  
  56.  
  57. MOdified.
  58. ~~~~~~~~~~~~~~~~
  59. http://examples.javacodegeeks.com/core-java/xml/parsers/documentbuilderfactory/modify-xml-file-in-java-using-dom-parser-example/
  60. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  61.  
  62. testFile.xml:
  63. ~~~~~~~~~~~~~~~
  64. <?xml version="1.0"?>
  65. <company>
  66.  
  67. <employee id="1">
  68. <firstname>James</firstname>
  69. <lastname>Harley</lastname>
  70. <email>james@example.org</email>
  71. <department>Human Resources</department>
  72. <salary>1000</salary>
  73. </employee>
  74.  
  75. <employee id="2">
  76. <firstname>John</firstname>
  77. <lastname>May</lastname>
  78. <email>john@example.org</email>
  79. <department>Logistics</department>
  80. <salary>400</salary>
  81. </employee>
  82.  
  83. </company>
  84.  
  85. ReadAndModifyXMLFile.java:
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. package com.javacodegeeks.java.core;
  88.  
  89. import java.io.File;
  90. import java.io.IOException;
  91. import javax.xml.parsers.DocumentBuilder;
  92. import javax.xml.parsers.DocumentBuilderFactory;
  93. import javax.xml.parsers.ParserConfigurationException;
  94. import javax.xml.transform.Transformer;
  95. import javax.xml.transform.TransformerException;
  96. import javax.xml.transform.TransformerFactory;
  97. import javax.xml.transform.dom.DOMSource;
  98. import javax.xml.transform.stream.StreamResult;
  99. import org.w3c.dom.Document;
  100. import org.w3c.dom.Element;
  101. import org.w3c.dom.NamedNodeMap;
  102. import org.w3c.dom.Node;
  103. import org.w3c.dom.NodeList;
  104. import org.xml.sax.SAXException;
  105.  
  106. public class ReadAndModifyXMLFile {
  107.  
  108. public static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\files\\testFile.xml";
  109.  
  110. public static void main(String argv[]) {
  111.  
  112. try {
  113.  
  114. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  115.  
  116. DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  117.  
  118. Document document = documentBuilder.parse(xmlFilePath);
  119.  
  120. // Get employee by tag name
  121. //use item(0) to get the first node with tage name "employee"
  122. Node employee = document.getElementsByTagName("employee").item(0);
  123.  
  124. // update employee , set the id to 10
  125. NamedNodeMap attribute = employee.getAttributes();
  126. Node nodeAttr = attribute.getNamedItem("id");
  127. nodeAttr.setTextContent("10");
  128.  
  129. // append a new node to the first employee
  130. Element address = document.createElement("address");
  131.  
  132. address.appendChild(document.createTextNode("34 Stanley St."));
  133.  
  134. employee.appendChild(address);
  135.  
  136. // loop the employee node and update salary value, and delete a node
  137. NodeList nodes = employee.getChildNodes();
  138.  
  139. for (int i = 0; i < nodes.getLength(); i++) {
  140.  
  141. Node element = nodes.item(i);
  142.  
  143. if ("salary".equals(element.getNodeName())) {
  144. element.setTextContent("2000000");
  145. }
  146.  
  147. // remove firstname
  148. if ("firstname".equals(element.getNodeName())) {
  149. employee.removeChild(element);
  150. }
  151.  
  152. }
  153.  
  154. // write the DOM object to the file
  155. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  156.  
  157. Transformer transformer = transformerFactory.newTransformer();
  158. DOMSource domSource = new DOMSource(document);
  159.  
  160. StreamResult streamResult = new StreamResult(new File(xmlFilePath));
  161. transformer.transform(domSource, streamResult);
  162.  
  163. System.out.println("The XML File was ");
  164.  
  165. } catch (ParserConfigurationException pce) {
  166. pce.printStackTrace();
  167. } catch (TransformerException tfe) {
  168. tfe.printStackTrace();
  169. } catch (IOException ioe) {
  170. ioe.printStackTrace();
  171. } catch (SAXException sae) {
  172. sae.printStackTrace();
  173. }
  174. }
  175. }
  176.  
  177. testFile.xml:
  178. ~~~~~~~~~~~~~~~~~~~~~
  179. <?xml version="1.0" encoding="UTF-8" standalone="no"?><company>
  180.  
  181. <employee id="10">
  182.  
  183. <lastname>Harley</lastname>
  184. <email>james@example.org</email>
  185. <department>Human Resources</department>
  186. <salary>2000000</salary>
  187. <address>34 Stanley St.</address><
  188. /employee>
  189.  
  190. <employee id="2">
  191. <firstname>John</firstname>
  192. <lastname>May</lastname>
  193. <email>john@example.org</email>
  194. <department>Logistics</department>
  195. <salary>400</salary>
  196. </employee>
  197.  
  198. </company>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement