Element node = doc_.createElement("New_Node"); node.setTextContent("This is the content"); //adds content node.setAttribute("attrib", "attrib_value"); //adds an attribute
private Node append(final Document doc, final Node parent, final String element, final String value) { final Element child = doc.createElement(element); if (value != null) child.setTextContent(value); parent.appendChild(child); return child; }
private void addStringElement(final Element parentElement, final String elementName, final String value) { final Element childElement = parentElement.getOwnerDocument().createElement(elementName); childElement.setTextContent(value); parentElement.appendChild(childElement); }
private void addStyle(final Element parentElement, final Map<String, String> style) { final Element element = parentElement.getOwnerDocument().createElement("styles"); for (final Map.Entry<String, String> entry : style.entrySet()) { final Element styleElement = parentElement.getOwnerDocument().createElement("style"); styleElement.setAttribute("name", entry.getKey()); styleElement.setTextContent(entry.getValue()); element.appendChild(styleElement); } parentElement.appendChild(element); }
private Element setElementTextContent(String localName, NamespaceImpl namespace, Optional<?> property, String propertyValue) { if (!property.isPresent()) return null; Element root = xmlDoc.getDocumentElement(); Element elem = (Element) root.getElementsByTagNameNS(namespace.getNamespaceURI(), localName).item(0); if (elem == null) { // missing, we add it elem = xmlDoc.createElementNS(namespace.getNamespaceURI(), getQName(localName, namespace)); root.appendChild(elem); } elem.setTextContent(propertyValue); return elem; }
public static Element createIdentificationElementWithStructureAttrAndParent(Document doc, String parentElementName, String identificationID, String structureAttrName, String structureAttrValue) { Element parentElement = doc.createElementNS(NIEMNamespaces.NC_20_NS, parentElementName); Element identificationIDElement = doc.createElementNS(NIEMNamespaces.NC_20_NS, "IdentificationID"); identificationIDElement.setTextContent(identificationID.trim()); identificationIDElement.setAttributeNS(NIEMNamespaces.STRUCT_NS, structureAttrName, structureAttrValue); parentElement.appendChild(identificationIDElement); return parentElement; }
private Element createTimeoutElement(Document doc, String timeoutName, Duration timeout) { Element retElement; if (READ_TIMEOUT_ELEMENT_NAME.equals(timeoutName)) { retElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + READ_TIMEOUT_ELEMENT_NAME); } else if (WRITE_TIMEOUT_ELEMENT_NAME.equals(timeoutName)) { retElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + WRITE_TIMEOUT_ELEMENT_NAME); } else { retElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + CONNECTION_TIMEOUT_ELEMENT_NAME); } retElement.setAttribute(UNIT_ATTRIBUTE_NAME, DEFAULT_UNIT_ATTRIBUTE_VALUE); retElement.setTextContent(Long.toString(timeout.getSeconds())); return retElement; }
private void setTextContentElement(Element element, DslElementModel<?> elementModel, Element parentNode) { getCustomizedValue(elementModel).ifPresent(value -> { DslElementSyntax dsl = elementModel.getDsl(); if (dsl.supportsChildDeclaration() && !dsl.supportsAttributeDeclaration()) { if (elementModel.getConfiguration().map(c -> c.getProperty(IS_CDATA).isPresent()).orElse(false)) { element.appendChild(doc.createCDATASection(value)); } else { element.setTextContent(value); } if (parentNode != element) { parentNode.appendChild(element); } } else { parentNode.setAttribute(dsl.getAttributeName(), value); } }); }
@Override protected Element createRootElement(Document doc, ResourcePool resourcePool) { Element rootElement = null; if (ClusteredResourcePoolImpl.class == resourcePool.getClass()) { rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + CLUSTERED_ELEMENT_NAME); } else if (DedicatedClusteredResourcePoolImpl.class == resourcePool.getClass()) { DedicatedClusteredResourcePoolImpl dedicatedClusteredResourcePool = (DedicatedClusteredResourcePoolImpl) resourcePool; rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + DEDICATED_ELEMENT_NAME); if (dedicatedClusteredResourcePool.getFromResource() != null) { rootElement.setAttribute(FROM_ELEMENT_NAME, dedicatedClusteredResourcePool.getFromResource()); } rootElement.setAttribute(UNIT_ELEMENT_NAME, dedicatedClusteredResourcePool.getUnit().toString()); rootElement.setTextContent(String.valueOf(dedicatedClusteredResourcePool.getSize())); } else if (SharedClusteredResourcePoolImpl.class == resourcePool.getClass()) { SharedClusteredResourcePoolImpl sharedClusteredResourcePool = (SharedClusteredResourcePoolImpl) resourcePool; rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + SHARED_ELEMENT_NAME); rootElement.setAttribute(SHARING_ELEMENT_NAME, sharedClusteredResourcePool.getSharedResourcePool()); } return rootElement; }
public static Document annotationToXmlDocument(Annotation annotation) { Element dateElem = XMLUtils.createElement("DATE"); dateElem.setTextContent(annotation.get(CoreAnnotations.DocDateAnnotation.class)); Element textElem = annotationToTmlTextElement(annotation); Element docElem = XMLUtils.createElement("DOC"); docElem.appendChild(dateElem); docElem.appendChild(textElem); // Create document and import elements into this document.... Document doc = XMLUtils.createDocument(); doc.appendChild(doc.importNode(docElem, true)); return doc; }
public static Element createSourceSystemElement(Document doc, String sourceSystemNamespace, String sourceSystemName) { Element sourceSystemElement = doc.createElementNS(sourceSystemNamespace, "SourceSystemNameText"); sourceSystemElement.setTextContent(sourceSystemName); return sourceSystemElement; }
@Override public void run(Debugger context, CommandArguments args, Document document) { Element init = document.createElement("init"); init.setAttribute("xmlns", "urn:debugger_protocol_v1"); init.setAttribute("fileuri", this.fileUri); init.setAttribute("language", this.language); init.setAttribute("protocol_version", this.protocolVersion); init.setAttribute("appid", this.appId); init.setAttribute("idekey", this.ideKey); Element engine = document.createElement("engine"); engine.setAttribute("version", Information.CORE_VERSION); engine.setTextContent("JPHP Debugger"); init.appendChild(engine); Element author = document.createElement("author"); author.setTextContent("JPHP Group"); init.appendChild(author); Element url = document.createElement("url"); url.setTextContent("http://j-php.net"); init.appendChild(url); Element copyright = document.createElement("copyright"); copyright.setTextContent("Copyright (c) 2015 by JPHP Group"); init.appendChild(copyright); document.appendChild(init); }
private static void addTextElement(final Element element, final String name, final Optional<String> value) { if (!value.isPresent()) { return; } final Document doc = element.getOwnerDocument(); final Element toAdd = doc.createElement(name); toAdd.setTextContent(CharacterFilterUtils.filterInvalidXmlCharacters(value.get())); // value should already be filtered, but just in case ensure there are no invalid xml characters element.appendChild(toAdd); }
org.w3c.dom.Document docContainer, DocumentFragment docfrag, String errMsg) { org.w3c.dom.Element wr = docContainer.createElementNS(Namespaces.NS_WORD12, "r"); org.w3c.dom.Element wt = docContainer.createElementNS(Namespaces.NS_WORD12, "t"); wt.setTextContent(errMsg); wr.appendChild(wt); } else if (sdtParent.equals("tbl")) { org.w3c.dom.Element wtr = docContainer.createElementNS(Namespaces.NS_WORD12, "tr"); docfrag.appendChild(wtr); org.w3c.dom.Element wtc = docContainer.createElementNS(Namespaces.NS_WORD12, "tc"); wtr.appendChild(wtc); org.w3c.dom.Element wp = docContainer.createElementNS(Namespaces.NS_WORD12, "p"); wtc.appendChild(wp); wp.appendChild(wr);
public static Element createClaimValue(String claimValue) { Element elClaims = createClaimsElement(); Element elClaimValue = elClaims.getOwnerDocument().createElementNS(IDENTITY_NS_05_05, "ClaimValue"); elClaimValue.setAttributeNS(null, "Uri", CLAIM_ROLE_NAME); Element elValue = elClaims.getOwnerDocument().createElementNS(IDENTITY_NS_05_05, "Value"); elValue.setTextContent(claimValue); elClaimValue.appendChild(elValue); elClaims.appendChild(elClaimValue); return elClaims; }
private Element createSharedPoolElement(Document doc, String poolName, ServerSideConfiguration.Pool pool) { Element poolElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + SHARED_POOL_ELEMENT_NAME); poolElement.setAttribute(NAME_ATTRIBUTE_NAME, poolName); String from = pool.getServerResource(); if (from != null) { if (from.trim().length() == 0) { throw new XmlConfigurationException("Resource pool name can not be empty."); } poolElement.setAttribute(FROM_ATTRIBUTE_NAME, from); } long memoryInBytes = MemoryUnit.B.convert(pool.getSize(), MemoryUnit.B); poolElement.setAttribute(UNIT_ATTRIBUTE_NAME, MemoryUnit.B.toString()); poolElement.setTextContent(Long.toString(memoryInBytes)); return poolElement; }
public static Timex fromMap(String text, Map<String, String> map) { try { Element element = XMLUtils.createElement("TIMEX3"); for (Map.Entry<String, String> entry : map.entrySet()) { if (entry.getValue() != null) { element.setAttribute(entry.getKey(), entry.getValue()); } } element.setTextContent(text); return new Timex(element); } catch (Exception ex) { throw new RuntimeException(ex); } }
@Test public void writeDOMSource() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); Document document = documentBuilderFactory.newDocumentBuilder().newDocument(); Element rootElement = document.createElement("root"); document.appendChild(rootElement); rootElement.setTextContent("Hello World"); DOMSource domSource = new DOMSource(document); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(domSource, null, outputMessage); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>")); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length, outputMessage.getHeaders().getContentLength()); }
private static final Element addElement(Document document, Node parentNode, String namespaceUri, String qualifiedName, String value, Map<String, String> attributes) { Element newElement = document.createElementNS(namespaceUri, qualifiedName); if (value != null) { newElement.setTextContent(value); } for (String attributeName : attributes.keySet()) { newElement.setAttribute(attributeName, attributes.get(attributeName)); } parentNode.appendChild(newElement); return newElement; }
Element newB = document.createElement("B"); Element newC = document.createElement("c"); newC.setTextContent("11"); Element newD = document.createElement("d"); newD.setTextContent("21"); Element newE = document.createElement("e"); newE.setTextContent("31"); newB.appendChild(newC); newB.appendChild(newD); newB.appendChild(newE); document.getDocumentElement().appendChild(newB);