private Attr createAttribute(Node node, String name, String value) { Attr attr = node.getOwnerDocument().createAttribute(name); attr.setNodeValue(value); return attr; }
/** If the child node or attribute doesn't exist, it is created. Usage example: Node property = * getFirstChildByAttrValue(properties, "property", "name"); */ private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(childName)) { NamedNodeMap attributes = childNodes.item(i).getAttributes(); Node attribute = attributes.getNamedItem(attr); if (attribute.getNodeValue().equals(value)) return childNodes.item(i); } } Node newNode = node.getOwnerDocument().createElement(childName); NamedNodeMap attributes = newNode.getAttributes(); Attr nodeAttr = node.getOwnerDocument().createAttribute(attr); nodeAttr.setNodeValue(value); attributes.setNamedItem(nodeAttr); if (childNodes.item(0) != null) { return node.insertBefore(newNode, childNodes.item(0)); } else { return node.appendChild(newNode); } }
/** If the child node or attribute doesn't exist, it is created. Usage example: Node property = * getFirstChildByAttrValue(properties, "property", "name"); */ private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(childName)) { NamedNodeMap attributes = childNodes.item(i).getAttributes(); Node attribute = attributes.getNamedItem(attr); if (attribute.getNodeValue().equals(value)) return childNodes.item(i); } } Node newNode = node.getOwnerDocument().createElement(childName); NamedNodeMap attributes = newNode.getAttributes(); Attr nodeAttr = node.getOwnerDocument().createAttribute(attr); nodeAttr.setNodeValue(value); attributes.setNamedItem(nodeAttr); if (childNodes.item(0) != null) { return node.insertBefore(newNode, childNodes.item(0)); } else { return node.appendChild(newNode); } }
@Test public void marshalDOMResult() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); Document result = builder.newDocument(); DOMResult domResult = new DOMResult(result); marshaller.marshal(flights, domResult); Document expected = builder.newDocument(); Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights"); Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns"); namespace.setNodeValue("http://samples.springframework.org/flight"); flightsElement.setAttributeNode(namespace); expected.appendChild(flightsElement); Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight"); flightsElement.appendChild(flightElement); Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number"); flightElement.appendChild(numberElement); Text text = expected.createTextNode("42"); numberElement.appendChild(text); assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected)); }
(atts.getLocalName(i).length() == 0)) { attr = doc.createAttribute(atts.getQName(i)); attr.setNodeValue(atts.getValue(i)); ((Element)top).setAttributeNode(attr); } else { attr = doc.createAttributeNS(atts.getURI(i), atts.getLocalName(i)); attr.setNodeValue(atts.getValue(i)); ((Element)top).setAttributeNodeNS(attr);
@Test public void marshalEmptyDOMResult() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); DOMResult domResult = new DOMResult(); marshaller.marshal(flights, domResult); assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document); Document result = (Document) domResult.getNode(); Document expected = builder.newDocument(); Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights"); Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns"); namespace.setNodeValue("http://samples.springframework.org/flight"); flightsElement.setAttributeNode(namespace); expected.appendChild(flightsElement); Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight"); flightsElement.appendChild(flightElement); Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number"); flightElement.appendChild(numberElement); Text text = expected.createTextNode("42"); numberElement.appendChild(text); assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected)); }
private Attr createAttribute(Node node, String name, String value) { Attr attr = node.getOwnerDocument().createAttribute(name); attr.setNodeValue(value); return attr; }
private Attr createAttribute(Node node, String name, String value) { Attr attr = node.getOwnerDocument().createAttribute(name); attr.setNodeValue(value); return attr; }
public void postProcess(ParserContext unused, BeanAssembler assembler, Element element) { Attr attribute = element.getOwnerDocument().createAttribute(name); attribute.setNodeValue(value); assembler.extendTarget(attribute); }
private void replaceLinkAttributes(final Element pushcontent) { for (final String attName: new String[] { ATTRIBUTE_NAME_HREF, ATTRIBUTE_NAME_CONREF }) { final Attr att = pushcontent.getAttributeNode(attName); if (att != null) { att.setNodeValue(replaceURL(att.getNodeValue())); } } }
private static void addAttributeToNode(@Nonnull final Node targetNode, @Nonnull final String attributeName, @Nonnull final String attributeValue) { final NamedNodeMap nodeAttributes = targetNode.getAttributes(); final Document doc = targetNode.getOwnerDocument(); final Attr attribute = doc.createAttribute(attributeName); attribute.setNodeValue(attributeValue); nodeAttributes.setNamedItem(attribute); }
private void addNodeAttribute(Node node, String name, String value) { Attr newNode = node.getOwnerDocument().createAttributeNS(node.getNamespaceURI(), name); newNode.setNodeValue(value); node.getAttributes().setNamedItem(newNode); }
@Override public void addToSchemaElement(XmlSchemaElement schemaElement) { if (expectedContentTypes != null) { Map<String, Node> extAttrMap = new HashMap<>(); Attr theAttr = DOMUtils.getEmptyDocument().createAttributeNS(XML_MIME_NS, "xmime"); theAttr.setNodeValue(expectedContentTypes); extAttrMap.put("xmime", theAttr); schemaElement.addMetaInfo(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES, extAttrMap); } }
@Override public void addToSchemaElement(XmlSchemaElement schemaElement) { if (expectedContentTypes != null) { Map<String, Node> extAttrMap = new HashMap<>(); Attr theAttr = DOMUtils.getEmptyDocument().createAttributeNS(XML_MIME_NS, "xmime"); theAttr.setNodeValue(expectedContentTypes); extAttrMap.put("xmime", theAttr); schemaElement.addMetaInfo(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES, extAttrMap); } }
public void addAttribute(String xPath, String attrName, String attrValue) { Node node = getNode(xPath); NamedNodeMap namedNodeMap = node.getAttributes(); Attr attr = document.createAttribute(attrName); attr.setNodeValue(attrValue); namedNodeMap.setNamedItem(attr); }
public void attribute(String uri, String localName, String qName, String value) throws IOException { if (_node instanceof Element) { Attr attr = _doc.createAttributeNS(uri, qName); attr.setNodeValue(value); ((Element) _node).setAttributeNode(attr); } else ((QDocument) _node).setAttribute(qName, value); }
public void attribute(String uri, String localName, String qName, String value) throws IOException { if (_node instanceof Element) { Attr attr = _doc.createAttributeNS(uri, qName); attr.setNodeValue(value); ((Element) _node).setAttributeNode(attr); } else ((QDocument) _node).setAttribute(qName, value); }
public void setAttributeNS(String uri, String local, String value) { Attr attr = _owner.createAttributeNS(uri, local); attr.setNodeValue(value); setAttributeNodeNS(attr); }
public void setAttributeNS(String uri, String local, String value) { Attr attr = _owner.createAttributeNS(uri, local); attr.setNodeValue(value); setAttributeNodeNS(attr); }
public void setAttribute(String name, String value) throws DOMException { Attr attribute = getAttributeNode(name); if (attribute == null) { attribute = mOwnerDocument.createAttribute(name); } attribute.setNodeValue(value); mAttributes.setNamedItem(attribute); }