Java通过替换复制xml节点和子节点
我有一个 XML 模板,其中包含以下节点(简化的):
<items>
<sl:each value="iter" ignoreonzero="total">
<item>
<description><sl:prop value="desc" /></description>
<total><sl:prop value="total" /></description>
</item>
</sl:each>
</items>
我可以获取迭代器(ArrayList)并获取对象的值。我只是不知道如何使用整个节点作为模板(
包装器除外),并保持其子节点(及其递归子节点)完好无损。我需要将
节点替换为 ArrayList 中对象的值,并为每个项目重复该值。
所需输出示例:
<items>
<item>
<description>item 1</description>
<total>1.23</description>
</item>
<item>
<description>item 2</description>
<total>3.21</description>
</item>
</items>
我一直在尝试:请问有什么帮助吗?
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
NodeList eaches = itemsElement.getElementsByTagNameNS("sl","each");
for (int i=0;i<eaches.getLength();i++)
{
Node origNode = eaches.item(i);
/*
Code to get ArrayList and object
*/
for (Object o : iter) {
Node node = origNode.cloneNode(true);
NodeList props = ((Element) node).getElementsByTagNameNS("sl","prop");
for (int j=0;j<props.getLength();j++) {
Node prop = props.item(j);
String textContent = "";
/*
Code to get text content
*/
Node parent = prop.getParentNode();
Node text = doc.createTextNode(textContent);
parent.replaceChild(prop,text);
}
}
}
I have an XML template with nodes like (simplified):
<items>
<sl:each value="iter" ignoreonzero="total">
<item>
<description><sl:prop value="desc" /></description>
<total><sl:prop value="total" /></description>
</item>
</sl:each>
</items>
I can get the iterator (an ArrayList) and get the values of the object. I just can't figure out how to use this entire node as a template (except the <sl:each>
wrapper), keeping it's children (and their children recursive) intact. I need to replace the <sl:prop />
nodes with the value from the object in the ArrayList, reapeated for each item.
Sample Desired output:
<items>
<item>
<description>item 1</description>
<total>1.23</description>
</item>
<item>
<description>item 2</description>
<total>3.21</description>
</item>
</items>
What I've been trying: Any help please?
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
NodeList eaches = itemsElement.getElementsByTagNameNS("sl","each");
for (int i=0;i<eaches.getLength();i++)
{
Node origNode = eaches.item(i);
/*
Code to get ArrayList and object
*/
for (Object o : iter) {
Node node = origNode.cloneNode(true);
NodeList props = ((Element) node).getElementsByTagNameNS("sl","prop");
for (int j=0;j<props.getLength();j++) {
Node prop = props.item(j);
String textContent = "";
/*
Code to get text content
*/
Node parent = prop.getParentNode();
Node text = doc.createTextNode(textContent);
parent.replaceChild(prop,text);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
Node node = origNode.cloneNode(true);
后,您应该在eaches
父节点上调用insertAfter
。不要忘记在迭代后删除
eaches
节点!After calling
Node node = origNode.cloneNode(true);
you should callinsertAfter
oneaches
parent node.Don't forget to remove the
eaches
node after the iteration!