Java Utililty Methods XML Node Save

List of utility methods to do XML Node Save

Description

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

Method

voidwriteNode(PrintWriter w, Node node, int depth)
write Node
short nodeType = node.getNodeType();
switch (nodeType) {
case Node.ELEMENT_NODE:
    writeElement(w, (Element) node, depth);
    break;
case Node.ATTRIBUTE_NODE:
    writeAttribute(w, (Attr) node, depth);
    break;
...
voidwriteToStream(OutputStream os, Node node)
write To Stream
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer serializer = domImpl.createLSSerializer();
LSOutput lso = domImpl.createLSOutput();
lso.setByteStream(os);
lso.setEncoding("UTF-8");
serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
serializer.write(node, lso);
...
StringwriteToString(Node node)
write To String
try {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    return writer.writeToString(node);
} catch (Exception e) {
    throw new RuntimeException(e);
StringwriteToString(Node node)
Returns a string representation of the given node.
ByteArrayOutputStream os = new ByteArrayOutputStream();
writeDocument(os, node);
return os.toString();
byte[]writeXml(final Node node)
Escribe un XML como texto.
final DOMImplementationLS domImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
final LSSerializer lsSerializer = domImpl.createLSSerializer();
final DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
if (domConfiguration.canSetParameter("namespaces", Boolean.FALSE)) { 
    domConfiguration.setParameter("namespaces", Boolean.FALSE); 
if (domConfiguration.canSetParameter("canonical-form", Boolean.TRUE)) { 
    lsSerializer.getDomConfig().setParameter("canonical-form", Boolean.TRUE); 
...
voidwriteXMLwalkTree(Node node, int indent, PrintWriter out)
write XM Lwalk Tree
if (node == null)
    throw new NullPointerException("Null node passed to writeXMLwalkTree()");
if (node.hasChildNodes()) {
    if (node instanceof Element) {
        Element elem = (Element) node;
        out.print("\n");
        for (int j = 0; j < indent; j++) {
            out.print(" ");
...
voidwriteXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
write XM Lwith XALAN
final LSSerializer serializer = ((DOMImplementationLS) node.getOwnerDocument().getImplementation())
        .createLSSerializer();
serializer.getDomConfig().setParameter("namespaces", Boolean.FALSE); 
final com.sun.org.apache.xerces.internal.dom.DOMOutputImpl output = new com.sun.org.apache.xerces.internal.dom.DOMOutputImpl();
output.setCharacterStream(writer);
if (xmlEncoding != null) {
    output.setEncoding(xmlEncoding);
serializer.write(node, output);
voidwriteXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
write XM Lwith XALAN
DOMImplementationLS domImplLS;
final Document doc = node.getOwnerDocument();
if (doc.getFeature("Core", "3.0") != null && doc.getFeature("LS", "3.0") != null) { 
    domImplLS = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0"); 
} else {
    throw new RuntimeException("La implementacion DOM cargada no permite la serializacion"); 
final LSOutput output = domImplLS.createLSOutput();
...
voidxmlSelectNodes0(List list, Node node, String path[], int n)
xml Select Nodes
if (n == path.length) {
    list.add(node);
    return;
if (path[n].equals("..")) {
    xmlSelectNodes0(list, node.getParentNode(), path, n + 1);
    return;
NodeList kids = node.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
    Node kid = kids.item(i);
    if (kid.getNodeName().equals(path[n]))
        xmlSelectNodes0(list, kid, path, n + 1);
Stringxmltostring(String indent, Node node)
xmltostring
if (indent == null)
    indent = "";
String str = "";
short type = node.getNodeType();
str += indent + " <" + node.getNodeName() + "(#" + type + ")" + "='" + node.getNodeValue() + "'>\n";
NamedNodeMap attrs = node.getAttributes();
if (attrs != null)
    for (int i = 0; i < attrs.getLength(); i++) {
...