Example usage for org.w3c.dom Element setAttributeNode

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

Introduction

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

Prototype

public Attr setAttributeNode(Attr newAttr) throws DOMException;

Source Link

Document

Adds a new attribute node.

Usage

From source file:Main.java

/**
 * Adds an attribute to the specified document element.
 * @param doc the document./* w  w w.  ja  v  a 2s  . c o m*/
 * @param parent the parent element.
 * @param name the attribute name.
 * @param value the attribute value.
 * @return the attribute.
 */
public static Attr addAttribute(final Document doc, final Element parent, final String name,
        final String value) {
    Attr attr = doc.createAttribute(name);
    attr.setNodeValue(value);
    parent.setAttributeNode(attr);
    return attr;
}

From source file:Main.java

public static void appendStringAttribute(Element thisElem, String attrName, String attrValue) {
    Attr attr = thisElem.getOwnerDocument().createAttribute(attrName);

    attr.setTextContent(attrValue);// w  w w . ja  va 2 s.c  om
    thisElem.setAttributeNode(attr);

}

From source file:Main.java

public static void addAttributeToElement(Document doc, Element projectElement, String attributeName,
        String attributeValue) {/*  w  w  w  .  j a v  a 2 s. c  om*/
    Attr xmlnsAttr = doc.createAttribute(attributeName);
    xmlnsAttr.setValue(attributeValue);
    projectElement.setAttributeNode(xmlnsAttr);
}

From source file:Main.java

private static Element populateRootElement(Document doc, Element item) {

    // set attribute to root element
    Attr attr = doc.createAttribute("xmlns:xsi");
    attr.setValue("http://www.w3.org/2001/XMLSchema-instance");
    item.setAttributeNode(attr);

    attr = doc.createAttribute("xmlns");
    attr.setValue("http://www.example.com/airlines");
    item.setAttributeNode(attr);/*from w  w  w .  j a v a  2 s  .c  o m*/

    attr = doc.createAttribute("xsi:schemaLocation");
    attr.setValue("http://www.example.com/airlines airline.xsd");
    item.setAttributeNode(attr);

    /**
     * TODO Do the same for root element
     */
    return item;
}

From source file:Main.java

private static Attr getOrCreateAttribute(Element parent, String name) {
    Attr a = parent.getAttributeNode(name);
    if (a == null) {
        Document doc = parent.getOwnerDocument();
        a = doc.createAttribute(name);//w ww.  j av a  2s.c  om
        parent.setAttributeNode(a);
    }
    return a;
}

From source file:Main.java

public static void writeXMLObject(String path, String name, String tag, String model, double ry, String version,
        double x, double y, double z) {
    try {/*from   w  w w. j a v a  2 s. c  om*/
        id++;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // Root elements
        Document doc = docBuilder.newDocument();
        doc.setXmlStandalone(true);
        Element rootElement = doc.createElement(tag);
        doc.appendChild(rootElement);

        rootElement.setAttributeNode(addAttribut(doc, "action", "void"));
        rootElement.setAttributeNode(addAttribut(doc, "actionDist", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "collidable", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "id", id + ""));
        rootElement.setAttributeNode(addAttribut(doc, "model", model));
        rootElement.setAttributeNode(addAttribut(doc, "pCameraFacing", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "pControlFlow", "false"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitH", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitInnerRadius", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitOutterRadius", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitType", "0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitW", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pFactoryNumber", "500"));
        rootElement.setAttributeNode(addAttribut(doc, "pInitialVelocity", "0.0030"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxAngle", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxLife", "2000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinAngle", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinLife", "1000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSecVar", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSeconds", "100"));
        rootElement.setAttributeNode(addAttribut(doc, "pSpeed", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "rx", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "ry", ry + ""));
        rootElement.setAttributeNode(addAttribut(doc, "rz", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "s", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "type", "basic"));
        rootElement.setAttributeNode(addAttribut(doc, "versionCode", version));
        rootElement.setAttributeNode(addAttribut(doc, "x", x + ""));
        rootElement.setAttributeNode(addAttribut(doc, "y", y + ""));
        rootElement.setAttributeNode(addAttribut(doc, "z", z + ""));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(path + "\\" + name + id + ".xml");
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}

From source file:Main.java

public static Element addElement(Node parent, String name, Attr[] attrs) {
    Element element;
    if (parent instanceof Document) {
        element = ((Document) parent).createElement(name);
    } else {//from  ww w . j a  v  a2s.c  o m
        element = parent.getOwnerDocument().createElement(name);
    }
    if (attrs != null && attrs.length > 0) {
        for (Attr attr : attrs) {
            element.setAttributeNode(attr);
        }
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

public static Element addTextElement(Node parent, String name, String value, Attr[] attrs) {
    Element element;
    if (parent instanceof Document) {
        element = ((Document) parent).createElement(name);
    } else {/*from   w w w . j a v a  2 s .c o  m*/
        element = parent.getOwnerDocument().createElement(name);
    }

    if (attrs != null && attrs.length > 0) {
        for (Attr attr : attrs) {
            element.setAttributeNode(attr);
        }
    }

    if (value != null) {
        element.setTextContent(value);
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Append attribute.//from ww w.j  a  v a 2 s .  co  m
 *
 * @param baseNode
 *            the base node
 * @param attributeName
 *            the attribute name
 * @param attributeValue
 *            the attribute value
 */
public static void appendAttribute(Element baseNode, String attributeName, String attributeValue) {
    Attr typeAttribute = baseNode.getOwnerDocument().createAttribute(attributeName);
    if (attributeValue == null) {
        typeAttribute.setNodeValue("<null>");
    } else {
        typeAttribute.setNodeValue(attributeValue);
    }
    baseNode.setAttributeNode(typeAttribute);
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void setPropertyValue(Document document, String propertyName, String value)
        throws XPathExpressionException {
    XPathExpression compile = pathFactory
            .compile("hibernate-configuration/session-factory/property[@name='" + propertyName + "']"); //$NON-NLS-1$ //$NON-NLS-2$
    Node node = (Node) compile.evaluate(document, XPathConstants.NODE);
    if (node != null) {
        node.setTextContent(value);//from   w w w .  j a  v a 2 s. c  o  m
    } else {
        XPathExpression parentNodeExpression = pathFactory.compile("hibernate-configuration/session-factory"); //$NON-NLS-1$
        Node parentNode = (Node) parentNodeExpression.evaluate(document, XPathConstants.NODE);
        // Create a new property element for this datasource-specified property (TMDM-4927).
        Element property = document.createElement("property"); //$NON-NLS-1$
        Attr propertyNameAttribute = document.createAttribute("name"); //$NON-NLS-1$
        property.setAttributeNode(propertyNameAttribute);
        propertyNameAttribute.setValue(propertyName);
        property.setTextContent(value);
        parentNode.appendChild(property);
    }
}