I have a xml file call data.xml like the code below. The project can run from client side no problem and it can read the xml file. The problem I have now is I I want to writ
Firstly there is an error in your XML you have extra <data>
tag . I have removed it. Now you have two options you could either use SAX
or DOM
. I would suggest DOM
reason being that you can read full XML using DOM
and for a small piece of XML like this it's better choice.
Code
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
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.Node;
import org.xml.sax.SAXException;
public class ModifyXMLFile {
public static void main(String argv[]) {
try {
String filepath = "file.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
// Get the root element
Node data= doc.getFirstChild();
Node startdate = doc.getElementsByTagName("startdate").item(0);
// I am not doing any thing with it just for showing you
String currentStartdate = startdate.getNodeValue();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date today = Calendar.getInstance().getTime();
startdate.setTextContent(df.format(today));
// 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("Done");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Corrected XML
<?xml version="1.0" encoding="UTF-8"?>
<data>
<username>admin</username>
<password>12345</password>
<interval>1</interval>
<timeout>90</timeout>
<startdate>29/07/2015</startdate>
<enddate>06/01/2013</enddate>
<ttime>1110</ttime>
</data>
Underscore-java library can read xml to the linked hash map and regenerate xml after modification. I am the maintainer of the project. Live example
import com.github.underscore.lodash.U;
public class MyClass {
public static void main(String args[]) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
+ "<data>"
+ " <username>admin</username>"
+ " <password>12345</password>"
+ " <interval>1</interval>"
+ " <timeout>90</timeout>"
+ " <startdate>01/01/2013</startdate>"
+ " <enddate>06/01/2013</enddate>"
+ " <ttime>1110</ttime>"
+ " </data>";
java.util.Map<String, Object> object = U.fromXmlMap(xml);
U.set(object, "data.startdate", "02/02/2013");
U.set(object, "data.enddate", "07/02/2013");
System.out.println(U.toXml(object));
}
}
// <?xml version="1.0" encoding="UTF-8" standalone="no"?>
// <data>
// <username>admin</username>
// <password>12345</password>
// <interval>1</interval>
// <timeout>90</timeout>
// <startdate>02/02/2013</startdate>
// <enddate>07/02/2013</enddate>
// <ttime>1110</ttime>
// </data>
This an example which i have tried for updating the xml files.
String filepath="Test.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Node company = doc.getFirstChild();
/**
* Get the param from xml and set value
*/
Node search = doc.getElementsByTagName("parameter").item(0);
NamedNodeMap attr = search.getAttributes();
Node nodeAttr = attr.getNamedItem("value");
nodeAttr.setTextContent(param);
/**
* write it back to the xml
*/
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("Done");
Following is the XML file i have used:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testing" parallel="false">
<parameter name="Search" value="param1"></parameter>
<test name="TestParams" preserve-order="true">
<classes>
<class name="uiscreen.TestingParam"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Hope it helps!
Start by loading the XML file...
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(new File("Data.xml"));
Now, there are a few ways to do this, but simply, you can use the xpath API to find the nodes you want and update their content
XPath xPath = XPathFactory.newInstance().newXPath();
Node startDateNode = (Node) xPath.compile("/data/startdate").evaluate(doc, XPathConstants.NODE);
startDateNode.setTextContent("29/07/2015");
xPath = XPathFactory.newInstance().newXPath();
Node endDateNode = (Node) xPath.compile("/data/enddate").evaluate(doc, XPathConstants.NODE);
endDateNode.setTextContent("29/07/2015");
Then save the Document
back to the file...
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource domSource = new DOMSource(doc);
StreamResult sr = new StreamResult(new File("Data.xml"));
tf.transform(domSource, sr);