Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package xulyxml;
- import com.sun.org.apache.xml.internal.serialize.OutputFormat;
- import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
- import java.io.FileOutputStream;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.xpath.XPath;
- import javax.xml.xpath.XPathConstants;
- import javax.xml.xpath.XPathFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- public class XPathDemo {
- public static void main(String[] args) throws Exception {
- InputSource xml = new InputSource("MalwareData.xml");
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(xml);
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xpath = xpf.newXPath();
- String exp = "/malware/timestamp";
- NodeList names = (NodeList) xpath.evaluate(exp, doc, XPathConstants.NODESET);
- for (int i = 0; i < names.getLength(); i++) {
- Element elem = (Element) names.item(i);
- //get current date time with Date()
- Date date = new Date();
- DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
- String timeCurrent = dateFormat.format(date);
- // Sua file MalwareData.xml
- names.item(i).setTextContent(timeCurrent);
- System.err.println(elem.getTextContent());
- }
- //Kết quả trả về là UtCung
- //Ghi file xml
- FileOutputStream output = new FileOutputStream("MalwareData.xml");
- XMLSerializer ser = new XMLSerializer(output, new OutputFormat(doc, "UTF-8", true));
- ser.serialize(doc);
- output.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment