Example usage for org.w3c.dom Node toString

List of usage examples for org.w3c.dom Node toString

Introduction

In this page you can find the example usage for org.w3c.dom Node toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

/**
 * Writes a DOM node (and all its ancestors) to the given output stream
 * //ww  w  .ja  v a 2s . c  o m
 * @param n
 *            the node to write
 * @param o
 *            the output stream to write the node to
 */
public static void writeNode(Node n, FileOutputStream o) {
    PrintWriter w = new PrintWriter(o);
    w.print(n.toString());
    w.close();
}

From source file:Main.java

/**
 * Returns the value of <code>Node</code>. This is done by assuming that
 * this node has one child that is a textnode. The value of this textnode is
 * returned.//from  w  w w.  j av a2  s .co  m
 * 
 * @param node
 *            The node that has to be examined.
 * @return The value of the node.
 */
public static String nodeValue(Node node) {
    if (node == null)
        return null;
    Node child = node.getFirstChild();
    if (child == null)
        return null;
    return (child.toString().equals("") ? null : child.toString());
}

From source file:Main.java

/**
 * Creates a map of tag name to dirty header XML from the SOAP header node.
 *
 * @param headerNode the header node to extract all XML from
 * @return a map of tag name to dirty header XML
 *///w ww. ja v a  2 s .c om
private static Map<String, String> createTagToDirtyXmlMap(Node headerNode) {
    Map<String, String> dirtyXmlMap = new HashMap<String, String>();
    for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) {
        Node headerChildNode = headerNode.getChildNodes().item(i);
        if (Arrays.asList(SENSITIVE_HEADER_TAGS).contains(headerChildNode.getLocalName())) {
            dirtyXmlMap.put(headerChildNode.getLocalName(), headerChildNode.toString());
        }
    }
    return dirtyXmlMap;
}

From source file:com.hp.test.framework.generatejellytess.GenerateJellyTests.java

public static void getData(Node parentNode, Node node) {

    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE: {
        if (node.toString().contains("Verification")) {
            System.out.println("******" + node.toString());
            verfiy = true;/*www  . j  a  v a 2  s.c  om*/
        }
        if (node.hasChildNodes()) {
            NodeList list = node.getChildNodes();
            int size = list.getLength();

            for (int index = 0; index < size; index++) {
                getData(node, list.item(index));
            }
        }

        break;
    }

    case Node.TEXT_NODE: {
        String data = node.getNodeValue();

        if (data.trim().length() > 0) {
            Locators.put(parentNode.getNodeName(), node.getNodeValue());
        }
        break;
    }

    }
}

From source file:io.cloudslang.content.xml.utils.XmlUtils.java

public static String nodeToString(Node node) throws TransformerException {

    if (node == null) {
        return EMPTY;
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        return node.toString();
    } else {// w ww .j a  v a2 s  .c  o m
        return transformElementNode(node);
    }
}

From source file:de.betterform.xml.xforms.action.LoadAction.java

private Node doIncludes(Node embed, String absoluteURI) {
    LOGGER.debug("LoadAction.doInclude: Node to embed: " + embed.toString() + " URI: " + absoluteURI);
    try {/*from   w w w.  ja va 2  s .c  o  m*/
        String uri = absoluteURI.substring(0, absoluteURI.lastIndexOf("/") + 1);

        CachingTransformerService transformerService = (CachingTransformerService) this.container.getProcessor()
                .getContext().get(TransformerService.TRANSFORMER_SERVICE);
        Transformer transformer = transformerService.getTransformerByName("include.xsl");

        DOMSource source = new DOMSource(embed);
        DOMResult result = new DOMResult();
        transformer.setParameter("root", uri);
        transformer.transform(source, result);

        return result.getNode();
    } catch (TransformerException e) {
        LOGGER.debug("LoadAction.doInclude: TransformerException:", e);
    }
    return embed;
}

From source file:DOMTreeTest.java

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        Node node = (Node) value;
        if (node instanceof Element)
            return elementPanel((Element) node);

        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        if (node instanceof CharacterData)
            setText(characterString((CharacterData) node));
        else//w  w  w  .  j a  v a2  s.c  o m
            setText(node.getClass() + ": " + node.toString());
        return this;
    }

