public String toString(final Reflections reflections) { Document document = createDocument(reflections); try { StringWriter writer = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint()); xmlWriter.write(document); xmlWriter.close(); return writer.toString(); } catch (IOException e) { throw new RuntimeException(); } }
private static void dump(Document document) { if ( !log.isTraceEnabled() ) { return; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Writer w = new PrintWriter( baos ); try { final XMLWriter xw = new XMLWriter( w, new OutputFormat( " ", true ) ); xw.write( document ); w.flush(); } catch (IOException e1) { throw new RuntimeException( "Error dumping enhanced class", e1 ); } log.tracef( "Envers-generate entity mapping -----------------------------\n%s", baos.toString() ); log.trace( "------------------------------------------------------------" ); } }
//Prepare JAXB objects JAXBContext jc = JAXBContext.newInstance("jaxb.package"); Marshaller m = jc.createMarshaller(); //Define an output file File output = new File("test.xml"); //Create a filter that will remove the xmlns attribute NamespaceFilter outFilter = new NamespaceFilter(null, false); //Do some formatting, this is obviously optional and may effect performance OutputFormat format = new OutputFormat(); format.setIndent(true); format.setNewlines(true); //Create a new org.dom4j.io.XMLWriter that will serve as the //ContentHandler for our filter. XMLWriter writer = new XMLWriter(new FileOutputStream(output), format); //Attach the writer to the filter outFilter.setContentHandler(writer); //Tell JAXB to marshall to the filter which in turn will call the writer m.marshal(myJaxbObject, outFilter);
/** * @since 1.4 */ public Dom4JDriver(NameCoder nameCoder) { this(new DocumentFactory(), OutputFormat.createPrettyPrint(), nameCoder); outputFormat.setTrimText(false); }
/** * A static helper method to create the default compact format. This format * does not have any indentation or newlines after an alement and all other * whitespace trimmed * * @return DOCUMENT ME! */ public static OutputFormat createCompactFormat() { OutputFormat format = new OutputFormat(); format.setIndent(false); format.setNewlines(false); format.setTrimText(true); return format; } }
public void testSurrogatePairAttribute() throws IOException { // U+1F427 PENGUIN final String penguin = "\ud83d\udc27"; Document document = DocumentHelper.createDocument(); document.addElement("doc").addAttribute("penguin", penguin); OutputFormat outputFormat = OutputFormat.createCompactFormat(); outputFormat.setSuppressDeclaration(true); outputFormat.setEncoding("US-ASCII"); StringWriter stringWriter = new StringWriter(); XMLWriter writer = new XMLWriter(stringWriter, outputFormat); writer.write(document); writer.close(); Assert.assertEquals(stringWriter.toString(), "<doc penguin=\"🐧\"/>"); }
public String asXML() { try { StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, new OutputFormat()); writer.write(this); writer.flush(); return out.toString(); } catch (IOException e) { throw new RuntimeException("IOException while generating " + "textual representation: " + e.getMessage()); } }
public static String serialize(final Document document) { try (StringWriter out = new StringWriter()) { final OutputFormat format = new OutputFormat(null, false, "UTF-8"); format.setExpandEmptyElements(false); format.setIndent(false); final XMLWriter writer = new XMLWriter(out, format); writer.write(document); writer.flush(); return out.toString(); } catch (final IOException e) { throw new IllegalStateException("Unable to serialize given document to XML", e); } }
public void testRussian() throws Exception { Document doc = getDocument("/xml/russArticle.xml"); assertEquals("encoding not correct", "koi8-r", doc.getXMLEncoding()); Element el = doc.getRootElement(); StringWriter writer = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("koi8-r"); xmlWriter.write(doc); log(writer.toString()); }
public void testPadding2() throws Exception { Document doc = DocumentFactory.getInstance().createDocument(); Element root = doc.addElement("root"); root.addText("prefix"); root.addElement("b"); root.addText("suffix"); OutputFormat format = new OutputFormat("", false); format.setOmitEncoding(true); format.setSuppressDeclaration(true); format.setExpandEmptyElements(true); format.setPadText(true); format.setTrimText(true); StringWriter buffer = new StringWriter(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); System.out.println("xml: " + xml); String expected = "<root>prefix<b></b>suffix</root>"; assertEquals(expected, xml); }
Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp")); Element subelement; subelement = vCard.addElement("N"); subelement.addElement("GIVEN").setText(name.trim()); StringWriter writer = new StringWriter(); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); try { xmlWriter.write(vCard); vcardXML = writer.toString();
Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root"); Element meaning = root.addElement("meaning"); meaning.addText("to li"); meaning.addText("ve"); OutputFormat format = new OutputFormat(); format.setEncoding("UTF-8"); format.setIndentSize(4); format.setNewlines(true); format.setTrimText(true); format.setExpandEmptyElements(true); StringWriter buffer = new StringWriter(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); log(xml); Document doc2 = DocumentHelper.parseText(xml); String text = doc2.valueOf("/root/meaning"); String expected = "to live"; doc2.getRootElement().element("meaning").getText());
public void testNamespaceBug() throws Exception { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root", "ns1"); Element child1 = root.addElement("joe", "ns2"); child1.addElement("zot", "ns1"); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(doc); String text = out.toString(); // System.out.println( "Generated:" + text ); Document doc2 = DocumentHelper.parseText(text); root = doc2.getRootElement(); assertEquals("root has incorrect namespace", "ns1", root .getNamespaceURI()); Element joe = (Element) root.elementIterator().next(); assertEquals("joe has correct namespace", "ns2", joe.getNamespaceURI()); Element zot = (Element) joe.elementIterator().next(); assertEquals("zot has correct namespace", "ns1", zot.getNamespaceURI()); }
public void testXmlWriter() throws Exception { Element elem = DocumentHelper.createElement("elem"); Document doc = DocumentHelper.createDocument(elem); elem.addCDATA(EXPECTED_TEXT); StringWriter sw = new StringWriter(); XMLWriter xWriter = new XMLWriter(sw, OutputFormat.createPrettyPrint()); xWriter.write(doc); xWriter.close(); String xmlString = sw.toString(); doc = DocumentHelper.parseText(xmlString); elem = doc.getRootElement(); assertEquals(EXPECTED_TEXT, elem.getText()); } }
/** * This test was provided by Manfred Lotz * * @throws Exception * DOCUMENT ME! */ public void testWhitespaceBug() throws Exception { String notes = "<notes> This is a multiline\n\rentry</notes>"; Document doc = DocumentHelper.parseText(notes); OutputFormat format = new OutputFormat(); format.setEncoding("UTF-8"); format.setIndentSize(4); format.setNewlines(true); format.setTrimText(true); format.setExpandEmptyElements(true); StringWriter buffer = new StringWriter(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); log(xml); Document doc2 = DocumentHelper.parseText(xml); String text = doc2.valueOf("/notes"); String expected = "This is a multiline entry"; assertEquals("valueOf() returns the correct text padding", expected, text); assertEquals("getText() returns the correct text padding", expected, doc2.getRootElement().getText()); }
public void testWritingEmptyElement() throws Exception { Document doc = DocumentFactory.getInstance().createDocument(); Element grandFather = doc.addElement("grandfather"); Element parent1 = grandFather.addElement("parent"); Element child1 = parent1.addElement("child1"); Element child2 = parent1.addElement("child2"); child2.setText("test"); Element parent2 = grandFather.addElement("parent"); Element child3 = parent2.addElement("child3"); child3.setText("test"); StringWriter buffer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(buffer, format); writer.write(doc); String xml = buffer.toString(); System.out.println(xml); assertTrue("child2 not present", xml.indexOf("<child2>test</child2>") != -1); }
private String renderSchema(Schema schema) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Schema.class); Marshaller marshaller = jaxbContext.createMarshaller(); NamespaceFilter outFilter = new NamespaceFilter(CORE_PREFIX, CORE_NAMESPACE, true); OutputFormat format = new OutputFormat(); format.setIndent(true); format.setNewlines(true); StringWriter sw = new StringWriter(); XMLWriter writer = new XMLWriter(sw, format); outFilter.setContentHandler(writer); marshaller.marshal(schema, outFilter); return sw.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } }
@Override public String buildXml(Report report) { try { Document document = DocumentFactory.getInstance().createDocument(); Element root = document.addElement("report"); root.addAttribute("name", report.getName()); writeTemplates(report, root); writeInputParameters(report, root); writeValueFormats(report, root); writeRootBand(report, root); StringWriter stringWriter = new StringWriter(); new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()).write(document); return stringWriter.toString(); } catch (IOException e) { throw new ReportingException(e); } }
throws java.io.IOException, java.io.UnsupportedEncodingException, org.dom4j.DocumentException { StringWriter sw = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setNewlines(newlines); format.setTrimText(trim); format.setXHTML(isXHTML); format.setExpandEmptyElements(expandEmpty); Document document = DocumentHelper.parseText(html); writer.write(document); writer.flush(); return sw.toString();
/** * This test harness was supplied by Lari Hotari * * @throws Exception * DOCUMENT ME! */ public void testContentHandler() throws Exception { StringWriter out = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("iso-8859-1"); XMLWriter writer = new XMLWriter(out, format); generateXML(writer); writer.close(); String text = out.toString(); if (VERBOSE) { log("Created XML"); log(text); } // now lets parse the output and test it with XPath Document doc = DocumentHelper.parseText(text); String value = doc.valueOf("/processes[@name='arvojoo']"); assertEquals("Document contains the correct text", "jeejee", value); }