I am trying to insert a new child at a certain position in an existing XML document. To do this, I have first parsed the existing XML document and now want to edit and save it.
In the imports i use
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Element;
import org.jdom2.Document;
import org.jdom2.Namespace;
import org.jdom2.input.DOMBuilder;
import org.jdom2.output.DOMOutputter;
This is how I parse the files and convert them to jdom2 format
DocumentBuilder builder = null;
org.jdom2.input.DOMBuilder jdomBuilder = new DOMBuilder();
Document tmpDoc = jdomBuilder.build( builder.parse(new FileInputStream(tmpFilePath)));
This is how i try to edit and save the files
Document tmpDoc = archivedFiles.get(id);
Element eleWithTracking = (Element) new Element("TRACKING_ID").addContent(idAndTracking.get(id));
Element root = (Element) tmpDoc.getRootElement();
Element header = (Element) root.getChild("ORDER_HEADER", Namespace.getNamespace("http://www.opentrans.org/XMLSchema/2.1"));
header.addContent(eleWithTracking);
tmpDoc.setContent(root);
try {
//Create the XML
XMLOutputter outter=new XMLOutputter();
outter.setFormat(Format.getPrettyFormat());
outter.output(tmpDoc, new FileWriter(new File(id + "_withTracking.xml")));
} catch (IOException ex) {
Logger.getLogger(Reverse_nes.class.getName()).log(Level.SEVERE, null, ex);
}
However, I cannot find the child I just inserted (eleWithTracking) anywhere in the output file.
Furthermore, the output file is not formatted in the same way as the source file. I don't have any nodes there that I can expand, but only pure text.
Unfortunately I don't know Java that well yet and i don't get any error messages.