Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!-- Document Root -->
- <DATA>
- <Settings USERNAME="test" PASSWORD="test" STATUS="active" / >
- </DATA>
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- public class Read {
- private final static String SETTINGS_LINE = Settings;
- public void readXML() {
- try {
- File xmlFile = new File("Test.xml");
- DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
- Document doc = documentBuilder.parse(xmlFile);
- // Normalize the XML file
- doc.getDocumentElement().normalize();
- NodeList nodeList = doc.getDocumentElement().getChildNodes();
- for(int temp = 0; temp < nodeList.getLength(); temp++) {
- Node node = nodeList.item(temp);
- if(node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
- Element settings = (Element) node;
- System.out.println("User" +settings.getAttribute("USERNAME"));
- System.out.println("Password" +settings.getAttribute("PASSWORD"));
- System.out.println("Status" +settings.getAttribute("STATUS"));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- 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.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class Modify {
- private final static String SETTINGS_LINE = "Settings";
- public static void main(String argv[]) {
- try {
- String filepath = "test.xml";
- DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
- // Normalize the XML File
- doc.getDocumentElement().normalize();
- NodeList nodeList = doc.getDocumentElement().getChildNodes();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- if (node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
- Element settings = (Element) node;
- if("USERNAME".equals(node.getChildNodes())){
- node.setTextContent("mivnadic");
- }
- }
- // Write the content into xml file
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- DOMSource source = new DOMSource(doc);
- StreamResult result = new StreamResult(new File(filepath));
- transformer.transform(source, result);
- }
- System.out.println("File saved");
- } catch (ParserConfigurationException pce) {
- pce.printStackTrace();
- } catch (TransformerException tfe) {
- tfe.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } catch (SAXException sae) {
- sae.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement