Example usage for org.w3c.dom Element setNodeValue

List of usage examples for org.w3c.dom Element setNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Element setNodeValue.

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Utils.java

public static Element createNewElementAndSet(Document document, Element parent, String childElement,
        String childValue) {/*from w w w .ja va 2  s  .  c o  m*/
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);
    child.setNodeValue(childValue);
    child.appendChild(document.createTextNode(childValue));
    return child;
}

From source file:Main.java

/**
 * Copy one node to another node.//w  w  w.  j a v  a 2s .  co m
 * @param source source Node
 * @param dest destination Node
 * @return destination Node
 */
public static synchronized Node copyNode(Node source, Node dest) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
        return tn;
    }

    Node attr = null;
    NamedNodeMap attrs = source.getAttributes();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            attr = attrs.item(i);
            ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue());
        }
    }

    Node child = null;
    NodeList list = source.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (!(child instanceof Text)) {
            Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName());

            if (child.getNodeValue() != null) {
                en.setNodeValue(child.getNodeValue());
            }

            Node n = copyNode(child, en);
            dest.appendChild(n);
        } else if (child instanceof CDATASection) {
            CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue());
            dest.appendChild(cd);
        } else {
            Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue());
            dest.appendChild(tn);
        }
    }
    return dest;
}

From source file:es.bsc.servicess.ide.actions.DeployAction.java

private void addLocalhostToProjectFile(IJavaProject pr, String coreLocation)
        throws ConfigurationException, SAXException, IOException, CoreException, ParserConfigurationException,
        TransformerFactoryConfigurationError, TransformerException {
    /*//  w  w w  .  j  av a2s  .  c  o  m
     * <Worker Name="enric2"> <InstallDir>/IT_worker/</InstallDir>
     * <WorkingDir>/home/user/</WorkingDir> <User>user</User>
     * <LimitOfTasks>1</LimitOfTasks> </Worker>
     */
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(getProjectFileLocation(pr));

    Element project = doc.getDocumentElement();
    Element worker = doc.createElement("Worker");
    worker.setAttribute("Name", "localhost");
    Element installDir = doc.createElement("InstallDir");
    installDir.setNodeValue(coreLocation);
    worker.appendChild(installDir);
    Element workingDir = doc.createElement("WorkingDir");
    workingDir.setNodeValue(coreLocation);
    worker.appendChild(workingDir);
    Element user = doc.createElement("User");
    user.setNodeValue(System.getProperty("user.name"));
    worker.appendChild(user);
    Element limit = doc.createElement("LimitOfTasks");
    limit.setNodeValue("2");
    worker.appendChild(limit);
    project.appendChild(project);
    Source source = new DOMSource(doc);
    IFolder output = pr.getProject().getFolder("output");
    if (output != null && output.exists()) {
        IFolder war_folder = output.getFolder(pr.getProject().getName());
        // Prepare the output file
        File file = (war_folder.getFile("project.xml").getLocation().toFile());
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }

}

From source file:com.rover12421.shaka.apktool.lib.AndrolibResourcesAj.java

private boolean horizontalScrollView_check(String errInfo) throws Exception {
    if (errInfo.indexOf("'@android:style/Widget.HorizontalScrollView'") > 0) {
        Pattern xmlPathPattern = Pattern.compile(
                "(.+?):\\d+:.+?(Error retrieving parent for item).+?'@android:style/Widget.HorizontalScrollView'");
        Matcher xmlPathMatcher = xmlPathPattern.matcher(errInfo);
        if (xmlPathMatcher.find()) {
            String xmlPathStr = xmlPathMatcher.group(1);
            File xmlPathFile = new File(xmlPathStr);
            if (xmlPathFile.exists()) {
                LogHelper.warning("Find HorizontalScrollView exception xml : " + xmlPathStr);
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(xmlPathStr);
                NodeList list = document.getChildNodes().item(0).getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    Node node = list.item(i);
                    if (node.getAttributes() != null) {
                        Node attr = node.getAttributes().getNamedItem("parent");
                        if (attr != null
                                && attr.getNodeValue().equals("@android:style/Widget.HorizontalScrollView")) {
                            /**
                             * HorizontalScrollView??aaptScrollView
                             *//*from  w  w  w  . ja v  a 2s . c  o m*/
                            attr.setNodeValue("@android:style/Widget.ScrollView");
                            /**
                             * ? android:scrollbars  android:fadingEdge 
                             * ??,
                             * ?????
                             */
                            Element scrollbars = document.createElement("item");
                            scrollbars.setAttribute("name", "android:scrollbars");
                            scrollbars.setNodeValue("horizontal");

                            Element fadingEdge = document.createElement("item");
                            fadingEdge.setAttribute("name", "android:fadingEdge");
                            fadingEdge.setNodeValue("horizontal");
                            Element element = (Element) node;
                            NodeList items = element.getElementsByTagName("item");
                            for (int j = 0; j < items.getLength(); j++) {
                                Element item = (Element) items.item(j);
                                if (item.getAttribute("name").equals("android:scrollbars")) {
                                    scrollbars = null;
                                }
                                if (item.getAttribute("name").equals("android:fadingEdge")) {
                                    fadingEdge = null;
                                }
                            }

                            if (scrollbars != null) {
                                node.appendChild(scrollbars);
                            }

                            if (fadingEdge != null) {
                                node.appendChild(fadingEdge);
                            }
                        }
                    }
                }

                /**
                 * ?xml
                 */
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(xmlPathStr));

                return true;
            }
        }
    }

    return false;
}

