Example usage for javax.xml.transform OutputKeys OMIT_XML_DECLARATION

List of usage examples for javax.xml.transform OutputKeys OMIT_XML_DECLARATION

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys OMIT_XML_DECLARATION.

Prototype

String OMIT_XML_DECLARATION

To view the source code for javax.xml.transform OutputKeys OMIT_XML_DECLARATION.

Click Source Link

Document

omit-xml-declaration = "yes" | "no".

Usage

From source file:com.enonic.esl.xml.XMLTool.java

private static void serialize(Result result, Node node, int indent, boolean omitDecl) {
    try {/*from  w w  w. j  av  a  2  s .c om*/
        final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, indent > 0 ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDecl ? "yes" : "no");
        transformer.transform(new DOMSource(node), result);
    } catch (Exception e) {
        throw new XMLToolException(e);
    }
}

From source file:eu.eidas.auth.engine.core.stork.StorkProtocolProcessor.java

private String extractEDocValue(final XSAnyImpl xmlString) {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//  www .j a va 2s . com
        transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    } catch (TransformerConfigurationException e) {
        LOG.warn(AbstractProtocolEngine.SAML_EXCHANGE, "Error transformer configuration exception",
                e.getMessage());
        LOG.debug(AbstractProtocolEngine.SAML_EXCHANGE, "Error transformer configuration exception", e);
    }
    StringWriter buffer = new StringWriter();
    try {
        if (transformer != null && xmlString != null && xmlString.getUnknownXMLObjects() != null
                && !xmlString.getUnknownXMLObjects().isEmpty()) {
            transformer.transform(new DOMSource(xmlString.getUnknownXMLObjects().get(0).getDOM()),
                    new StreamResult(buffer));
        }
    } catch (TransformerException e) {
        LOG.info(AbstractProtocolEngine.SAML_EXCHANGE, "BUSINESS EXCEPTION : Error transformer exception",
                e.getMessage());
        LOG.debug(AbstractProtocolEngine.SAML_EXCHANGE, "BUSINESS EXCEPTION : Error transformer exception", e);
    }
    return buffer.toString();
}

From source file:io.kahu.hawaii.util.logger.DefaultLogManager.java

@Override
public String prettyPrintXml(final String s) {
    try {/*from  www.j av  a2 s  .co m*/
        if (s == null) {
            return null;
        }
        if (!looksLikeXml(s)) {
            return s;
        }
        Source xmlInput = new StreamSource(new StringReader(s));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable t) {
        return s;
    }
}

From source file:com.diversityarrays.dalclient.DalUtil.java

public static void writeXmlResult(String xml, Writer w) throws IOException, TransformerException {
    StreamSource source = new StreamSource(new StringReader(xml));

    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF_8);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$

    transformer.transform(source, new StreamResult(w));
}

From source file:vmTools.java

