如何使用处理将内容添加到 XML
How to add Content to XML with Proceesing
我想将数组的值添加到某个 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 ) {
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 文件中保存 1000 到 1901 之间的所有数字。
提前致谢。
您发布的代码有些地方看起来有点不对劲:
- 您在尝试加载同一个节点时多次加载和保存同一个 xml 文件。
- 您正在尝试通过看起来像 xpath 的方式访问节点,但我认为 Processing 的 XML 库不支持这种方式。但是,您可以通过名称获取嵌套节点:
XML firstChild = xml.getChild("childTwo");
- 您有一个从未满足的条件:
if (j < multiZips.length)
。 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 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");
还不清楚为什么要迭代两次,当您可以重新使用此循环时:for (int i = start; i < end; i++)
还可以添加 XML 内容。
我想将数组的值添加到某个 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 ) {
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 文件中保存 1000 到 1901 之间的所有数字。
提前致谢。
您发布的代码有些地方看起来有点不对劲:
- 您在尝试加载同一个节点时多次加载和保存同一个 xml 文件。
- 您正在尝试通过看起来像 xpath 的方式访问节点,但我认为 Processing 的 XML 库不支持这种方式。但是,您可以通过名称获取嵌套节点:
XML firstChild = xml.getChild("childTwo");
- 您有一个从未满足的条件:
if (j < multiZips.length)
。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 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");
还不清楚为什么要迭代两次,当您可以重新使用此循环时:for (int i = start; i < end; i++)
还可以添加 XML 内容。