Java Utililty Methods XML Node to String

List of utility methods to do XML Node to String

  1. HOME
  2. Java
  3. X
  4. XML Node to String

Description

The list of methods to do XML Node to String are organized into topic(s).

Method

String asString(Element elt)
as String
TransformerFactory transfac = TransformerFactory.newInstance();
try {
 Transformer trans = transfac.newTransformer();
 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 trans.setOutputProperty(OutputKeys.INDENT, "yes");
 trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
 StringWriter sw = new StringWriter();
 StreamResult result = new StreamResult(sw);
...
String asString(Node node)
as String
StringWriter writer = new StringWriter();
try {
 Transformer trans = TransformerFactory.newInstance().newTransformer();
 trans.setOutputProperty(OutputKeys.INDENT, "no");
 trans.setOutputProperty(OutputKeys.VERSION, "1.0");
 if (!(node instanceof Document)) {
 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 trans.transform(new DOMSource(node), new StreamResult(writer));
} catch (final TransformerConfigurationException ex) {
 throw new IllegalStateException(ex);
} catch (final TransformerException ex) {
 throw new IllegalArgumentException(ex);
return writer.toString();
String asString(Node node)
Format and return the string representation of the specified node and it's children
TransformerFactory factory = TransformerFactory.newInstance();
try {
 factory.setAttribute("indent-number", 4);
} catch (Throwable t) {
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
String asString(Node node)
Creates a String out of a Node
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
String asString(XMLStreamReader xmlr)
as String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new StAXSource(xmlr), new StreamResult(stringWriter));
return stringWriter.toString();
String asXML(Node node)
A helper method useful for debugging and logging which will convert the given DOM node into XML text
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter buffer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(buffer));
return buffer.toString();
String asXmlString(Node node)
as Xml String
final Source source = new DOMSource(node);
final StringWriter stringWriter = new StringWriter();
final Result result = new StreamResult(stringWriter);
final TransformerFactory factory = TransformerFactory.newInstance();
try {
 final Transformer transformer = factory.newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
...
String convertToString(Node node)
convert To String
return convertDocumentToString(convertToDocument(node));
String convertToString(Node node)
convert To String
boolean withXMLDeclaration = true;
String result;
if (withXMLDeclaration) {
 Document document = node.getOwnerDocument();
 DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
 LSSerializer serializer = domImplLS.createLSSerializer();
 result = serializer.writeToString(node);
} else {
...
String createXmlString(Node node)
create Xml String
Transformer transformer;
try {
 transformer = TransformerFactory.newInstance().newTransformer(); 
} catch (TransformerConfigurationException e) {
 throw new RuntimeException(e); 
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter xml = new StringWriter();
...


AltStyle によって変換されたページ (->オリジナル) /