List of usage examples for org.w3c.dom Attr setValue
public void setValue(String value) throws DOMException;
From source file:Main.java
/** * Sets an attribute.//w ww . jav a2 s .c o 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); }
From source file:Main.java
/** * Add an attribute to an Element.// w ww . j av a2s .c om * @param doc XML document * @param element XML Element * @param attribute attribute name * @param value attribute value */ public static void addAttribute(final Document doc, final Element element, final String attribute, final String value) { final NamedNodeMap atts = element.getAttributes(); final Attr newAttr = doc.createAttribute(attribute); newAttr.setValue(value); atts.setNamedItem(newAttr); }
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);/*from w ww.j a va 2 s .c o m*/ attr = doc.createAttribute("xmlns"); attr.setValue("http://www.example.com/airlines"); item.setAttributeNode(attr); 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
public static Attr createAttr(String attrName, String attrValue, Document document) { if (attrName == null || attrName.length() == 0 || attrValue == null || document == null) { return null; }/* w w w .j av a2s . c o m*/ Attr attribute = document.createAttribute(attrName); attribute.setValue(attrValue); return attribute; }
From source file:Main.java
public static Attr addAttribute(Node parent, String name, String value) { Attr node = parent.getOwnerDocument().createAttribute(name); node.setValue(value); parent.getAttributes().setNamedItem(node); return node;/*w ww . j a v a2 s .c o m*/ }
From source file:Main.java
/** * Utility method to add an attribute to a given element * * @param document// w ww .j av a 2 s . c o m * @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
/** * Add the specified attribute./*from ww w. j ava2s.c o m*/ * @param e The node to add the attribute to. * @param name The name of the attribute. * @param value The value of the attribute. */ public static void addAttribute(final Node e, final String name, final String value) { final Attr attr = e.getOwnerDocument().createAttribute(name); attr.setValue(value); e.getAttributes().setNamedItem(attr); }
From source file:Main.java
public static Attr addAttribut(Document doc, String tag, String value) { Attr attr = doc.createAttribute(tag); attr.setValue(value); return attr;/*from w w w . j a v a 2 s. c om*/ }
From source file:Main.java
/** * Sets an attribute on the specified node, with the given name and value *///from www . j ava 2 s . c o m public static void setStrValue(Node node, String name, String value) { Attr att = node.getOwnerDocument().createAttribute(name); node.getAttributes().setNamedItem(att); att.setValue(value); }
From source file:Main.java
/** * Adds a new node to a file.// w w w.ja v a 2s. c o m * * @param nodeType {@link String} The type of the element to add. * @param idField {@link String} The name of the field used to identify this * node. * @param nodeID {@link String} The identifier for this node, so its data * can be later retrieved and modified. * @param destFile {@link File} The file where the node must be added. * @param attributes {@link ArrayList} of array of String. The arrays must * be bidimensional (first index must contain attribute name, second one * attribute value). Otherwise, an error will be thrown. However, if * <value>null</value>, it is ignored. */ public static void addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList<String[]> attributes) { if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { if (it.next().length != 2) { throw new IllegalArgumentException("Invalid attribute combination"); } } } /* * XML DATA CREATION - BEGINNING */ DocumentBuilder docBuilder; Document doc; try { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = docBuilder.parse(destFile); } catch (SAXException | IOException | ParserConfigurationException ex) { return; } Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType); NamedNodeMap elementAttributes = newElement.getAttributes(); Attr attrID = doc.createAttribute(idField); attrID.setValue(nodeID); elementAttributes.setNamedItem(attrID); if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { String[] x = it.next(); Attr currAttr = doc.createAttribute(x[0]); currAttr.setValue(x[1]); elementAttributes.setNamedItem(currAttr); } } index.appendChild(newElement); /* * XML DATA CREATION - END */ /* * XML DATA DUMP - BEGINNING */ Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException ex) { return; } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); try { transformer.transform(source, result); } catch (TransformerException ex) { return; } String xmlString = result.getWriter().toString(); try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) { bufferedWriter.write(xmlString); } catch (IOException ex) { } /* * XML DATA DUMP - END */ }