-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
7u291, 8u261, 11-pool, 16-pool, 17
Customer reported an issue where setting writer.getDomConfig().setParameter("xml-declaration", true); produces no line break after the xml headers.
Expected:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item/>
</root>
Actual:
<?xml version="1.0" encoding="UTF-8"?><root> <<<<--- missing line break after header
</item>
</root>
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
public class TestToStringLS {
public static void main(final String[] args) {
try {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = dbf.newDocumentBuilder();
final Document doc = builder.newDocument();
final Element element = doc.createElement("root");
doc.appendChild(element);
final Element itemElement = doc.createElement("item");
element.appendChild(itemElement);
itemElement.appendChild(itemElement.getOwnerDocument().createTextNode(""));
System.out.println("------------W3C----------");
writeDocument(doc);
} catch (final Exception ex) {
ex.printStackTrace();
}
}
public static final void writeDocument(final Document doc) {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMImplementationRegistry domRegistry = null;
try {
domRegistry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl =
(DOMImplementationLS) domRegistry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("xml-declaration", true);
writer.getDomConfig().setParameter("format-pretty-print", true);
final LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(outStream);
writer.write(doc, output);
final byte[] result = outStream.toByteArray();
System.out.println(new String(result));
} catch (final Exception e) {
System.err.println("ERROR" + e.getMessage());
e.printStackTrace();
}
}
}
Expected:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item/>
</root>
Actual:
<?xml version="1.0" encoding="UTF-8"?><root> <<<<--- missing line break after header
</item>
</root>
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
public class TestToStringLS {
public static void main(final String[] args) {
try {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = dbf.newDocumentBuilder();
final Document doc = builder.newDocument();
final Element element = doc.createElement("root");
doc.appendChild(element);
final Element itemElement = doc.createElement("item");
element.appendChild(itemElement);
itemElement.appendChild(itemElement.getOwnerDocument().createTextNode(""));
System.out.println("------------W3C----------");
writeDocument(doc);
} catch (final Exception ex) {
ex.printStackTrace();
}
}
public static final void writeDocument(final Document doc) {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMImplementationRegistry domRegistry = null;
try {
domRegistry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl =
(DOMImplementationLS) domRegistry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("xml-declaration", true);
writer.getDomConfig().setParameter("format-pretty-print", true);
final LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(outStream);
writer.write(doc, output);
final byte[] result = outStream.toByteArray();
System.out.println(new String(result));
} catch (final Exception e) {
System.err.println("ERROR" + e.getMessage());
e.printStackTrace();
}
}
}
- duplicates
-
JDK-8261209 isStandalone property: remove dependency on pretty-print
- Resolved
-
JDK-8249867 XML declaration is not followed by a newline
- Closed
- relates to
-
JDK-8035467 Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer.
- Resolved
-
JDK-8238164 Update Xerces2 Java to version 2.12.0
- Resolved