95 可读性
 将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Compare a string date from xml file with today's date and delete the node from the xml if the date has expired. The string date is in format dd/MM/yy
将字符串日期与今天的xml文件和当前日期进行比较,如果日期过期,则将该节点从xml中删除 在格式 dd/MM/yy 中,字符串日期为

 public void BrisiOdXML(List<Nastani> lista)
 {
 try {
//reading from xml
 File fXmlFile = new File("/Users/Darko/workspace/Seminarska_1/nastani.xml");
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 Document doc = dBuilder.parse(fXmlFile);
 doc.getDocumentElement().normalize();
 NodeList nList = doc.getElementsByTagName("Nastan");

//today's date//i can't get today's date in dd/MM/yy format without the time

 Calendar today = Calendar.getInstance();
 today.set(Calendar.HOUR_OF_DAY, 0);
 today.set(Calendar.MINUTE, 0);
 today.set(Calendar.SECOND, 0);
 today.set(Calendar.MILLISECOND, 0);

//searching the list

 for (int temp = 0; temp <nList.getLength(); temp++) {
 Node nNode = nList.item(temp); 
 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 Element eElement = (Element) nNode;
//string date from xml dile in format dd/MM/yy 
 Date datexml = (Date) new SimpleDateFormat("dd/MM/yy").parse(eElement.getElementsByTagName("Datum").item(temp).getTextContent()); 
//comparing today's date with date from the xml file
 if (today.after(datexml))
 {
//removing node
 nNode.getParentNode().removeChild(nNode);
 doc.normalize();
 prettyPrint(doc);
 }
 }
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 } 

//printing the new xml file

 public static final void prettyPrint(Document xml) throws Exception {
 Transformer tf = TransformerFactory.newInstance().newTransformer();
 tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
 tf.setOutputProperty(OutputKeys.INDENT,"yes");
 Writer out = new StringWriter();
 tf.transform(new DOMSource(xml), new StreamResult(out));
 System.out.println(out.toString());
 }
}

//it suppose to delete the node from the xml file if the date has expired, but when i read the xml list it only displays error messages and shows tho whole list

时间:原作者:1个回答

What you are comparing in the method after is Calendar vs Date.i am not sure if that's correct.try to convert the datexml to Calendar as well by doing this :

Calendar calendar = Calendar.getInstance();
calendar.setTime(datexml);
原作者:
...