There are a dozen threads regarding that topic, but all of them contain answers that do not work for me in a satisfactory manner. It seems one needs to use a specific DOM implementation. However, I cannot get it to read the xml input:
@Test
public void testPrettyPrintConvertDomLevel3() {
String unformattedXml
= "<?xml version=\"1.0\" encoding=\"UTF-16\"?><QueryMessage\n"
+ " xmlns=\"http://ift.tt/1m0rTGL\"\n"
+ " xmlns:query=\"http://ift.tt/WTKLe8\">\n"
+ " <Query>\n"
+ " <query:CategorySchemeWhere>\n"
+ " \t\t\t\t\t <query:AgencyID>ECB\n\n\n\n</query:AgencyID>\n"
+ " </query:CategorySchemeWhere>\n"
+ " </Query>\n\n\n\n\n"
+ "</QueryMessage>";
System.out.println(prettyPrintWithXercesDomLevel3(unformattedXml));
}
Here is the method:
public static String prettyPrintWithXercesDomLevel3(String xml) {
try {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
if (impl == null) {
throw new RuntimeException("No DOMImplementation found !");
}
log.info(String.format("DOMImplementationLS: %s", impl.getClass().getName()));
LSParser parser = impl.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
"http://ift.tt/1Haow7f");
// http://ift.tt/tphNwY
log.info(String.format("LSParser: %s", parser.getClass().getName()));
LSInput lsi = impl.createLSInput();
lsi.setByteStream(new ByteArrayInputStream(xml.getBytes()));
Document doc = parser.parse(lsi);
LSSerializer serializer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
output.setByteStream(baos);
serializer.write(doc, output);
return baos.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
And this is what I get:
java.lang.RuntimeException: org.w3c.dom.ls.LSException: Content is not allowed in prolog.
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParserImpl.parse(Unknown Source)
at tests.javax.xml.XMLWriterTest.prettyPrintWithXercesDomLevel3(XMLWriterTest.java:176)
at tests.javax.xml.XMLWriterTest.testPrettyPrintConvertDomLevel3(XMLWriterTest.java:219)
No comments:
Post a Comment