Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import org.jdom2.output.Format;
  2. import org.jdom2.output.XMLOutputter;
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.Element;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.NodeList;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.transform.Transformer;
  10. import javax.xml.transform.TransformerFactory;
  11. import javax.xml.transform.dom.DOMSource;
  12. import javax.xml.transform.stream.StreamResult;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.FileWriter;
  16. import org.jdom2.output.Format;
  17.  
  18. public class WriteXml {
  19.     public static void main (String args[])
  20.     {
  21.  
  22.         File docFile = new File("E:\\Enet'com\\Java\\DS_Java\\test.xml");
  23.  
  24.         Document doc = null;
  25.         try
  26.         {
  27.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  28.             DocumentBuilder db = dbf.newDocumentBuilder();
  29.             doc = db.parse(docFile);
  30.         }
  31.         catch (java.io.IOException e)
  32.         {
  33.             System.out.println("Can't find the file");
  34.         }
  35.         catch (Exception e)
  36.         {
  37.             System.out.print("Problem parsing the file.");
  38.         }
  39.  
  40.         Element root = doc.getDocumentElement();
  41.  
  42.         System.out.println("The root element is " + root.getNodeName() + ".\n");
  43.  
  44.         NodeList children = root.getChildNodes();
  45.         System.out.print("There are "+children.getLength()+" child elements.\n");
  46.         System.out.print("They are: \n");
  47.  
  48. //Print the file
  49.         for (Node child = root.getFirstChild(); child != null; child = child.getNextSibling())
  50.         {
  51.             if (child.getNodeType() == child.TEXT_NODE)
  52.             {
  53.                 System.out.println("Text: "+child.getNodeValue());
  54.             }
  55.             else if (child.getNodeType() == child.ELEMENT_NODE)
  56.             {
  57.                 System.out.println(child.getNodeName()+" = "+child.getFirstChild().getNodeValue());
  58.             }
  59.         }
  60.  
  61.  
  62. //NodeList deleteElement = root.getElementsByTagName("staff");
  63.  
  64. //Node deleteNode= deleteElement.item(0);
  65.  
  66. //root.removeChild(deleteNode);
  67.         Element FormationElement = doc.createElement("Formation");
  68.         Node updateText = doc.createTextNode("");
  69.         FormationElement.appendChild(updateText);
  70. //
  71.         Element Name = doc.createElement("nom");
  72.         String str_firstName="added firstname";
  73.         Node NameNode = doc.createTextNode(str_firstName);
  74.         Name.appendChild(NameNode);
  75.  
  76.         FormationElement.appendChild(Name);
  77.  
  78. //
  79.  
  80.         Element lastName = doc.createElement("lastname");
  81.         String str_lastName="added lastname";
  82.         Node lastNameNode = doc.createTextNode(str_lastName);
  83.         lastName.appendChild(lastNameNode);
  84.  
  85.         FormationElement.appendChild(lastName);
  86.  
  87.  
  88. //
  89.         Element nickName = doc.createElement("nickname");
  90.         String str_nickName="added nickname";
  91.         Node nickNameNode = doc.createTextNode(str_nickName);
  92.         nickName.appendChild(nickNameNode);
  93.         FormationElement.appendChild(nickName);
  94.  
  95.  
  96. //
  97.         Element salary = doc.createElement("salary");
  98.         String str_salary="$1,000";
  99.         Node salaryNode = doc.createTextNode(str_salary);
  100.         salary.appendChild(salaryNode);
  101.         FormationElement.appendChild(salary);
  102.  
  103.  
  104. //
  105.         root.appendChild(FormationElement);
  106.  
  107. //Node StaffNode=(Node)updateElement;
  108.  
  109.  
  110.  
  111.  
  112.  
  113.         try{
  114.             String outputURL = "E:\\Enet'com\\Java\\DS_Java\\test.xml";
  115.  
  116.             DOMSource source = new DOMSource(doc);
  117.             StreamResult result = new StreamResult(new FileOutputStream(outputURL));
  118.  
  119.             TransformerFactory transFactory = TransformerFactory.newInstance();
  120.             Transformer transformer = transFactory.newTransformer();
  121.  
  122.             transformer.transform(source, result);
  123.         } catch (Exception e) {
  124.             e.printStackTrace();
  125.         }
  126.  
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement