gpt4 book ai didi

java - 如何在java中更新xml文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:46 24 4
gpt4 key购买 nike

我有一个名为 data.xml 的 xml 文件,如下面的代码所示。该项目可以从客户端运行没问题,它可以读取 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

    public class main
{
public static void main(String[] args)
{
Calendar cal2 =null;

try {

//read the xml
File data = new File("data.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(data);
doc.getDocumentElement().normalize();

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
username = getValue("username", element);
startdate = getValue("startdate", element);
enddate = getValue("enddate", element);
}
}


date = startdate;
Date date_int = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(date);
cal2 = Calendar.getInstance();
cal2.setTime(date_int);


//loop the child node to update the initial date
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;

setValue("startdate", element , date_int.toString());
}
}

//write the content in xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("data.xml"));
transformer.transform(source, result);

} catch (Exception ex) {
log.error(ex.getMessage());
ex.printStackTrace();
}
}


private static void setValue(String tag, Element element , String input) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
node.setTextContent(input);

}

最佳答案

从加载 XML 文件开始...

DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(new File("Data.xml"));

现在,有几种方法可以做到这一点,但简单地说,您可以使用 xpath API 找到您想要的节点并更新它们的内容

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");

然后将 Document 保存回文件...

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);

关于java - 如何在java中更新xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com