public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
/**** * Creates a node from the String xml definition * * @return Node representing the xml definition */ private static Node createNode(Document owner, String xmlDefinition) throws SAXException, IOException, ParserConfigurationException { InputSource inputSource = new InputSource(new StringReader(xmlDefinition)); Document document = getDocumentBuilder().parse(inputSource); Node newNode = document.getDocumentElement(); return owner.importNode(newNode, true); }
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; }
/** * Expects an <code>org.w3c.Document</code> as metadata, copies nodes of the document into the DIDL content. * <p> * This method will ignore the content and log a warning if it's of the wrong type. If you override * {@link #createDescMetaHandler(org.fourthline.cling.support.model.DescMeta, org.seamless.xml.SAXParser.Handler)}, * you most likely also want to override this method. * </p> * * @param descElement The DIDL content {@code <desc>} element wrapping the final metadata. * @param descMeta The metadata with a <code>org.w3c.Document</code> payload. */ protected void populateDescMetadata(Element descElement, DescMeta descMeta) { if (descMeta.getMetadata() instanceof Document) { Document doc = (Document) descMeta.getMetadata(); NodeList nl = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() != Node.ELEMENT_NODE) continue; Node clone = descElement.getOwnerDocument().importNode(n, true); descElement.appendChild(clone); } } else { log.warning("Unknown desc metadata content, please override populateDescMetadata(): " + descMeta.getMetadata()); } }
public static void addTemplate(final Element element, final Template template) { try { final byte[] serialized = TemplateSerializer.serialize(template.getDetails()); final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); final Document document; try (final InputStream in = new ByteArrayInputStream(serialized)) { document = docBuilder.parse(in); } final Node templateNode = element.getOwnerDocument().importNode(document.getDocumentElement(), true); element.appendChild(templateNode); } catch (final Exception e) { throw new FlowSerializationException(e); } } }
Configurations value = unmarshaller.unmarshal(document.getDocumentElement(), Configurations.class).getValue(); if (configOrVariant.size() == 1 && configOrVariant.get(0) instanceof Node) { Document configDoc = domBuilder.newDocument(); configDoc.appendChild(configDoc.importNode((Element) configOrVariant.get(0), true)); return new SingleConfig(configParser.apply(c.getIdentity(), configDoc)); } else { .collect(toMap(Configurations.Configuration.Variant::getType, v -> { Document configDoc = domBuilder.newDocument(); configDoc.appendChild(configDoc.importNode(v.getConfig(), true)); return configParser.apply(c.getIdentity(), configDoc); })));
int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); to.appendChild(newNode);
public static void main(String[] args) { //String exp = "/configs/markets/market/"; String exp = "/configs/markets"; String path = "src/a/testConfig.xml"; try { Document xmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(path); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(exp); NodeList nodes = (NodeList) xPathExpression. evaluate(xmlDocument,XPathConstants.NODESET); Document newXmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); //Element root = newXmlDocument.createElement("root"); //newXmlDocument.appendChild(root); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node copyNode = newXmlDocument.importNode(node, true); newXmlDocument.appendChild(copyNode); //root.appendChild(copyNode); } printXmlDocument(newXmlDocument); } catch (Exception ex) { ex.printStackTrace(); } }
Node newTimemlNode = annotatedDoc.importNode(timemlNode, false); if(docIdNode != null){ newTimemlNode.appendChild(annotatedDoc.importNode(docIdNode, true)); newTimemlNode.appendChild(annotatedDoc.importNode(dctNode, true)); if (titleNode != null) { newTimemlNode.appendChild(annotatedDoc.importNode(titleNode, true)); newTimemlNode.appendChild(annotatedDoc.importNode(extraInfoNode, true)); annotatedDoc.appendChild(newTimemlNode);
tc.peek().appendChild( currentP ); } else { parentNode.appendChild( currentP ); currentP.appendChild( spanEl ); anchorOrInline = null; currentP.appendChild( document.importNode(foreignFragment, true) ); currentP.appendChild( document.importNode(foreignFragment, true) );
NodeList nl = orig.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof DocumentType) { doctype = (DocumentType) nl.item(i); if (doctype != null) { doc = builder.getDOMImplementation().createDocument( orig.getDocumentElement().getNamespaceURI(), orig.getDocumentElement().getTagName(), builder.getDOMImplementation().createDocumentType( orig.getDoctype().getName(), orig.getDoctype().getSystemId())); doc.removeChild(doc.getDocumentElement()); } else { doc = builder.newDocument(); Node node = nl.item(i); if (!(node instanceof DocumentType)) { try { doc.appendChild(doc.importNode(node, true)); } catch (DOMException x) {
public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
throw new IllegalArgumentException("Expected MaryXML data"); String rootLanguage = maryxml.getDocument().getDocumentElement().getAttribute("xml:lang"); Document newDoc = MaryXML.newDocument(); Element newRoot = newDoc.getDocumentElement(); Element importedInner = (Element) newDoc.importNode(paragraph, true); Element toImport = (Element) paragraph.getParentNode(); while (!toImport.getTagName().equals(MaryXML.MARYXML)) { Element imported = (Element) newDoc.importNode(toImport, false); imported.appendChild(importedInner); importedInner = imported; toImport = (Element) toImport.getParentNode(); newRoot.appendChild(importedInner);
Node root = document.getDocumentElement(); Document copiedDocument = getDocumentBuilder().newDocument(); Node copiedRoot = copiedDocument.importNode(root, true); copiedDocument.appendChild(copiedRoot); document = copiedDocument; final Element root = document.getDocumentElement();
temp.appendChild(doc.createTextNode((String) source)); nl = temp.getChildNodes(); } else if (source == null) { if (nl.item(i) instanceof Attr) { targetElem = ((Attr) nl.item(i)).getValue(); } else if (nl.item(i) instanceof Text) { targetElem = ((Text) nl.item(i)).getWholeText(); } else { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); targetElem = doc.importNode(nl.item(i), true); org.w3c.dom.Node n = ((org.w3c.dom.Node) targetElem).getOwnerDocument().importNode(nl.item(i), true); if (n instanceof Attr) { ((Element) targetElem).setAttributeNode((Attr) n);
Document newXmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = newXmlDocument.createElement("fileVersionListWrapper"); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); Node copyNode = newXmlDocument.importNode(node, true); root.appendChild(copyNode); } newXmlDocument.appendChild(root);
/** * @param docBuilder * the parser * @param parent * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Document document, Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { Node fragmentNode = XmlUtils.getNewDocumentBuilder().parse( new InputSource(new StringReader(fragment))) .getDocumentElement(); fragmentNode = document.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
org.w3c.dom.Node copy = doc.importNode(el, true); copy.getAttributes().removeNamedItemNS("http://www.w3.org/2000/xmlns/","xml"); } catch (DOMException e) {} doc.appendChild(copy); data.setDocument(doc); org.w3c.dom.Node copy = doc.importNode(el, true); try { copy.getAttributes().removeNamedItemNS("http://www.w3.org/2000/xmlns/","xml"); } catch (DOMException e) {} doc.appendChild(copy); data.setDocument(doc); org.w3c.dom.Node copy = doc.importNode(el, true); try { copy.getAttributes().removeNamedItemNS("http://www.w3.org/2000/xmlns/","xml"); doc.appendChild(copy);
/** * Returns the XML element for this execution within the given Maven POM * * @param document the Maven POM to which to add the element (required) * @return a non-<code>null</code> element */ public Element getElement(final Document document) { final Element executionElement = document.createElement("execution"); // ID if (StringUtils.isNotBlank(id)) { executionElement.appendChild(XmlUtils.createTextElement(document, "id", id)); } // Phase if (StringUtils.isNotBlank(phase)) { executionElement.appendChild(XmlUtils.createTextElement(document, "phase", phase)); } // Goals final Element goalsElement = DomUtils.createChildElement("goals", executionElement, document); for (final String goal : goals) { goalsElement.appendChild(XmlUtils.createTextElement(document, "goal", goal)); } // Configuration if (configuration != null) { final Node configurationNode = document.importNode(configuration.getConfiguration(), true); executionElement.appendChild(configurationNode); } return executionElement; }
public void append(MaryData md) { if (md == null) throw new NullPointerException("Received null marydata"); if (!md.getType().equals(this.getType())) throw new IllegalArgumentException("Cannot append mary data of type `" + md.getType().name() + "' to mary data of type `" + this.getType().name() + "'"); if (getType().isXMLType()) { NodeList kids = md.getDocument().getDocumentElement().getChildNodes(); logger.debug("Appending " + kids.getLength() + " nodes to MaryXML structure"); Element docEl = this.getDocument().getDocumentElement(); for (int i = 0; i < kids.getLength(); i++) { docEl.appendChild(this.getDocument().importNode(kids.item(i), true)); } } else if (getType().isTextType()) { // Attention: XML type is a text type! if (this.plainText == null) { this.plainText = md.getPlainText(); } else { this.plainText = this.plainText + "\n\n" + md.getPlainText(); } } else if (getType().equals(MaryDataType.get("AUDIO"))) { appendAudio(md.getAudio()); } else { throw new UnsupportedOperationException("Cannot append two mary data items of type `" + getType() + "'"); } }