Java 如何使用过程向XML添加内容
我想将数组的值添加到某个xml文件中。在我的代码中,只需添加一个数字,并用以下数字替换 代码如下:Java 如何使用过程向XML添加内容,java,xml,processing,Java,Xml,Processing,我想将数组的值添加到某个xml文件中。在我的代码中,只需添加一个数字,并用以下数字替换 代码如下: XML xml; int start = 1000; int end = 1901; int[] multiZips = new int[end- start]; for (int i = start; i < end; i++) { multiZips[i-start] = i; } for (int j : multiZips ) {
XML xml;
int start = 1000;
int end = 1901;
int[] multiZips = new int[end- start];
for (int i = start; i < end; i++) {
multiZips[i-start] = i;
}
for (int j : multiZips ) {
String zip = str(j);
xml = loadXML("sample.xml");
XML firstChild = xml.getChild("childOne/childTwo");
firstChild.setContent(zip + ", ");
print(firstChild.getContent());
if (j < multiZips.length) {
saveXML(xml, "sample.xml");
}
}
XML;
int start=1000;
int end=1901;
int[]multiZips=newint[end-start];
for(int i=start;i
我想在xml文件中保存1000到1901之间的所有数字
提前感谢。您发布的代码有一些地方看起来有点不对劲:
xmlfirstchild=XML.getChild(“childTwo”)代码>
如果(j
j
从1000
到1900
,这比901
XML xml;
int start = 1000;
int end = 1901;
int[] multiZips = new int[end- start];
for (int i = start; i < end; i++) {
multiZips[i-start] = i;
}
//load the XML once
xml = loadXML("sample.xml");
//get a reference to the child you want to append to
XML firstChild = xml.getChild("childTwo");
//create a string to concatenate to
String zips = "";
for (int j : multiZips ) {
String zip = str(j);
//append to string
zips += (zip + ", ");
}
//add the concatenated string
firstChild.setContent(zips);
//save once
saveXML(xml, "sample.xml");
XML;
int start=1000;
int end=1901;
int[]multiZips=newint[end-start];
for(int i=start;i
如果要保存单个节点,也可以执行以下操作:
XML xml;
int start = 1000;
int end = 1901;
int[] multiZips = new int[end- start];
for (int i = start; i < end; i++) {
multiZips[i-start] = i;
}
//load once
xml = loadXML("sample.xml");
//get a reference to <childtwo>
XML firstChild = xml.getChild("childTwo");
for (int j : multiZips ) {
String zip = str(j);
//create a new node (in this case we'll call it zip, but it can be something else)
XML newNode = new XML("zip");
//set the value as it's content
newNode.setContent(zip);
//append the new node to the <childTwo> node
firstChild.addChild(newNode);
}
//save once
saveXML(xml,"sample.xml");
XML;
int start=1000;
int end=1901;
int[]multiZips=newint[end-start];
for(int i=start;i
当您可以重复使用这个循环时,也不清楚为什么要迭代两次:
for(int i=start;i
也可以添加XML内容。期望的结果会很好,您的问题是什么?非常感谢@George,我非常感谢您的帮助。现在它开始工作了!在尝试了几个小时不同的东西之后。(我是编码界的新手)。来自奥地利的干杯。很高兴我的回答有帮助。当事情在几个小时内无法正常工作时,我建议简化代码,将代码分成几个部分(它是做什么的?(例如,1.加载数据,2.循环数据,3.追加数据,4.保存数据)如何实现每个步骤)?我还建议放慢速度,访问可用的参考和教程。例如,它是全面的,也包含XML