From source file:be.ibridge.kettle.trans.step.xmlinput.XMLInput.java

private Row getRowFromXML() throws KettleValueException {
    while (data.itemPosition >= data.itemCount || data.file == null) // finished reading the file, read the next file!
    {/*from  w ww.j a  v  a  2s .c om*/
        data.file = null;
        if (!openNextFile()) {
            return null;
        }
    }

    Row row = buildEmptyRow();

    // Get the item in the XML file...

    // First get the appropriate node

    Node itemNode;
    if (meta.getInputPosition().length > 1) {
        itemNode = XMLHandler.getSubNodeByNr(data.section, data.itemElement, data.itemPosition);
    } else {
        itemNode = data.section; // Only the root node, 1 element to read in the whole document.
    }
    data.itemPosition++;

    // Read from the Node...
    for (int i = 0; i < meta.getInputFields().length; i++) {
        Node node = itemNode;

        XMLInputField xmlInputField = meta.getInputFields()[i];

        String value = null;

        for (int p = 0; (value == null) && node != null && p < xmlInputField.getFieldPosition().length; p++) {
            XMLInputFieldPosition pos = xmlInputField.getFieldPosition()[p];

            switch (pos.getType()) {
            case XMLInputFieldPosition.XML_ELEMENT: {
                if (pos.getElementNr() <= 1) {
                    Node subNode = XMLHandler.getSubNode(node, pos.getName());
                    if (subNode != null) {
                        if (p == xmlInputField.getFieldPosition().length - 1) // last level
                        {
                            value = XMLHandler.getNodeValue(subNode);
                        }
                    } else {
                        if (log.isDebug())
                            logDebug(Messages.getString("XMLInput.Log.UnableToFindPosition", pos.toString(),
                                    node.toString()));
                    }
                    node = subNode;
                } else // Multiple possible values: get number pos.getElementNr()!
                {
                    Node subNode = XMLHandler.getSubNodeByNr(node, pos.getName(), pos.getElementNr() - 1,
                            false);
                    if (subNode != null) {
                        if (p == xmlInputField.getFieldPosition().length - 1) // last level
                        {
                            value = XMLHandler.getNodeValue(subNode);
                        }
                    } else {
                        if (log.isDebug())
                            logDebug(Messages.getString("XMLInput.Log.UnableToFindPosition", pos.toString(),
                                    node.toString()));
                    }
                    node = subNode;
                }
            }
                break;

            case XMLInputFieldPosition.XML_ATTRIBUTE: {
                value = XMLHandler.getTagAttribute(node, pos.getName());
            }
                break;
            case XMLInputFieldPosition.XML_ROOT: {
                value = XMLHandler.getNodeValue(node);
            }
                break;
            default:
                break;
            }

        }

        // OK, we have the string...
        Value v = row.getValue(i);

        if (value != null)
            v.setValue(value);
        else
            v.setNull();

        // DO Trimming!
        switch (xmlInputField.getTrimType()) {
        case XMLInputField.TYPE_TRIM_LEFT:
            v.ltrim();
            break;
        case XMLInputField.TYPE_TRIM_RIGHT:
            v.rtrim();
            break;
        case XMLInputField.TYPE_TRIM_BOTH:
            v.trim();
            break;
        default:
            break;
        }

        // System.out.println("after trim, field #"+i+" : "+v);

        // DO CONVERSIONS...
        switch (xmlInputField.getType()) {
        case Value.VALUE_TYPE_STRING:
            // System.out.println("Convert value to String :"+v);
            break;
        case Value.VALUE_TYPE_NUMBER:
            // System.out.println("Convert value to Number :"+v);
            if (xmlInputField.getFormat() != null && xmlInputField.getFormat().length() > 0) {
                if (xmlInputField.getDecimalSymbol() != null && xmlInputField.getDecimalSymbol().length() > 0) {
                    if (xmlInputField.getGroupSymbol() != null && xmlInputField.getGroupSymbol().length() > 0) {
                        if (xmlInputField.getCurrencySymbol() != null
                                && xmlInputField.getCurrencySymbol().length() > 0) {
                            v.str2num(xmlInputField.getFormat(), xmlInputField.getGroupSymbol(),
                                    xmlInputField.getGroupSymbol(), xmlInputField.getCurrencySymbol());
                        } else {
                            v.str2num(xmlInputField.getFormat(), xmlInputField.getGroupSymbol(),
                                    xmlInputField.getGroupSymbol());
                        }
                    } else {
                        v.str2num(xmlInputField.getFormat(), xmlInputField.getGroupSymbol());
                    }
                } else {
                    v.str2num(xmlInputField.getFormat()); // just a format mask
                }
            } else {
                v.str2num();
            }
            v.setLength(xmlInputField.getLength(), xmlInputField.getPrecision());
            break;
        case Value.VALUE_TYPE_INTEGER:
            // System.out.println("Convert value to integer :"+v);
            v.setValue(v.getInteger());
            v.setLength(xmlInputField.getLength(), xmlInputField.getPrecision());
            break;
        case Value.VALUE_TYPE_BIGNUMBER:
            // System.out.println("Convert value to BigNumber :"+v);
            v.setValue(v.getBigNumber());
            v.setLength(xmlInputField.getLength(), xmlInputField.getPrecision());
            break;
        case Value.VALUE_TYPE_DATE:
            // System.out.println("Convert value to Date :"+v);

            if (xmlInputField.getFormat() != null && xmlInputField.getFormat().length() > 0) {
                v.str2dat(xmlInputField.getFormat());
            } else {
                v.setValue(v.getDate());
            }
            break;
        case Value.VALUE_TYPE_BOOLEAN:
            v.setValue(v.getBoolean());
            break;
        default:
            break;
        }

        // Do we need to repeat this field if it is null?
        if (meta.getInputFields()[i].isRepeated()) {
            if (v.isNull() && data.previousRow != null) {
                Value previous = data.previousRow.getValue(i);
                v.setValue(previous);
            }
        }

    } // End of loop over fields...

    // See if we need to add the filename to the row...  
    if (meta.includeFilename() && meta.getFilenameField() != null && meta.getFilenameField().length() > 0) {
        Value fn = new Value(meta.getFilenameField(), KettleVFS.getFilename(data.file));
        row.addValue(fn);
    }

    // See if we need to add the row number to the row...  
    if (meta.includeRowNumber() && meta.getRowNumberField() != null && meta.getRowNumberField().length() > 0) {
        Value fn = new Value(meta.getRowNumberField(), data.rownr);
        row.addValue(fn);
    }

    data.previousRow = new Row(row); // copy it to make sure the next step doesn't change it in between... 
    data.rownr++;

    // Throw away the information in the item?
    NodeList nodeList = itemNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++)
        itemNode.removeChild(nodeList.item(i));

    return row;
}

