Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!-- Document Root -->
  3. <DATA>
  4. <Settings USERNAME="test" PASSWORD="test" STATUS="active" / >
  5. </DATA>
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9.  
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.NodeList;
  13.  
  14. public class Read {
  15.  
  16. private final static String SETTINGS_LINE = Settings;
  17.  
  18. public void readXML() {
  19.  
  20. try {
  21.  
  22. File xmlFile = new File("Test.xml");
  23. DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
  24. DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
  25. Document doc = documentBuilder.parse(xmlFile);
  26.  
  27. // Normalize the XML file
  28. doc.getDocumentElement().normalize();
  29.  
  30. NodeList nodeList = doc.getDocumentElement().getChildNodes();
  31.  
  32. for(int temp = 0; temp < nodeList.getLength(); temp++) {
  33.  
  34. Node node = nodeList.item(temp);
  35.  
  36. if(node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
  37.  
  38. Element settings = (Element) node;
  39.  
  40. System.out.println("User" +settings.getAttribute("USERNAME"));
  41. System.out.println("Password" +settings.getAttribute("PASSWORD"));
  42. System.out.println("Status" +settings.getAttribute("STATUS"));
  43.  
  44.  
  45. }
  46.  
  47. }
  48.  
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. import java.io.File;
  58. import java.io.IOException;
  59.  
  60. import javax.xml.parsers.DocumentBuilder;
  61. import javax.xml.parsers.DocumentBuilderFactory;
  62. import javax.xml.parsers.ParserConfigurationException;
  63. import javax.xml.transform.Transformer;
  64. import javax.xml.transform.TransformerException;
  65. import javax.xml.transform.TransformerFactory;
  66. import javax.xml.transform.dom.DOMSource;
  67. import javax.xml.transform.stream.StreamResult;
  68.  
  69. import org.w3c.dom.Document;
  70. import org.w3c.dom.Element;
  71. import org.w3c.dom.Node;
  72. import org.w3c.dom.NodeList;
  73. import org.xml.sax.SAXException;
  74.  
  75. public class Modify {
  76.  
  77. private final static String SETTINGS_LINE = "Settings";
  78.  
  79. public static void main(String argv[]) {
  80.  
  81. try {
  82. String filepath = "test.xml";
  83. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  84. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  85.  
  86.  
  87. // Normalize the XML File
  88. doc.getDocumentElement().normalize();
  89.  
  90. NodeList nodeList = doc.getDocumentElement().getChildNodes();
  91.  
  92. for (int i = 0; i < nodeList.getLength(); i++) {
  93.  
  94. Node node = nodeList.item(i);
  95.  
  96. if (node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
  97.  
  98. Element settings = (Element) node;
  99.  
  100. if("USERNAME".equals(node.getChildNodes())){
  101. node.setTextContent("mivnadic");
  102. }
  103.  
  104. }
  105.  
  106. // Write the content into xml file
  107. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  108. Transformer transformer = transformerFactory.newTransformer();
  109. DOMSource source = new DOMSource(doc);
  110. StreamResult result = new StreamResult(new File(filepath));
  111. transformer.transform(source, result);
  112.  
  113. }
  114.  
  115. System.out.println("File saved");
  116.  
  117. } catch (ParserConfigurationException pce) {
  118. pce.printStackTrace();
  119. } catch (TransformerException tfe) {
  120. tfe.printStackTrace();
  121. } catch (IOException ioe) {
  122. ioe.printStackTrace();
  123. } catch (SAXException sae) {
  124. sae.printStackTrace();
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement