Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jdom2.output.Format;
- import org.jdom2.output.XMLOutputter;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import org.jdom2.output.Format;
- public class WriteXml {
- public static void main (String args[])
- {
- File docFile = new File("E:\\Enet'com\\Java\\DS_Java\\test.xml");
- Document doc = null;
- try
- {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- doc = db.parse(docFile);
- }
- catch (java.io.IOException e)
- {
- System.out.println("Can't find the file");
- }
- catch (Exception e)
- {
- System.out.print("Problem parsing the file.");
- }
- Element root = doc.getDocumentElement();
- System.out.println("The root element is " + root.getNodeName() + ".\n");
- NodeList children = root.getChildNodes();
- System.out.print("There are "+children.getLength()+" child elements.\n");
- System.out.print("They are: \n");
- //Print the file
- for (Node child = root.getFirstChild(); child != null; child = child.getNextSibling())
- {
- if (child.getNodeType() == child.TEXT_NODE)
- {
- System.out.println("Text: "+child.getNodeValue());
- }
- else if (child.getNodeType() == child.ELEMENT_NODE)
- {
- System.out.println(child.getNodeName()+" = "+child.getFirstChild().getNodeValue());
- }
- }
- //NodeList deleteElement = root.getElementsByTagName("staff");
- //Node deleteNode= deleteElement.item(0);
- //root.removeChild(deleteNode);
- Element FormationElement = doc.createElement("Formation");
- Node updateText = doc.createTextNode("");
- FormationElement.appendChild(updateText);
- //
- Element Name = doc.createElement("nom");
- String str_firstName="added firstname";
- Node NameNode = doc.createTextNode(str_firstName);
- Name.appendChild(NameNode);
- FormationElement.appendChild(Name);
- //
- Element lastName = doc.createElement("lastname");
- String str_lastName="added lastname";
- Node lastNameNode = doc.createTextNode(str_lastName);
- lastName.appendChild(lastNameNode);
- FormationElement.appendChild(lastName);
- //
- Element nickName = doc.createElement("nickname");
- String str_nickName="added nickname";
- Node nickNameNode = doc.createTextNode(str_nickName);
- nickName.appendChild(nickNameNode);
- FormationElement.appendChild(nickName);
- //
- Element salary = doc.createElement("salary");
- String str_salary="$1,000";
- Node salaryNode = doc.createTextNode(str_salary);
- salary.appendChild(salaryNode);
- FormationElement.appendChild(salary);
- //
- root.appendChild(FormationElement);
- //Node StaffNode=(Node)updateElement;
- try{
- String outputURL = "E:\\Enet'com\\Java\\DS_Java\\test.xml";
- DOMSource source = new DOMSource(doc);
- StreamResult result = new StreamResult(new FileOutputStream(outputURL));
- TransformerFactory transFactory = TransformerFactory.newInstance();
- Transformer transformer = transFactory.newTransformer();
- transformer.transform(source, result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement