Example usage for org.w3c.dom Document getXmlStandalone

List of usage examples for org.w3c.dom Document getXmlStandalone

Introduction

In this page you can find the example usage for org.w3c.dom Document getXmlStandalone.

Prototype

public boolean getXmlStandalone();

Source Link

Document

An attribute specifying, as part of the <a href='http://www.w3.org/TR/2004/REC-xml-20040204#NT-XMLDecl'>XML declaration</a>, whether this document is standalone.

Usage

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    System.out.println(xmlDoc.getXmlStandalone());

}

From source file:Main.java

/**
 * @param node// w w w  .  j  ava 2 s  . co m
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:org.openengsb.openengsbplugin.tools.Tools.java

public static String serializeXML(Document doc) throws IOException {
    StringWriter sw = null;//from w w w. ja  v  a 2 s  .c o  m
    try {
        sw = new StringWriter();
        XMLSerializer xmlSerializer = new XMLSerializer();
        OutputFormat of = new OutputFormat(doc, doc.getXmlEncoding(), false);
        of.setStandalone(doc.getXmlStandalone());
        xmlSerializer.setOutputFormat(of);
        xmlSerializer.setOutputCharStream(sw);
        xmlSerializer.serialize(doc);
        return sw.toString();
    } finally {
        IOUtils.closeQuietly(sw);
    }
}