Java修改XML中某元素值,生成新的XML文件。结果其他的空值元素出现问题,求助
现在有一个XML文件,其中有一个元素形式为<processid>XXX</processid>,我需要将该元素值修改成一个自增的值,然后批量生成新的XML文件。我使用如下...
现在有一个XML文件,其中有一个元素形式为<processid>XXX</processid>,我需要将该元素值修改成一个自增的值,然后批量生成新的XML文件。我使用如下的程序实现了该过程,但是问题来了!原XML文件中类似于<priority></priority>这样的空值元素的标签在新XML中变成了<priority/>。求各位大神解答怎么保持空值标签形式不变,或者给一个新的方式解决这个问题。
代码如下:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.NodeList;
public class generateXML {
public static void main(String[] args) {
int times = 1;
String path1 = "D:\\Process_process2F.xml";
String path2 = "D:\\test\\2f1000\\Process_process2F";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(path1);
NodeList list=document.getElementsByTagName("processid");
for (int i = 1; i <= times; i++) {
String num=i+"";
Element brandElement =(Element) list.item(0);
brandElement.setTextContent("process2F"+num);
Source xmlSource = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result result = new StreamResult(new File(path2 + num + ".xml"));
transformer.transform(xmlSource, result);
}
System.out.println("执行成功!输出了"+times+"份XML文件");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
} 展开
代码如下:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.NodeList;
public class generateXML {
public static void main(String[] args) {
int times = 1;
String path1 = "D:\\Process_process2F.xml";
String path2 = "D:\\test\\2f1000\\Process_process2F";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(path1);
NodeList list=document.getElementsByTagName("processid");
for (int i = 1; i <= times; i++) {
String num=i+"";
Element brandElement =(Element) list.item(0);
brandElement.setTextContent("process2F"+num);
Source xmlSource = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result result = new StreamResult(new File(path2 + num + ".xml"));
transformer.transform(xmlSource, result);
}
System.out.println("执行成功!输出了"+times+"份XML文件");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询