From source file:com.twinsoft.convertigo.engine.util.WsReference.java

private static void extractSoapVariables(XmlSchema xmlSchema, List<RequestableHttpVariable> variables,
        Node node, String longName, boolean isMulti, QName variableType) throws EngineException {
    if (node == null)
        return;//from   w ww .  j a va2s.  c o m
    int type = node.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element != null) {
            String elementName = element.getLocalName();
            if (longName != null)
                elementName = longName + "_" + elementName;

            if (!element.getAttribute("soapenc:arrayType").equals("") && !element.hasChildNodes()) {
                String avalue = element.getAttribute("soapenc:arrayType");
                element.setAttribute("soapenc:arrayType", avalue.replaceAll("\\[\\]", "[1]"));

                Element child = element.getOwnerDocument().createElement("item");
                String atype = avalue.replaceAll("\\[\\]", "");
                child.setAttribute("xsi:type", atype);
                if (atype.startsWith("xsd:")) {
                    String variableName = elementName + "_item";
                    child.appendChild(
                            element.getOwnerDocument().createTextNode("$(" + variableName.toUpperCase() + ")"));
                    RequestableHttpVariable httpVariable = createHttpVariable(true, variableName,
                            new QName(Constants.URI_2001_SCHEMA_XSD, atype.split(":")[1]));
                    variables.add(httpVariable);
                }
                element.appendChild(child);
            }

            // extract from attributes
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node child = map.item(i);
                if (child.getNodeName().equals("soapenc:arrayType"))
                    continue;
                if (child.getNodeName().equals("xsi:type"))
                    continue;
                if (child.getNodeName().equals("soapenv:encodingStyle"))
                    continue;

                String variableName = getVariableName(variables, elementName + "_" + child.getLocalName());

                child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                RequestableHttpVariable httpVariable = createHttpVariable(false, variableName,
                        Constants.XSD_STRING);
                variables.add(httpVariable);
            }

            // extract from children nodes
            boolean multi = false;
            QName qname = Constants.XSD_STRING;
            NodeList children = element.getChildNodes();
            if (children.getLength() > 0) {
                Node child = element.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.COMMENT_NODE) {
                        String value = child.getNodeValue();
                        if (value.startsWith("type:")) {
                            String schemaType = child.getNodeValue().substring("type:".length()).trim();
                            qname = getVariableSchemaType(xmlSchema, schemaType);
                        }
                        if (value.indexOf("repetitions:") != -1) {
                            multi = true;
                        }
                    } else if (child.getNodeType() == Node.TEXT_NODE) {
                        String value = child.getNodeValue().trim();
                        if (value.equals("?") || !value.equals("")) {
                            String variableName = getVariableName(variables, elementName);

                            child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                            RequestableHttpVariable httpVariable = createHttpVariable(isMulti, variableName,
                                    variableType);
                            variables.add(httpVariable);
                        }
                    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
                        extractSoapVariables(xmlSchema, variables, child, elementName, multi, qname);
                        multi = false;
                        qname = Constants.XSD_STRING;
                    }

                    child = child.getNextSibling();
                }
            }

        }
    }
}

From source file:com.portfolio.data.provider.MysqlAdminProvider.java

