0

Cant figure out why XMLUtils.outputDOM is not outputing anything

import org.apache.xml.security.utils.XMLUtils;

DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
InputSource is = new InputSource(new StringReader("<EDoc></EDoc>"));
Document doc = dbf.newDocumentBuilder().parse(is);
Element root = doc.getDocumentElement();
System.out.println(root);
OutputStream os = new FileOutputStream("out_xs.xml");
XMLUtils.outputDOM(root, os, true);
System.out.println(XMLUtils.getFullTextChildrenFromElement(root));

Output is:

[EDoc: null]
(empty string)

out_xs.xml:

<?xml version="1.0" encoding="UTF-8"?>

I can get the root element OK, but file is generated with xml definition only as true is provided. What is going on? I am using same function in other place where it works after library uses Document, so I suspect Document should be notified to commit changes? Cant find any information using search

4
  • 1
    Did you close the OutputStream? Commented Sep 26, 2019 at 6:27
  • @Kayaman added os.close() after outputDOM, nothing changed Commented Sep 26, 2019 at 6:29
  • 1
    I'm confused. Your question seems to be about the out_xs.xml file not getting XML, but you don't show us what is actually in the file. Instead you show us that getFullTextChildrenFromElement(root) returns an empty string, which has nothing whatsoever to do with outputDOM(root, os, true), and blank output is expected, since there are no text nodes in the XML, so not sure why that code is even there, except it is confusing the issue. Please clean up the question, to clarify it. Commented Sep 26, 2019 at 6:55
  • @Andreas updated question and added out_xs.xml contents Commented Sep 26, 2019 at 6:57

1 Answer 1

1

You question is a bit confusing / unclear. I ran the code provided and the xml file generated "out_xs.xml" contains the following...

<?xml version="1.0" encoding="UTF-8"?>
<EDoc></EDoc>

... which is correct according to the code block you've specified.

I did have to make a slight change to your code though, had to add the line Init.init(); just above XMLUtils.outputDOM(root, os, true);. Otherwise I kept getting the exception org.apache.xml.security.c14n.InvalidCanonicalizerException: You must initialize the xml-security library correctly before you use it

Maybe check your dependency versions, I used the following in Gradle:

// https://mvnrepository.com/artifact/xml-security/xmlsec
compile group: 'xml-security', name: 'xmlsec', version: '1.3.0'

// https://mvnrepository.com/artifact/commons-logging/commons-logging
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.