public String createVmXml(String jsonString, String server) {
    String ret = "";

    ArrayList<String> ide = new ArrayList<String>();
    ide.add("hda");
    ide.add("hdb");
    ide.add("hdc");
    ide.add("hdd");
    ide.add("hde");
    ide.add("hdf");
    ArrayList<String> scsi = new ArrayList<String>();
    scsi.add("sda");
    scsi.add("sdb");
    scsi.add("sdc");
    scsi.add("sdd");
    scsi.add("sde");
    scsi.add("sdf");

    try {/*from   www  .ja va2s  . c  o m*/
        JSONObject jo = new JSONObject(jsonString);
        // A JSONArray is an ordered sequence of values. Its external form is a 
        // string wrapped in square brackets with commas between the values.
        // Get the JSONObject value associated with the search result key.
        String varName = jo.get("name").toString();

        String domainType = jo.get("domain_type").toString();
        String varMem = jo.get("memory").toString();
        String varCpu = jo.get("vcpu").toString();
        String varArch = jo.get("arch").toString();

        // Get the JSONArray value associated with the Result key
        JSONArray diskArray = jo.getJSONArray("diskList");
        JSONArray nicArray = jo.getJSONArray("nicList");

        // Cration d'un nouveau DOM
        DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
        DocumentBuilder constructeur = fabrique.newDocumentBuilder();
        Document document = constructeur.newDocument();

        // Proprits du DOM
        document.setXmlStandalone(true);

        // Cration de l'arborescence du DOM
        Element domain = document.createElement("domain");
        document.appendChild(domain);
        domain.setAttribute("type", domainType);

        //racine.appendChild(document.createComment("Commentaire sous la racine"));
        Element name = document.createElement("name");
        domain.appendChild(name);
        name.setTextContent(varName);

        UUID varuid = UUID.randomUUID();
        Element uid = document.createElement("uuid");
        domain.appendChild(uid);
        uid.setTextContent(varuid.toString());

        Element memory = document.createElement("memory");
        domain.appendChild(memory);
        memory.setTextContent(varMem);

        Element currentMemory = document.createElement("currentMemory");
        domain.appendChild(currentMemory);
        currentMemory.setTextContent(varMem);

        Element vcpu = document.createElement("vcpu");
        domain.appendChild(vcpu);
        vcpu.setTextContent(varCpu);
        //<os>
        Element os = document.createElement("os");
        domain.appendChild(os);
        Element type = document.createElement("type");
        os.appendChild(type);
        type.setAttribute("arch", varArch);
        type.setAttribute("machine", jo.get("machine").toString());
        type.setTextContent(jo.get("machine_type").toString());

        JSONArray bootArray = jo.getJSONArray("bootList");
        int count = bootArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject bootDev = bootArray.getJSONObject(i);
            Element boot = document.createElement("boot");
            os.appendChild(boot);
            boot.setAttribute("dev", bootDev.get("dev").toString());
        }

        Element bootmenu = document.createElement("bootmenu");
        os.appendChild(bootmenu);
        bootmenu.setAttribute("enable", jo.get("bootMenu").toString());
        //</os>
        //<features>
        Element features = document.createElement("features");
        domain.appendChild(features);
        JSONArray featureArray = jo.getJSONArray("features");
        int featureCount = featureArray.length();
        for (int i = 0; i < featureCount; i++) {
            JSONObject jasonFeature = featureArray.getJSONObject(i);
            String newFeature = jasonFeature.get("opt").toString();
            Element elFeature = document.createElement(newFeature);
            features.appendChild(elFeature);
        }
        //</features>
        Element clock = document.createElement("clock");
        domain.appendChild(clock);
        // Clock settings
        clock.setAttribute("offset", jo.get("clock_offset").toString());
        JSONArray timerArray = jo.getJSONArray("timers");
        for (int i = 0; i < timerArray.length(); i++) {
            JSONObject jsonTimer = timerArray.getJSONObject(i);
            Element elTimer = document.createElement("timer");
            clock.appendChild(elTimer);
            elTimer.setAttribute("name", jsonTimer.get("name").toString());
            elTimer.setAttribute("present", jsonTimer.get("present").toString());
            elTimer.setAttribute("tickpolicy", jsonTimer.get("tickpolicy").toString());
        }

        Element poweroff = document.createElement("on_poweroff");
        domain.appendChild(poweroff);
        poweroff.setTextContent(jo.get("on_poweroff").toString());
        Element reboot = document.createElement("on_reboot");
        domain.appendChild(reboot);
        reboot.setTextContent(jo.get("on_reboot").toString());
        Element crash = document.createElement("on_crash");
        domain.appendChild(crash);
        crash.setTextContent(jo.get("on_crash").toString());
        //<devices>
        Element devices = document.createElement("devices");
        domain.appendChild(devices);
        String varEmulator = jo.get("emulator").toString();
        Element emulator = document.createElement("emulator");
        devices.appendChild(emulator);
        emulator.setTextContent(varEmulator);

        int resultCount = diskArray.length();
        for (int i = 0; i < resultCount; i++) {
            Element disk = document.createElement("disk");
            devices.appendChild(disk);
            JSONObject newDisk = diskArray.getJSONObject(i);
            String diskType = newDisk.get("type").toString();
            Element driver = document.createElement("driver");
            Element target = document.createElement("target");
            disk.appendChild(driver);
            disk.appendChild(target);
            if (diskType.equals("file")) {
                Element source = document.createElement("source");
                disk.appendChild(source);
                source.setAttribute("file", newDisk.get("source").toString());
                driver.setAttribute("cache", "none");
            }

            disk.setAttribute("type", diskType);
            disk.setAttribute("device", newDisk.get("device").toString());
            driver.setAttribute("type", newDisk.get("format").toString());
            driver.setAttribute("name", newDisk.get("driver").toString());

            //String diskDev = ide.get(0);
            //ide.remove(0);
            String diskDev = newDisk.get("bus").toString();
            String diskBus = "";
            if (diskDev.indexOf("hd") > -1) {
                diskBus = "ide";
            } else if (diskDev.indexOf("sd") > -1) {
                diskBus = "scsi";
            } else if (diskDev.indexOf("vd") > -1) {
                diskBus = "virtio";
            }

            target.setAttribute("dev", diskDev);
            target.setAttribute("bus", diskBus);

        }

        resultCount = nicArray.length();
        for (int i = 0; i < resultCount; i++) {
            JSONObject newNic = nicArray.getJSONObject(i);
            String macaddr = newNic.get("mac").toString().toLowerCase();
            if (macaddr.indexOf("automatic") > -1) {
                Random rand = new Random();
                macaddr = "52:54:00";
                String hexa = Integer.toHexString(rand.nextInt(255));
                macaddr += ":" + hexa;
                hexa = Integer.toHexString(rand.nextInt(255));
                macaddr += ":" + hexa;
                hexa = Integer.toHexString(rand.nextInt(255));
                macaddr += ":" + hexa;
            }

            Element netIf = document.createElement("interface");
            devices.appendChild(netIf);
            Element netSource = document.createElement("source");
            Element netDevice = document.createElement("model");
            Element netMac = document.createElement("mac");
            netIf.appendChild(netSource);
            netIf.appendChild(netDevice);
            netIf.appendChild(netMac);
            netIf.setAttribute("type", "network");
            netSource.setAttribute("network", newNic.get("bridge").toString());
            String portgroup = newNic.get("portgroup").toString();
            if (!portgroup.equals("")) {
                netSource.setAttribute("portgroup", portgroup);
            }
            netDevice.setAttribute("type", newNic.get("device").toString());
            netMac.setAttribute("address", macaddr);
        }

        JSONArray serialArray = jo.getJSONArray("serial");
        count = serialArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject serialDev = serialArray.getJSONObject(i);
            Element serial = document.createElement("serial");
            devices.appendChild(serial);
            serial.setAttribute("type", serialDev.get("type").toString());
            Element target = document.createElement("target");
            serial.appendChild(target);
            target.setAttribute("port", serialDev.get("port").toString());
        }

        JSONArray consoleArray = jo.getJSONArray("console");
        count = consoleArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject consoleDev = consoleArray.getJSONObject(i);
            Element console = document.createElement("console");
            devices.appendChild(console);
            console.setAttribute("type", "pty");
            Element target = document.createElement("target");
            console.appendChild(target);
            target.setAttribute("port", consoleDev.get("port").toString());
            target.setAttribute("type", consoleDev.get("type").toString());
        }

        JSONArray inputArray = jo.getJSONArray("input");
        count = inputArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject inputDev = inputArray.getJSONObject(i);
            Element input = document.createElement("input");
            devices.appendChild(input);
            input.setAttribute("type", inputDev.get("type").toString());
            input.setAttribute("bus", inputDev.get("bus").toString());
        }

        JSONArray graphicsArray = jo.getJSONArray("graphics");
        count = graphicsArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject graphicsDev = graphicsArray.getJSONObject(i);
            Element graphics = document.createElement("graphics");
            devices.appendChild(graphics);
            graphics.setAttribute("type", graphicsDev.get("type").toString());
            graphics.setAttribute("port", graphicsDev.get("port").toString());
            graphics.setAttribute("autoport", graphicsDev.get("autoport").toString());
            graphics.setAttribute("listen", graphicsDev.get("listen").toString());
            graphics.setAttribute("keymap", graphicsDev.get("keymap").toString());
        }

        JSONArray soundArray = jo.getJSONArray("sound");
        count = soundArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject soundDev = soundArray.getJSONObject(i);
            Element sound = document.createElement("sound");
            devices.appendChild(sound);
            sound.setAttribute("model", soundDev.get("model").toString());
        }
        //sound.setAttribute("model", "ac97");

        JSONArray videoArray = jo.getJSONArray("video");
        count = videoArray.length();
        for (int i = 0; i < count; i++) {
            JSONObject videoDev = videoArray.getJSONObject(i);
            Element video = document.createElement("video");
            devices.appendChild(video);
            Element model = document.createElement("model");
            video.appendChild(model);
            model.setAttribute("model", videoDev.get("type").toString());
            model.setAttribute("model", videoDev.get("vram").toString());
            model.setAttribute("model", videoDev.get("heads").toString());
        }

        //write the content into xml file
        this.makeRelativeDirs("/" + server + "/vm/configs/" + varName);
        String pathToXml = RuntimeAccess.getInstance().getSession().getServletContext()
                .getRealPath("resources/data/" + server + "/vm/configs/" + varName + "/" + varName + ".xml");
        File xmlOutput = new File(pathToXml);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(xmlOutput);
        transformer.transform(source, result);

        StringWriter stw = new StringWriter();
        transformer.transform(source, new StreamResult(stw));
        ret = stw.toString();

    } catch (Exception e) {
        log(ERROR, "create xml file has failed", e);
        return e.toString();
    }
    return ret;
}

