如何在Java中更新xml文件

我有一个xml文件,调用data.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>

我的main.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中更新xml文件 的全部内容, 来源链接: utcz.com/qa/405556.html

回到顶部