From source file:com.panet.imeta.trans.steps.xmlinput.XMLInput.java

private Object[] getRowFromXML() throws KettleValueException {
    // finished reading the file, read the next file

    while (data.itemPosition >= data.itemCount || data.file == null) {
        data.file = null;// w w w . j  a  v a 2s. com
        if (!openNextFile()) {
            return null;
        }
    }

    Object[] outputRowData = buildEmptyRow();

    // Get the item in the XML file...

    // First get the appropriate node

    Node itemNode;
    if (meta.getInputPosition().length > 1) {
        itemNode = XMLHandler.getSubNodeByNr(data.section, data.itemElement, data.itemPosition);
    } else {
        itemNode = data.section; // Only the root node, 1 element to read
        // in the whole document.
    }
    data.itemPosition++;

    // Read from the Node...
    for (int i = 0; i < meta.getInputFields().length; i++) {
        Node node = itemNode;

        XMLInputField xmlInputField = meta.getInputFields()[i];

        // This value will contain the value we're looking for...
        //
        String value = null;

        for (int p = 0; (value == null) && node != null && p < xmlInputField.getFieldPosition().length; p++) {
            XMLInputFieldPosition pos = xmlInputField.getFieldPosition()[p];

            switch (pos.getType()) {
            case XMLInputFieldPosition.XML_ELEMENT: {
                if (pos.getElementNr() <= 1) {
                    Node subNode = XMLHandler.getSubNode(node, pos.getName());
                    if (subNode != null) {
                        if (p == xmlInputField.getFieldPosition().length - 1) // last
                        // level
                        {
                            value = XMLHandler.getNodeValue(subNode);
                        }
                    } else {
                        if (log.isDebug())
                            logDebug(Messages.getString("XMLInput.Log.UnableToFindPosition", pos.toString(),
                                    node.toString()));
                    }
                    node = subNode;
                } else
                // Multiple possible values: get number
                // pos.getElementNr()!
                {
                    Node subNode = XMLHandler.getSubNodeByNr(node, pos.getName(), pos.getElementNr() - 1,
                            false);
                    if (subNode != null) {
                        if (p == xmlInputField.getFieldPosition().length - 1) // last
                        // level
                        {
                            value = XMLHandler.getNodeValue(subNode);
                        }
                    } else {
                        if (log.isDebug())
                            logDebug(Messages.getString("XMLInput.Log.UnableToFindPosition", pos.toString(),
                                    node.toString()));
                    }
                    node = subNode;
                }
            }
                break;

            case XMLInputFieldPosition.XML_ATTRIBUTE: {
                value = XMLHandler.getTagAttribute(node, pos.getName());
            }
                break;
            case XMLInputFieldPosition.XML_ROOT: {
                value = XMLHandler.getNodeValue(node);
            }
                break;
            default:
                break;
            }

        }

        // OK, we have grabbed the string called value
        // Trim it, convert it, ...

        // DO Trimming!
        switch (xmlInputField.getTrimType()) {
        case XMLInputField.TYPE_TRIM_LEFT:
            value = Const.ltrim(value);
            break;
        case XMLInputField.TYPE_TRIM_RIGHT:
            value = Const.rtrim(value);
            break;
        case XMLInputField.TYPE_TRIM_BOTH:
            value = Const.trim(value);
            break;
        default:
            break;
        }

        // System.out.println("after trim, field #"+i+" : "+v);

        // DO CONVERSIONS...
        //
        ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta(i);
        ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta(i);
        outputRowData[i] = targetValueMeta.convertData(sourceValueMeta, value);

        // Do we need to repeat this field if it is null?
        if (meta.getInputFields()[i].isRepeated()) {
            if (data.previousRow != null && Const.isEmpty(value)) {
                outputRowData[i] = data.previousRow[i];
            }
        }
    } // End of loop over fields...

    int outputIndex = meta.getInputFields().length;

    // See if we need to add the filename to the row...
    if (meta.includeFilename() && !Const.isEmpty(meta.getFilenameField())) {
        outputRowData[outputIndex++] = KettleVFS.getFilename(data.file);
    }

    // See if we need to add the row number to the row...
    if (meta.includeRowNumber() && !Const.isEmpty(meta.getRowNumberField())) {
        outputRowData[outputIndex++] = new Long(data.rownr);
    }

    RowMetaInterface irow = getInputRowMeta();

    data.previousRow = irow == null ? outputRowData : (Object[]) irow.cloneRow(outputRowData); // copy it to make
    // surely the next step doesn't change it in between...
    data.rownr++;

    // Throw away the information in the item?
    NodeList nodeList = itemNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        itemNode.removeChild(nodeList.item(i));
    }

    return outputRowData;
}

From source file:org.ambraproject.wombat.util.ParseXmlUtil.java

private static void appendElementNode(StringBuilder mixedContent, Node child) {
    String nodeName = child.getNodeName();
    mixedContent.append('<').append(nodeName);
    List<Node> attributes = NodeListAdapter.wrap(child.getAttributes());

    // Search for xlink attributes and declare the xlink namespace if found
    // TODO Better way? This is probably a symptom of needing to use a proper XML library here in the first place.
    for (Node attribute : attributes) {
        if (attribute.getNodeName().startsWith("xlink:")) {
            mixedContent.append(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"");
            break;
        }//w  w  w . jav a2  s . c  o  m
    }

    for (Node attribute : attributes) {
        mixedContent.append(' ').append(attribute.toString());
    }

    mixedContent.append('>');
    getElementMixedContent(mixedContent, child);
    mixedContent.append("</").append(nodeName).append('>');
}