From source file:com.diversityarrays.dalclient.DalUtil.java

public static void printXmlDocument(Document doc, Writer w) throws IOException, TransformerException {

    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF_8);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$

    transformer.transform(new DOMSource(doc), new StreamResult(w));
}

From source file:elh.eus.absa.CorpusReader.java

/**
 * print annotations in Semeval-absa 2015 format
 *
 * @param savePath string : path for the file to save the data 
 * @throws ParserConfigurationException/*from  w  ww  .  java 2 s  .  c om*/
 */
public void print2Semeval2015format(String savePath) throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    org.w3c.dom.Document doc = docBuilder.newDocument();
    org.w3c.dom.Element rootElement = doc.createElement("Reviews");
    doc.appendChild(rootElement);

    for (String rev : getReviews().keySet()) {
        // review elements
        org.w3c.dom.Element review = doc.createElement("Review");
        rootElement.appendChild(review);

        // set id attribute to sentence element
        review.setAttribute("rid", rev);

        // Sentences element
        org.w3c.dom.Element sentences = doc.createElement("sentences");
        review.appendChild(sentences);

        List<String> processed = new ArrayList<String>();

        for (String sent : this.revSents.get(rev)) {
            if (processed.contains(sent)) {
                continue;
            } else {
                processed.add(sent);
            }
            //System.err.println("creating elements for sentence "+sent);

            // sentence elements
            org.w3c.dom.Element sentence = doc.createElement("sentence");
            sentences.appendChild(sentence);

            // set attribute to sentence element               
            sentence.setAttribute("id", sent);

            // text element of the current sentence
            org.w3c.dom.Element text = doc.createElement("text");
            sentence.appendChild(text);
            text.setTextContent(getSentences().get(sent));

            // Opinions element
            org.w3c.dom.Element opinions = doc.createElement("Opinions");
            sentence.appendChild(opinions);

            for (Opinion op : getSentenceOpinions(sent)) {
                if (op.getCategory().equalsIgnoreCase("NULL")) {
                    continue;
                }
                // opinion elements
                org.w3c.dom.Element opinion = doc.createElement("Opinion");
                opinions.appendChild(opinion);

                // set attributes to the opinion element               
                opinion.setAttribute("target", op.getTarget());
                opinion.setAttribute("category", op.getCategory());
                opinion.setAttribute("polarity", op.getPolarity());
                opinion.setAttribute("from", op.getFrom().toString());
                opinion.setAttribute("to", op.getTo().toString());
            }
        }
    }

    // write the content into xml file
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(savePath));

        // Output to console for testing
        //StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.err.println("File saved to run.xml");

    } catch (TransformerException e) {
        System.err.println("CorpusReader: error when trying to print generated xml result file.");
        e.printStackTrace();
    }
}

