private Element simpleElement(String type) { final Element theElement = (Element) document.createElement(type); root.appendChild(theElement); return theElement; }
private Element createDefaultServerResourceElement(Document doc, String defaultServerResource) { Element defaultResourceElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + DEFAULT_RESOURCE_ELEMENT_NAME); defaultResourceElement.setAttribute(FROM_ATTRIBUTE_NAME, defaultServerResource); return defaultResourceElement; }
private void addFilter(Element filter, String name, String... data) { final Element elt = (Element) document.createElement(name); for (int i = 0; i < data.length; i += 2) { elt.setAttribute(data[i], data[i + 1]); } filter.appendChild(elt); }
public CatalogWriter() { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(false); builderFactory.setValidating(false); document = builderFactory.newDocumentBuilder().newDocument(); catalog = document.createElement("catalog"); document.appendChild(catalog); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testExclusiveSplitXPathAdvanced2() throws Exception { KieBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2"); ksession = createKnowledgeSession(kbase); ksession.getWorkItemManager().registerWorkItemHandler("Email", new SystemOutWorkItemHandler()); Map<String, Object> params = new HashMap<String, Object>(); Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element hi = doc.createElement("hi"); Element ho = doc.createElement("ho"); hi.appendChild(ho); Attr attr = doc.createAttribute("value"); ho.setAttributeNode(attr); attr.setValue("a"); params.put("x", hi); params.put("y", "Second"); ProcessInstance processInstance = ksession.startProcess( "com.sample.test", params); assertProcessInstanceCompleted(processInstance); }
/** * appends a new Element with the given name to currentElement, sets * currentElement to be new Element, and returns the new Element as well */ private Element appendElement(String name) { Element ret = doc.createElement(name); if (currentElement == null) { ret.setAttribute("format_version", Integer.toString(FormatVersion.VERSION)); doc.appendChild(ret); } else { currentElement.appendChild(ret); } currentElement = ret; return ret; }
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setIgnoringComments(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new File("XmlTest.xml")); NodeList nodes = doc.getElementsByTagName("CustomerId"); Text a = doc.createTextNode("value"); Element p = doc.createElement("newNode"); p.appendChild(a); nodes.item(0).getParentNode().insertBefore(p, nodes.item(0));
Element node = factory.createElement("foundJar"); node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-"))); node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1)); node.appendChild(factory.createTextNode((String)subhash.get(keyStr))); container.appendChild(node); Element node = factory.createElement("foundJar"); node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString())); container.appendChild(node);
private Element appendElement(Element parent, String name, String text) { Document document = parent.getOwnerDocument(); Element child = document.createElement(name); parent.appendChild(child); if (text != null) { Text textNode = document.createTextNode(text); child.appendChild(textNode); } return child; }
/** * Test method for {@link XmlUtils#changeNamespace(Node, String, String)}. */ @Test public void testChangeNamespaceWithNoRootNamespace() throws Exception { Document doc = XmlUtils.getDocumentBuilder().newDocument(); Element root = doc.createElement("root"); root = (Element) doc.appendChild(root); final Element child = doc.createElement("child"); root.appendChild(child); final String ns2 = "urn:namespace2"; final Element childWithNamespace = doc.createElementNS(ns2, "childWithNamespace"); root.appendChild(childWithNamespace); root.appendChild(doc.createTextNode("some text")); assertEquals(null, root.getNamespaceURI()); assertEquals(null, child.getNamespaceURI()); assertEquals(ns2, childWithNamespace.getNamespaceURI()); final String ns1 = "urn:namespace1"; root = (Element) XmlUtils.changeNamespace(root, XMLConstants.NULL_NS_URI, ns1); assertEquals(ns1, root.getNamespaceURI()); assertEquals(ns1, root.getElementsByTagName("child").item(0).getNamespaceURI()); assertEquals(ns2, root.getElementsByTagName("childWithNamespace").item(0).getNamespaceURI()); }
private void initUnsafeDoc() { if (unsafeDoc == null) { unsafeDoc = getCustomDocument(); if (unsafeDoc == null) { return; } Element rootElement = (Element) unsafeDoc.getElementsByTagName(CounterEngine.TAG_COUNTERS).item(0); Element familiesElement = (Element) unsafeDoc.getElementsByTagName(CounterEngine.TAG_FAMILYS).item(0); Element typesElements = (Element) unsafeDoc.getElementsByTagName(CounterEngine.TAG_TYPES).item(0); if (rootElement == null) { rootElement = unsafeDoc.createElement(CounterEngine.TAG_COUNTERS); unsafeDoc.appendChild(rootElement); } if (familiesElement == null) { familiesElement = unsafeDoc.createElement(CounterEngine.TAG_FAMILYS); rootElement.appendChild(familiesElement); } if (typesElements == null) { typesElements = unsafeDoc.createElement(CounterEngine.TAG_TYPES); rootElement.appendChild(typesElements); } } }
public static Element appendNewElement(Document document, Element parent, String element, Object content, String namespace) { Element childElement; if (namespace != null) { childElement = document.createElementNS(namespace, element); } else { childElement = document.createElement(element); } if (content != null) { // TODO: We'll have that on Android 2.2: // childElement.setTextContent(content.toString()); // Meanwhile: childElement.appendChild(document.createTextNode(content.toString())); } parent.appendChild(childElement); return childElement; }
private Element getRootNode() { // Create the root node named svg and append it to // the document. final Element svg = (Element) document.createElement("svg"); document.appendChild(svg); // Set some attributes on the root node that are // required for proper rendering. Note that the // approach used here is somewhat different from the // approach used in the earlier program named Svg01, // particularly with regard to the style. svg.setAttribute("xmlns", "http://www.w3.org/2000/svg"); svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); svg.setAttribute("version", "1.1"); return svg; }
/** * 创建XML文档<br> * 创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,既XML在转为文本的时候才定义编码 * * @param rootElementName 根节点名称 * @return XML文档 */ public static Document createXml(String rootElementName) { final Document doc = createXml(); doc.appendChild(doc.createElement(rootElementName)); return doc; }
private Document initDocument() throws IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IOException("Failed to create settings document builder", e); } Document document = documentBuilder.newDocument(); Element settingsElement = document.createElement(SCHEMA_DOCUMENT_ELEMENT); settingsElement.setAttribute(SCHEMA_MODEL_VERSION, "" + getRevisionNumber()); document.appendChild(settingsElement); return document; }
@Test public void testExclusiveSplitXPathAdvanced() throws Exception { KieBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced.bpmn2"); ksession = createKnowledgeSession(kbase); ksession.getWorkItemManager().registerWorkItemHandler("Email", new SystemOutWorkItemHandler()); Map<String, Object> params = new HashMap<String, Object>(); Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element hi = doc.createElement("hi"); Element ho = doc.createElement("ho"); hi.appendChild(ho); Attr attr = doc.createAttribute("value"); ho.setAttributeNode(attr); attr.setValue("a"); params.put("x", hi); params.put("y", "Second"); ProcessInstance processInstance = ksession.startProcess( "com.sample.test", params); assertProcessInstanceCompleted(processInstance); }
private Element createException(Document doc, String clazz, String message, String stacktrace) { Element exceptionElement = doc.createElement("exception"); exceptionElement.setAttribute("class", clazz); if (message != null) { Element messageElement = doc.createElement("message"); messageElement.appendChild(doc.createCDATASection(message)); exceptionElement.appendChild(messageElement); } Element stacktraceElement = doc.createElement("full-stacktrace"); stacktraceElement.appendChild(doc.createCDATASection(stacktrace)); exceptionElement.appendChild(stacktraceElement); return exceptionElement; } }
private static Document makeErr(String msg) { Document d=XmlUtils.getNewDocumentBuilder().newDocument(); Element span = d.createElement("span"); span.setAttribute("style", "color:red;"); d.appendChild(span); Text err = d.createTextNode( msg ); span.appendChild(err); return d; }
Element node = factory.createElement("foundJar"); node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-"))); node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1)); node.appendChild(factory.createTextNode((String)subhash.get(keyStr))); container.appendChild(node); Element node = factory.createElement("foundJar"); node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString())); container.appendChild(node);
private Element appendElement(Element parent, String name, String text) { Document document = parent.getOwnerDocument(); Element child = document.createElement(name); parent.appendChild(child); if (text != null) { Text textNode = document.createTextNode(text); child.appendChild(textNode); } return child; }