I'm using dom4j to create a XML. I would like to add sub tags into another tag, as following.
Before:
<section>
<title>Nice</title>
</section>
After
<section>
<title>Nice<title>
<entry>Hi</entry>
<entry>wow</entry>
</section>
the tag "entry" are other Element Object coming from some api, and I try to such as
Element section = component.addElement("section");
section.add(entry);
or
section.addElement(entry);
but just get errors.
How can I add Element objects into other Element Object?
thanks a lot.
SAXReader reader = new SAXReader();
Document doc = reader.read("xxx.xml");
Element root = doc.getRootElement();
Element entry = DocumentHelper.createElement("entry");
entry.setText("Hi");
root.element("section").add(entry);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("xxx.xml"),format);
writer.write(doc);
writer.close();