From source file:de.huberlin.wbi.hiway.am.galaxy.GalaxyApplicationMaster.java

/**
 * A (recursive) helper function for parsing the macros used by the XML files describing Galaxy's tools
 * //from w ww .j  a  v a2 s .c o m
 * @param macrosNd
 *            an XML node that specifies a set of macros
 * @param dir
 *            the directory in which the currently processed macros are located
 * @return processed macros accessible by their name
 */
private Map<String, String> processMacros(Node macrosNd, String dir) {
    Map<String, String> macrosByName = new HashMap<>();
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Element macrosEl = (Element) macrosNd;

        // (1) if additional macro files are to be imported, open the files and recursively invoke this method
        NodeList importNds = macrosEl.getElementsByTagName("import");
        for (int j = 0; j < importNds.getLength(); j++) {
            Element importEl = (Element) importNds.item(j);
            String importFileName = importEl.getChildNodes().item(0).getNodeValue().trim();
            File file = new File(dir, importFileName);
            Document doc = builder.parse(file);
            macrosByName.putAll(processMacros(doc.getDocumentElement(), dir));
        }

        // (2) parse all macros in this set
        NodeList macroNds = macrosEl.getElementsByTagName("macro");
        for (int j = 0; j < macroNds.getLength(); j++) {
            Element macroEl = (Element) macroNds.item(j);
            String name = macroEl.getAttribute("name");

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(macroEl);
            transformer.transform(source, result);
            String macro = result.getWriter().toString();
            macro = macro.substring(macro.indexOf('\n') + 1, macro.lastIndexOf('\n'));
            macrosByName.put(name, macro);
        }
    } catch (SAXException | IOException | TransformerException | ParserConfigurationException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return macrosByName;
}

