com.sun.org.apache.xml.internal.serialize.XMLSerializer Java Examples
The following examples show how to use
com.sun.org.apache.xml.internal.serialize.XMLSerializer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextUtil.java From JVoiceXML with GNU Lesser General Public License v2.1 | 6 votes |
public String xmlFilePrint(String fileName) { try { File file = new File(fileName); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(file); OutputFormat format = new OutputFormat(document); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); Writer out = new StringWriter(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(document); return out.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; }
Example #2
Source File: XmlUtil.java From gameserver with Apache License 2.0 | 6 votes |
/** * Format the given string as xml content. * @param xml * @return */ public static String formatXml(String xml) { try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = domFactory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); Document doc = domBuilder.parse(is); OutputFormat format = new OutputFormat(doc); format.setLineWidth(80); format.setIndent(2); format.setIndenting(true); StringWriter out = new StringWriter(); XMLSerializer xmls = new XMLSerializer(out, format); xmls.serialize(doc); return out.toString(); } catch (Exception e) { e.printStackTrace(); } return xml; }
Example #3
Source File: CustomSchemaParser.java From rice with Educational Community License v2.0 | 6 votes |
protected String nodeToString(Node node) { StringWriter stringOut = new StringWriter(); OutputFormat format = new OutputFormat(Method.XML, null, false); format.setOmitXMLDeclaration(true); XMLSerializer serial = new XMLSerializer(stringOut, format); try { serial.serialize(node); } catch (IOException e) { throw new RuntimeException(e); } return stringOut.toString(); }
Example #4
Source File: JaxbParser.java From sword-lang with Apache License 2.0 | 6 votes |
/** * 转为xml串 * @param obj * @return */ public String toXML(Object obj){ String result = null; try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头 OutputStream os = new ByteOutputStream(); StringWriter writer = new StringWriter(); XMLSerializer serializer = getXMLSerializer(os); m.marshal(obj, serializer.asContentHandler()); result = os.toString(); } catch (Exception e) { e.printStackTrace(); } logger.info("response text:" + result); return result; }
Example #5
Source File: XmlReport.java From livingdoc-core with GNU General Public License v3.0 | 5 votes |
@Override public void printTo(Writer out) throws IOException { if (dom == null) { return; } StringWriter sw = new StringWriter(); OutputFormat format = new OutputFormat(dom); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(sw, format); serializer.serialize(dom); out.write(sw.toString()); out.flush(); }
Example #6
Source File: JaxbParser.java From sword-lang with Apache License 2.0 | 5 votes |
/** * 设置属性 * @param os * @return */ private XMLSerializer getXMLSerializer(OutputStream os) { OutputFormat of = new OutputFormat(); formatCDataTag(); of.setCDataElements(cdataNode); of.setPreserveSpace(true); of.setIndenting(true); of.setOmitXMLDeclaration(true); XMLSerializer serializer = new XMLSerializer(of); serializer.setOutputByteStream(os); return serializer; }
Example #7
Source File: XmlUtils.java From Doradus with Apache License 2.0 | 5 votes |
static public String formatXml(String xmlText, String prefix) throws Exception { if (xmlText == null) return xmlText; Document doc = parseXml(xmlText); OutputFormat format = new OutputFormat(doc); format.setLineWidth(120); format.setIndenting(true); format.setIndent(4); Writer out = new StringWriter(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(doc); xmlText = StringUtils.trim(out.toString(), " \r\n"); if (xmlText.startsWith("<?xml")) { int ind = xmlText.indexOf("?>"); if (ind > 1) { xmlText = xmlText.substring(ind + 2); xmlText = StringUtils.trim(xmlText, " \r\n"); } } return StringUtils.formatText(xmlText, prefix); }
Example #8
Source File: TransformerTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }
Example #9
Source File: TransformerTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }
Example #10
Source File: TransformerTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }
Example #11
Source File: TransformerTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }
Example #12
Source File: TransformerTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }
Example #13
Source File: TransformerTest.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
@Test public final void testBug8162598() throws IOException, TransformerException { final String LINE_SEPARATOR = System.getProperty("line.separator"); final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + " <xsl:template match=\"/\">" + LINE_SEPARATOR + " <root xmlns=\"ns1\">" + LINE_SEPARATOR + " <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + " </root>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + " <xsl:template name=\"transform\">" + LINE_SEPARATOR + " <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + " <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + " <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + " <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + " <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + " <test6 xmlns=\"\"/>" + LINE_SEPARATOR + " </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR; System.out.println("Stylesheet:"); System.out.println("============================="); System.out.println(xsl); System.out.println(); System.out.println("Source before transformation:"); System.out.println("============================="); System.out.println(sourceXml); System.out.println(); System.out.println("Result after transformation:"); System.out.println("============================"); Document document = transformInputStreamToDocument( createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes())); OutputFormat format = new OutputFormat(); format.setIndenting(true); new XMLSerializer(System.out, format).serialize(document); System.out.println(); checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null); checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null); Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6"); }