请高手帮忙,下变的代码怎么改可以循环插入xml节点不同的值,下边代码插入到xml节点,都是同一个值啊。
packagebao;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileWriter;...
package bao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLResult;
import org.dom4j.io.XMLWriter;
public class UpdateFlash {
public static Document readXml() {
Document doc = null;
SAXReader saxReader = new SAXReader();
try {
doc = saxReader.read(new File("js/MSLine.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
return doc;
}
public static void update1Xml(Document doc,String zj,String zjl) {
List<Element> list = doc.selectNodes("/chart/dataset/set");
//doc.selectSingleNode("id=a");
if(!list.isEmpty()) {
int i=1;
for( Element element : list) {
if(i<=list.size()/2){
element.addAttribute("value",zj);
}else{
element.addAttribute("value",zjl);
}
i++;
}
}
try {
XMLWriter output = new XMLWriter(new FileWriter(
new File("js/MSLine.xml")));
output.write(doc);
output.close();
} catch (Exception e) {
}
}
} 展开
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLResult;
import org.dom4j.io.XMLWriter;
public class UpdateFlash {
public static Document readXml() {
Document doc = null;
SAXReader saxReader = new SAXReader();
try {
doc = saxReader.read(new File("js/MSLine.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
return doc;
}
public static void update1Xml(Document doc,String zj,String zjl) {
List<Element> list = doc.selectNodes("/chart/dataset/set");
//doc.selectSingleNode("id=a");
if(!list.isEmpty()) {
int i=1;
for( Element element : list) {
if(i<=list.size()/2){
element.addAttribute("value",zj);
}else{
element.addAttribute("value",zjl);
}
i++;
}
}
try {
XMLWriter output = new XMLWriter(new FileWriter(
new File("js/MSLine.xml")));
output.write(doc);
output.close();
} catch (Exception e) {
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询