From source file:eu.stork.peps.auth.engine.STORKSAMLEngine.java

/**
 * Generate personal attribute list.//from w  ww.ja  v a2s .  co m
 * 
 * @param assertion the assertion
 * 
 * @return the personal attribute list
 * 
 * @throws STORKSAMLEngineException the STORKSAML engine exception
 */
private IPersonalAttributeList generatePersonalAttributeList(final Assertion assertion)
        throws STORKSAMLEngineException {
    LOG.debug("Generate personal attribute list from XMLObject.");
    final List<XMLObject> listExtensions = assertion.getOrderedChildren();

    boolean find = false;
    AttributeStatement requestedAttr = null;

    // Search the attribute statement.
    for (int i = 0; i < listExtensions.size() && !find; i++) {
        final XMLObject xml = listExtensions.get(i);
        if (xml instanceof AttributeStatement) {
            requestedAttr = (AttributeStatement) xml;
            find = true;
        }
    }

    if (!find) {
        LOG.error("Error: AttributeStatement it's not present.");
        throw new STORKSAMLEngineException("AttributeStatement it's not present.");
    }

    final List<Attribute> reqAttrs = requestedAttr.getAttributes();

    final IPersonalAttributeList personalAttrList = new PersonalAttributeList();
    String attributeName;

    // Process the attributes.
    for (int nextAttribute = 0; nextAttribute < reqAttrs.size(); nextAttribute++) {
        final Attribute attribute = reqAttrs.get(nextAttribute);

        final PersonalAttribute personalAttribute = new PersonalAttribute();

        attributeName = attribute.getName();
        personalAttribute.setName(attributeName.substring(attributeName.lastIndexOf('/') + 1));

        personalAttribute
                .setStatus(attribute.getUnknownAttributes().get(new QName(SAMLCore.STORK10_NS.getValue(),
                        "AttributeStatus", SAMLCore.STORK10_PREFIX.getValue())));

        final ArrayList<String> simpleValues = new ArrayList<String>();
        final HashMap<String, String> multiValues = new HashMap<String, String>();

        final List<XMLObject> values = attribute.getOrderedChildren();

        // Process the values.
        for (int nextValue = 0; nextValue < values.size(); nextValue++) {

            final XMLObject xmlObject = values.get(nextValue);

            if (xmlObject instanceof XSStringImpl) {

                simpleValues.add(((XSStringImpl) xmlObject).getValue());

            } else if (xmlObject instanceof XSAnyImpl) {

                if (attributeName.equals("http://www.stork.gov.eu/1.0/signedDoc")) {

                    final XSAnyImpl xmlString = (XSAnyImpl) values.get(nextValue);

                    TransformerFactory transFactory = TransformerFactory.newInstance();
                    Transformer transformer = null;
                    try {
                        transformer = transFactory.newTransformer();
                        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                    } catch (TransformerConfigurationException e) {
                        LOG.error("Error transformer configuration exception", e);
                    }
                    StringWriter buffer = new StringWriter();
                    try {
                        if (xmlString != null && xmlString.getUnknownXMLObjects() != null
                                && xmlString.getUnknownXMLObjects().size() > 0) {
                            transformer.transform(
                                    new DOMSource(xmlString.getUnknownXMLObjects().get(0).getDOM()),
                                    new StreamResult(buffer));
                        }
                    } catch (TransformerException e) {
                        LOG.error("Error transformer exception", e);
                    }
                    String str = buffer.toString();

                    simpleValues.add(str);

                } else if (isComplex(xmlObject)) {
                    LOG.info(attributeName + " found");
                    // Process complex value.
                    final XSAnyImpl complexValue = (XSAnyImpl) xmlObject;

                    for (int nextComplexValue = 0; nextComplexValue < complexValue.getUnknownXMLObjects()
                            .size(); nextComplexValue++) {

                        final XSAnyImpl simple = (XSAnyImpl) complexValue.getUnknownXMLObjects()
                                .get(nextComplexValue);

                        multiValues.put(simple.getElementQName().getLocalPart(), simple.getTextContent());
                    }

                } else {
                    // Process simple value.
                    simpleValues.add(((XSAnyImpl) xmlObject).getTextContent());
                }

            } else {
                LOG.error("Error: attribute value it's unknown.");
                throw new STORKSAMLEngineException("Attribute value it's unknown.");
            }
        }

        personalAttribute.setValue(simpleValues);
        personalAttribute.setComplexValue(multiValues);
        personalAttrList.add(personalAttribute);
    }

    return personalAttrList;
}

From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that returns a String form of a DOM {@link org.w3c.dom.Node} Object
 * starting at the parent/child level specified by root node
 * @param rootnode   the DOM {@link org.w3c.dom.Node} to use as the root
 * @return an XML form of <code>rootnode</code> and any children
 * @throws IOException//w  w w  .ja  va2 s  .  c  o m
 * @throws TransformerException
 */
final static String documentToXMLstring(final Node rootnode) throws TransformerException {
    final StringWriter out = new StringWriter();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName());
    transformer.transform(new DOMSource(rootnode), new StreamResult(out));
    return out.toString();
}