JDOM adding content into the same namespace

Viewed 10

I am trying to add an OCR to an XML file. When I try it, jdom always adds an empty namespace. I tried to set it with rootE.setNamespace(Namespace.getNamespace("http://www.tei-c.org/ns/1.0")); the xmlns="" still gets added.

        SAXBuilder sax = new SAXBuilder();
        Document doc = sax.build(new File("src/main/resources/MS_transcript.xml"));
        Element rootNode = doc.getRootElement();
        Element div2Node = div1Node.getChild("div", Namespace.getNamespace("http://www.tei-c.org/ns/1.0"));

        String finalOCR = "<div>" + String.join("", ocrContent) + "</div>";
        finalOCR = finalOCR.replace("&", " &amp;");
        StringReader stringReader = new StringReader(finalOCR);
        Document ocr = sax.build(stringReader);
        Element rootE = ocr.getRootElement();
        rootE.setNamespace(Namespace.getNamespace("http://www.tei-c.org/ns/1.0"));
        System.out.println(div2Node.getAttributes());
        System.out.println(rootE.getContent());
        div2Node.setContent(rootE.cloneContent());
        XMLOutputter xmlOutput = new XMLOutputter();

        // write to console
        // xmlOutput.output(doc, System.out);

        // write to a file
        try (FileOutputStream output =
                     new FileOutputStream("src/main/resources/MS_transcript1.xml")) {
            xmlOutput.output(doc, output);
        }
0 Answers
Related