我正在尝试更改活动A04的PlannedDuration,但无法看到XML文件中的更改。所做的更改不会更新到文件中。我不知道我在做什么错。
XML太长。该文件包含有关多个活动的信息。我只对ID A04的活动感兴趣。
PlannedDuration的初始值为16,我将其更改为25。
package modifyXML;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ModifyXML{
public static void main(String[] args) {
File xmlfile = new File("C:/Users/dianitaf/Downloads/xml Files/CS_1.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(xmlfile);
document.getDocumentElement().normalize();
updateElementValue(document,"Id", "A04");
document.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("xmlfile"));
transformer.transform(source, result);
System.out.println("XML file updated successfully");
} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
private static void updateElementValue(Document document, String textNodeName, String textNodeValue) {
NodeList Activity = document.getElementsByTagName("Activity");
Element act = null;
//loop for each activity
for(int i=0; i<Activity.getLength();i++){
Node activityNode = Activity.item(i);
if(activityNode.getNodeType() == Node.ELEMENT_NODE)
{
Element activityElement = (Element) activityNode;
NodeList textNodes = activityElement.getElementsByTagName(textNodeName);
if(textNodes.getLength() > 0)
{
if(textNodes.item(0).getTextContent().equalsIgnoreCase(textNodeValue))
{
System.out.println(textNodes.item(0).getTextContent());
System.out.println(activityElement.getElementsByTagName("Name").item(0).getTextContent());
act = (Element) Activity.item(i);
Node duration = act.getElementsByTagName("PlannedDuration").item(0).getFirstChild();
System.out.println("Initial Duration : " + act.getElementsByTagName("PlannedDuration").item(0).getTextContent()); act.getElementsByTagName("PlannedDuration").item(0).getTextContent();
duration.setTextContent("25");
System.out.println("Updaded Duration : " + act.getElementsByTagName("PlannedDuration").item(0).getTextContent());
}
}
}
}
}
}
答案 0 :(得分:0)
XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><APIBusinessObjects>
<ProjectList>
<Project ObjectId="4627">
<Id>CS_1</Id>
<Name>Case Study No.1 Warehouse Project - CPM</Name>
</Project>
</ProjectList>
<Project>
<Activity>
<Id>A04</Id>
<Name>Nailers to Purlins</Name>
<PlannedDuration>16</PlannedDuration>
<StartDate>2018-12-26Te8:00:00</StartDate>
</Activity>
</Project>
</APIBusinessObjects>
Java:
package modifyXML;
import java.io.File;
import java.io.IOException;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ModifyXML {
public static void main(String[] args) {
File xmlfile = new File("D:\\delete\\test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(xmlfile);
document.getDocumentElement().normalize();
updateElementValue(document, "Id", "A04");
document.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(xmlfile);
transformer.transform(source, result);
System.out.println("XML file updated successfully");
} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
private static void updateElementValue(Document document, String textNodeName, String textNodeValue) {
NodeList Activity = document.getElementsByTagName("Activity");
Element act = null;
// loop for each activity
for (int i = 0; i < Activity.getLength(); i++) {
Node activityNode = Activity.item(i);
if (activityNode.getNodeType() == Node.ELEMENT_NODE) {
Element activityElement = (Element) activityNode;
NodeList textNodes = activityElement.getElementsByTagName(textNodeName);
if (textNodes.getLength() > 0) {
if (textNodes.item(0).getTextContent().equalsIgnoreCase(textNodeValue)) {
System.out.println(textNodes.item(0).getTextContent());
System.out.println(activityElement.getElementsByTagName("Name").item(0).getTextContent());
act = (Element) Activity.item(i);
Node duration = act.getElementsByTagName("PlannedDuration").item(0).getFirstChild();
System.out.println("Initial Duration : "
+ act.getElementsByTagName("PlannedDuration").item(0).getTextContent());
act.getElementsByTagName("PlannedDuration").item(0).getTextContent();
duration.setTextContent("25");
System.out.println("Updaded Duration : "
+ act.getElementsByTagName("PlannedDuration").item(0).getTextContent());
}
}
}
}
}
}