[英]What is the best approach to create multiple xml elements with same name with Java?
Let's say I want to create this xml:假设我想创建这个 xml:
<root>
<element>
text 1
</element>
<element>
text 2
</element>
<element>
text 3
</element>
.
.
.
n elements
</root>
With a java loop, I having trouble creating it with jdom.element, since at the second iteration, it says that there's already an element called "element", and I have read that once attached, you can't create more of the same element, so use clone or something, but I haven't figure out how, and I believe this should be fairly simple.使用 java 循环,我无法使用 jdom.element 创建它,因为在第二次迭代时,它说已经有一个名为“element”的元素,并且我已经阅读了附加的元素,您无法创建更多相同的元素元素,所以使用克隆或其他东西,但我还没有弄清楚如何,我相信这应该相当简单。
int i = 0;
int n = 100;
while(i < n){
(Missing code)
}
Missing code is what I need.缺少代码是我需要的。
EDIT: Sorry for being lazy, I've added code to exemplify better what I needed to do, and what worked, but @rolfl understood what I wanted to do and my problem, and he solved it.编辑:对不起,我很懒惰,我已经添加了代码来更好地说明我需要做什么,什么有效,但@rolfl 了解我想做什么和我的问题,他解决了它。 Thank you.
谢谢你。 And sorry again everyone for being lazy.
再次对不起大家懒惰。
My code:我的代码:
Element eElements = new org.jdom.Element("Elements");
Element eElement;
Element eSubElement1 = new org.jdom.Element("SubElement1");
Element eSubElement2 = new org.jdom.Element("SubElement2");
int i = 0;
int n = 100;
while (i < n){
eSubElement1.setText("Text " + i);
eSubElement2.setText("Text " + i);
eElement = new org.jdom.Element("Element");
eElement.addContent(eSubElement1);
eElement.addContent(eSubElement2);
eElements.addContent(eElement);
}
I thought that calling a new "Element" everytime would be enough, but you have to call new "SubElementX" too.我认为每次调用一个新的“元素”就足够了,但你也必须调用新的“SubElementX”。
While loop that worked:有效的while循环:
while (i < n){
eSubElement1 = new org.jdom.Element("SubElement1").setText("Text " + i);
eSubElement2 = new org.jdom.Element("SubElement2").setText("Text " + i);
eElement = new org.jdom.Element("Element");
eElement.addContent(eSubElement1);
eElement.addContent(eSubElement2);
eElements.addContent(eElement);
}
You are obviously trying to add the same instance multiple times, or something.您显然是在尝试多次添加相同的实例,或者什么的。 You should create a new instance of the Element "element" for each value.
您应该为每个值创建一个新的元素“元素”实例。
Something like:就像是:
Element root = new Element("root");
for (int i = 0; i < 100; i++) {
root.addContent(new Element("element").setText("Text " + i));
}
Then output the root
element using XMLOutputter
(Use the Format.getPrettyFormat()
on your outputter to get nice whitespace in the results.然后使用
XMLOutputter
输出root
元素Format.getPrettyFormat()
在输出器上使用Format.getPrettyFormat()
以在结果中获得漂亮的空白。
For XML processing (marshalling/unmarshalling) I would suggest JAXB instead of jdom.对于 XML 处理(编组/解组),我建议使用 JAXB 而不是 jdom。
Not knowing your exact use case (I assume it could be more than a simple text, at least in theory), I would use POJOs that represent a xml-element and another POJO that represents the xml-root and basically just holds a list of the xml-element-POJOs.不知道你的确切用例(我认为它可能不仅仅是一个简单的文本,至少在理论上),我会使用代表 xml-element 的 POJO 和代表 xml-root 的另一个 POJO,基本上只包含一个列表xml-element-POJO。
With that you would populate xml-root-POJO with the xml-element-POJOs in your while-loop and after you are done you hand the xml-root-POJO to JAXB for marshalling it into an xml-file.这样,您将在 while 循环中使用 xml-element-POJO 填充 xml-root-POJO,完成后将 xml-root-POJO 交给 JAXB 以将其编组到 xml 文件中。
In your case, the POJOs may should be annotated like @XmlRootElement(name = "root")
and @XmlElement(name = "element")
在您的情况下,POJO 可能应该像
@XmlRootElement(name = "root")
和@XmlElement(name = "element")
一样注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.