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

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);/*from  w w  w . jav  a  2 s.  c  o m*/

    // staff elements
    Element staff = doc.createElement("Staff");
    rootElement.appendChild(staff);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue("1");
    staff.setAttributeNode(attr);

    // shorten way
    // staff.setAttribute("id", "1");

    // firstname elements
    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("A"));
    staff.appendChild(firstname);

    // lastname elements
    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("B"));
    staff.appendChild(lastname);

    // nickname elements
    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("C"));
    staff.appendChild(nickname);

    // salary elements
    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("100000"));
    staff.appendChild(salary);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

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

    transformer.transform(source, result);

    System.out.println("File saved!");
}

From source file:Main.java

/**
 * @param sampleNode//from  w  w w  . j  a  va2 s  . c  o  m
 */
public static void tTestSetSampleAsReference(Node sampleNode) {
    Attr attr = sampleNode.getOwnerDocument().createAttribute("ttest");
    attr.setNodeValue("reference");
    Element e = (Element) sampleNode;
    e.setAttributeNode(attr);
}

From source file:Main.java

public static void tTestSetSampleSignificane(Node sampleNode, boolean different) {
    Attr attr = sampleNode.getOwnerDocument().createAttribute("ttest");
    if (different)
        attr.setNodeValue("H1");
    else/*from  ww  w  .ja  v a  2  s. c o m*/
        attr.setNodeValue("H0");
    // System.out.println(attr.getNodeValue());
    Element e = (Element) sampleNode;
    e.setAttributeNode(attr);
    e.removeAttribute("ttest-level");
}

From source file:Main.java

public static void setTTestSetSampleSignificane(Node sampleNode, boolean different, double level) {
    Attr attr = sampleNode.getOwnerDocument().createAttribute("ttest");
    Attr attrLevel = sampleNode.getOwnerDocument().createAttribute("ttest-level");
    if (different)
        attr.setNodeValue("H1");
    else//www  . j  a v a  2s. com
        attr.setNodeValue("H0");
    // System.out.println(attr.getNodeValue());
    attrLevel.setNodeValue(level + "");
    Element e = (Element) sampleNode;
    e.setAttributeNode(attr);
    e.setAttributeNode(attrLevel);
}

From source file:Main.java

/**
 * Adds a {@link Attr} to the parent {@link Element}. 
 *
 * @param doc//from  w  w  w.ja  v  a 2  s  . com
 * @param parent
 * @param name
 * @return
 */
public static Attr addAttr(Document doc, Element parent, String name) {
    final Attr attr = doc.createAttribute(name);
    parent.setAttributeNode(attr);
    return attr;
}

From source file:Main.java

private static void addAttribute(Document doc, Element root, String param, String value) {
    Attr attr = doc.createAttribute(param);
    attr.setValue(value);/*  www . j  a va 2 s. c  o m*/
    root.setAttributeNode(attr);
}

From source file:Main.java

public static void addAttribute(Document doc, Element element, String name, String value) {
    Attr attr = doc.createAttribute(name);
    attr.setValue(value);/*w  w w. j  ava  2 s .c  om*/
    element.setAttributeNode(attr);
}

From source file:Main.java

/**
 * Set or add node attribute./*from   w  ww  . java 2s.c o  m*/
 * 
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @param attributeValue
 *          the attribute value
 * @param doc
 *          the document
 */
public static void setAddNodeAttribute(Node node, String attributeName, String attributeValue, Document doc) {
    if (null == node) {
        return;
    }
    NamedNodeMap attributes = node.getAttributes();
    Element element = (Element) node;
    if (null == attributes) {
        element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
    } else {
        Node n = attributes.getNamedItem(attributeName);
        if (null == n) {
            element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
        } else {
            n.setNodeValue(attributeValue);
        }
    }
}

From source file:Main.java

/**
 * Utility method to add an attribute to a given element
 *
 * @param document/*  w  w w.jav a2s . c om*/
 * @param AttribName
 * @param attribValue
 * @param element
 */
public static void addAttribute(Document document, String AttribName, String attribValue, Element element) {
    Attr attribute = document.createAttribute(AttribName);
    attribute.setValue(attribValue);
    element.setAttributeNode(attribute);
}

From source file:Main.java

/**
 * Sets an attribute.// ww w  .  j  av a 2s. co  m
 * 
 * @param doc document
 * @param node parent node
 * @param name name of attribute
 * @param text value of attribute
 * @return attribute object
 */
public static Attr setAttr(Document doc, Element node, String name, String text) {
    Attr attr = doc.createAttribute(name);
    attr.setValue(text);
    return node.setAttributeNode(attr);
}