Java Code Examples for org.apache.xml.serialize.XMLSerializer#serialize()
The following examples show how to use
org.apache.xml.serialize.XMLSerializer#serialize() .
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: AtsProjectConfiguration.java From ats-framework with Apache License 2.0 | 6 votes |
public void save() throws AtsConfigurationException { // save the XML file try { OutputFormat format = new OutputFormat(doc); format.setIndenting(true); format.setIndent(4); format.setLineWidth(1000); XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(atsConfigurationFile)), format); serializer.serialize(doc); } catch (Exception e) { throw new AtsConfigurationException("Error saving ATS configuration in '" + atsConfigurationFile + "'", e); } }
Example 2
Source File: JSONTemplateUtilities.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
public static String convertJsonToXML(JSONObject json) throws ParserConfigurationException, JSONException, IOException { String xml; DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuildFactory.newDocumentBuilder(); Document dom = docBuilder.newDocument(); Element xmlDom = extractXmlFromJson(json, dom, null); StringWriter outputStream = new StringWriter(); OutputFormat outputFormat = new OutputFormat(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndenting(true); XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat); serializer.serialize(xmlDom); xml = outputStream.toString(); return xml; }
Example 3
Source File: B2BParserHelper.java From kfs with GNU Affero General Public License v3.0 | 6 votes |
private byte[] addXMLNameSpace(Document xmlDoc, String nameSpace){ Node node = xmlDoc.getDocumentElement(); Element element = (Element)node; element.setAttribute("xmlns", nameSpace); OutputFormat outputFormat = new OutputFormat(xmlDoc); outputFormat.setOmitDocumentType(true); ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer( out,outputFormat ); try { serializer.asDOMSerializer(); serializer.serialize( xmlDoc.getDocumentElement()); } catch (IOException e) { throw new RuntimeException(e); } return out.toByteArray(); }
Example 4
Source File: XMLUtils.java From jdal with Apache License 2.0 | 6 votes |
/** * Serialize a Document to a String using JAXP with identation (4 spaces) * @param doc to serialize * @return xml string */ public static String prettyDocumentToString(Document doc) { StringWriter writer = new StringWriter(); OutputFormat out = new OutputFormat(); out.setOmitXMLDeclaration(true); out.setIndenting(true); out.setIndent(4); out.setLineSeparator(System.getProperty("line.separator")); out.setLineWidth(Integer.MAX_VALUE); XMLSerializer serializer = new XMLSerializer(writer, out); try { Element rootElement = doc.getDocumentElement(); serializer.serialize(rootElement); } catch (IOException e) { log.error(e); } return writer.toString(); }
Example 5
Source File: PolicyEditorService.java From carbon-identity-framework with Apache License 2.0 | 5 votes |
/** * Formats a given unformatted XML string * * @param xml * @return A CDATA wrapped, formatted XML String */ public String formatXML(String xml) { try { DocumentBuilder docBuilder; Document xmlDoc; // create the factory DocumentBuilderFactory docFactory = IdentityUtil.getSecuredDocumentBuilderFactory(); docFactory.setIgnoringComments(true); // now use the factory to create the document builder docBuilder = docFactory.newDocumentBuilder(); xmlDoc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8))); OutputFormat format = new OutputFormat(xmlDoc); format.setLineWidth(0); format.setIndenting(true); format.setIndent(2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(baos, format); serializer.serialize(xmlDoc); xml = baos.toString("UTF-8"); } catch (ParserConfigurationException pce) { throw new IllegalArgumentException("Failed to parse the unformatted XML String. ", pce); } catch (Exception e) { log.error("Error occured while formtting the unformatted XML String. ", e); } return "<![CDATA[" + xml + "]]>"; }
Example 6
Source File: XMLHelpers.java From SAMLRaider with MIT License | 5 votes |
public String getString(Document document, boolean indenting, int indent) throws IOException{ OutputFormat format = new OutputFormat(document); format.setLineWidth(200); format.setIndenting(indenting); format.setIndent(indent); format.setPreserveEmptyAttributes(true); format.setEncoding("UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(baos, format); serializer.asDOMSerializer(); serializer.serialize(document); return baos.toString("UTF-8"); }
Example 7
Source File: XPathReplacer.java From maven-replacer-plugin with MIT License | 5 votes |
private String writeXml(Document doc) throws Exception { OutputFormat of = new OutputFormat(doc); of.setPreserveSpace(true); of.setEncoding(doc.getXmlEncoding()); StringWriter sw = new StringWriter(); XMLSerializer serializer = new XMLSerializer(sw, of); serializer.serialize(doc); return sw.toString(); }
Example 8
Source File: DocumentUtils.java From cs-actions with Apache License 2.0 | 5 votes |
/** * Returns a String representation for the XML Document. * * @param xmlDocument the XML Document * @return String representation for the XML Document * @throws IOException */ public static String documentToString(Document xmlDocument) throws IOException { String encoding = (xmlDocument.getXmlEncoding() == null) ? "UTF-8" : xmlDocument.getXmlEncoding(); OutputFormat format = new OutputFormat(xmlDocument); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); format.setEncoding(encoding); try (Writer out = new StringWriter()) { XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(xmlDocument); return out.toString(); } }
Example 9
Source File: ElementIdGenerator.java From xcurator with Apache License 2.0 | 5 votes |
public byte[] asByteArray(Element element) throws IOException { ByteArrayOutputStream bis = new ByteArrayOutputStream(); OutputFormat format = new OutputFormat(element.getOwnerDocument()); XMLSerializer serializer = new XMLSerializer( bis, format); serializer.asDOMSerializer(); serializer.serialize(element); return bis.toByteArray(); }
Example 10
Source File: XMLUtils.java From xcurator with Apache License 2.0 | 5 votes |
public static byte[] asByteArray(Element element) throws IOException { ByteArrayOutputStream bis = new ByteArrayOutputStream(); OutputFormat format = new OutputFormat(element.getOwnerDocument()); XMLSerializer serializer = new XMLSerializer( bis, format); serializer.asDOMSerializer(); serializer.serialize(element); return bis.toByteArray(); }
Example 11
Source File: DebugParseFilter.java From storm-crawler with Apache License 2.0 | 5 votes |
@Override public void filter(String URL, byte[] content, DocumentFragment doc, ParseResult parse) { try { XMLSerializer serializer = new XMLSerializer(os, null); serializer.serialize(doc); os.flush(); } catch (IOException e) { e.printStackTrace(); } }
Example 12
Source File: XMLUtils.java From jdal with Apache License 2.0 | 5 votes |
/** * Element to String without format * * @param elto to serialize * @return serialized elto */ public static String elementToString(Element elto) { StringWriter writer = new StringWriter(); OutputFormat of = new OutputFormat(); of.setOmitXMLDeclaration(true); XMLSerializer serializer = new XMLSerializer(writer, of); serializer.setNamespaces(true); try { serializer.serialize(elto); } catch (IOException ioe) { log.error(ioe); } return writer.toString(); }
Example 13
Source File: ProcessFile.java From pdfxtk with Apache License 2.0 | 5 votes |
public static void serializeXML (org.w3c.dom.Document resultDocument, Writer output) throws IOException { // The third parameter in the constructor method for // _OutputFormat_ controls whether indenting should be // used. Unfortunately, I have found some bugs in the // indenting implementation that have corrupted the text // so I have switched it off. OutputFormat myOutputFormat = new OutputFormat(resultDocument, "UTF-8", true); // output used to be replaced with System.out XMLSerializer s = new XMLSerializer(output, myOutputFormat); try { s.serialize(resultDocument); // next line added by THA 21.03.05 output.flush(); } catch (IOException e) { System.err.println("Couldn't serialize document: "+ e.getMessage()); throw e; } // end of addition }
Example 14
Source File: PolicyEditorService.java From carbon-identity with Apache License 2.0 | 4 votes |
/** * Formats a given unformatted XML string * * @param xml * @return A CDATA wrapped, formatted XML String */ public String formatXML(String xml) { try { // create the factory DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setIgnoringComments(true); docFactory.setNamespaceAware(true); docFactory.setExpandEntityReferences(false); SecurityManager securityManager = new SecurityManager(); securityManager.setEntityExpansionLimit(ENTITY_EXPANSION_LIMIT); docFactory.setAttribute(SECURITY_MANAGER_PROPERTY, securityManager); DocumentBuilder docBuilder; Document xmlDoc; // now use the factory to create the document builder docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); docFactory.setFeature(EXTERNAL_GENERAL_ENTITIES_URI, false); docBuilder = docFactory.newDocumentBuilder(); docBuilder.setEntityResolver(new CarbonEntityResolver()); xmlDoc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8))); OutputFormat format = new OutputFormat(xmlDoc); format.setLineWidth(0); format.setIndenting(true); format.setIndent(2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(baos, format); serializer.serialize(xmlDoc); xml = baos.toString("UTF-8"); } catch (ParserConfigurationException pce) { throw new IllegalArgumentException("Failed to parse the unformatted XML String. ", pce); } catch (Exception e) { log.error("Error occured while formtting the unformatted XML String. ", e); } return "<![CDATA[" + xml + "]]>"; }
Example 15
Source File: WebServiceInterface.java From quetzal with Eclipse Public License 2.0 | 4 votes |
default void printDocument(Document doc) throws Exception { OutputFormat format = new OutputFormat(doc); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(System.out, format); serializer.serialize(doc); }
Example 16
Source File: Xerces.java From pdfxtk with Apache License 2.0 | 4 votes |
public void writeDocument(Document document, Writer writer) throws IOException { XMLSerializer s = new XMLSerializer(writer, new OutputFormat()); s.serialize(document); }