Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行时动态更新xml文件_Java_Xml - Fatal编程技术网

Java 在运行时动态更新xml文件

Java 在运行时动态更新xml文件,java,xml,Java,Xml,如何使用java程序在运行时动态编辑xml文件。例如,我有这样的xml文件 <chart> <set label="Item A" value="4"/> <set label="Item B" value="5"/> <set label="Item C" ``alue="6"/> <set label="Item D" value="7"/> </chart> 我尝试了JDOMAPI在运行时更新xml。但它只能编辑

如何使用java程序在运行时动态编辑xml文件。例如,我有这样的xml文件

<chart>
<set label="Item A" value="4"/>
<set label="Item B" value="5"/>
<set label="Item C" ``alue="6"/>
<set label="Item D" value="7"/>
</chart>


我尝试了JDOMAPI在运行时更新xml。但它只能编辑单个值标记。但这里我有同名的多任务。我想在运行时动态更改每个标记的值。谁能给我提些建议吗

假设您使用的是jdom-1.1.2.jar

Document doc = (Document) builder.build(YourFileName);
Element rootNode = doc.getRootElement();
List<Element> childrenNode = rootNode.getChildren();
      for (Element child : childrenNode) {
           System.out.println(child.getAttribute("value").getIntValue());
           child.getAttribute("value").setValue("2");
      }

// print updates to xml file with good formatting
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter(YourFileName));
documentdoc=(Document)builder.build(YourFileName);
Element rootNode=doc.getRootElement();
List childrenNode=rootNode.getChildren();
for(元素子元素:childrenNode){
System.out.println(child.getAttribute(“value”).getIntValue();
child.getAttribute(“值”).setValue(“2”);
}
//将更新打印到格式良好的xml文件
XMLOutputter xmlOutput=新的XMLOutputter();
setFormat(Format.getPrettyFormat());
output(doc,新的FileWriter(YourFileName));

谢谢你的回答,朋友。我使用dom4japi完成了这个任务。无论如何,非常感谢你的回答。。