@Override
public String getGroupRightList() {
    PreparedStatement st;/*from ww  w .j a  v a2 s  . c  o m*/
    String sql;
    ResultSet res;
    String retVal = "";

    try {
        sql = "SELECT grid, owner, label, change_rights, bin2uuid(portfolio_id) "
                + "FROM group_right_info ORDER BY portfolio_id";
        st = connection.prepareStatement(sql);
        res = st.executeQuery();

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        Element groups = document.createElement("group_rights");
        document.appendChild(groups);

        String prevPort = "DUMMY";
        Element portNode = null;
        while (res.next()) {
            Integer grid = res.getInt(1);
            Integer owner = res.getInt(2);
            String label = res.getString(3);
            Integer change = res.getInt(4);
            String portfolio = res.getString(5);
            if (portfolio == null)
                portfolio = "";

            if (!prevPort.equals(portfolio)) {
                portNode = document.createElement("portfolio");
                portNode.setAttribute("id", portfolio);
                groups.appendChild(portNode);

                prevPort = portfolio;
            }

            Element group = document.createElement("group");
            group.setAttribute("grid", grid.toString());
            group.setAttribute("owner", owner.toString());
            group.setAttribute("change_rights", change.toString());
            //          group.setAttribute("portfolio", portfolio);
            group.setNodeValue(label);

            portNode.appendChild(group);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        retVal = writer.getBuffer().toString().replaceAll("\n|\r", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return retVal;
}

From source file:org.apache.nutch.collection.CollectionManager.java

/**
 * Save collections into file/*  w w w  . java2 s  . c o  m*/
 * 
 * @throws Exception
 */
public void save() throws IOException {
    try {
        final FileOutputStream fos = new FileOutputStream(new File(configfile.getFile()));
        final Document doc = new DocumentImpl();
        final Element collections = doc.createElement(Subcollection.TAG_COLLECTIONS);
        final Iterator iterator = collectionMap.values().iterator();

        while (iterator.hasNext()) {
            final Subcollection subCol = (Subcollection) iterator.next();
            final Element collection = doc.createElement(Subcollection.TAG_COLLECTION);
            collections.appendChild(collection);
            final Element name = doc.createElement(Subcollection.TAG_NAME);
            name.setNodeValue(subCol.getName());
            collection.appendChild(name);
            final Element whiteList = doc.createElement(Subcollection.TAG_WHITELIST);
            whiteList.setNodeValue(subCol.getWhiteListString());
            collection.appendChild(whiteList);
            final Element blackList = doc.createElement(Subcollection.TAG_BLACKLIST);
            blackList.setNodeValue(subCol.getBlackListString());
            collection.appendChild(blackList);
        }

        DomUtil.saveDom(fos, collections);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        throw new IOException(e.toString());
    }
}

From source file:org.kalypsodeegree.xml.XMLTools.java

/**
 * copies one node to another node (of a different dom document).
 */// ww w .  j  ava2s  .  c om
public static Node copyNode(final Node source, final Node dest) {
    // Debug.debugMethodBegin( "XMLTools", "copyNode" );
    if (source.getNodeType() == Node.TEXT_NODE) {
        final Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
        return tn;
    }

    final NamedNodeMap attr = source.getAttributes();

    if (attr != null) {
        for (int i = 0; i < attr.getLength(); i++) {
            ((Element) dest).setAttribute(attr.item(i).getNodeName(), attr.item(i).getNodeValue());
        }
    }

    final NodeList list = source.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        if (!(list.item(i) instanceof Text)) {
            final Element en = dest.getOwnerDocument().createElementNS(list.item(i).getNamespaceURI(),
                    list.item(i).getNodeName());

            if (list.item(i).getNodeValue() != null) {
                en.setNodeValue(list.item(i).getNodeValue());
            }

            final Node n = copyNode(list.item(i), en);
            dest.appendChild(n);
        } else if (list.item(i) instanceof CDATASection) {
            final CDATASection cd = dest.getOwnerDocument().createCDATASection(list.item(i).getNodeValue());
            dest.appendChild(cd);
        } else {
            final Text tn = dest.getOwnerDocument().createTextNode(list.item(i).getNodeValue());
            dest.appendChild(tn);
        }
    }

    // Debug.debugMethodEnd();
    return dest;
}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Creates an element with the given tag name and a value  
 * @param tagName//from  w ww.j  a va2 s  . c o  m
 * @param nodeValue
 * @return
 */
public Element createElement(String tagName, String nodeValue) {
    Element element = doc.createElement(tagName);

    if (nodeValue == null) {
        return element;
    } else {
        element.setNodeValue(nodeValue);
        return element;
    }
}