本文整理汇总了Java中org.w3c.dom.Document.adoptNode方法的典型用法代码示例。如果您正苦于以下问题:Java Document.adoptNode方法的具体用法?Java Document.adoptNode怎么用?Java Document.adoptNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.Document
的用法示例。
在下文中一共展示了Document.adoptNode方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wrapWithSoapEnvelope
import org.w3c.dom.Document; //导入方法依赖的package包/类
public Document wrapWithSoapEnvelope(Element element) {
DocumentBuilder documentBuilder;
try {
documentBuilder = newDocumentBuilder();
} catch (ParserConfigurationException e) {
LOG.error("*** ALERT: Failed to create a document builder when trying to construct the the soap message. ***", e);
throw propagate(e);
}
Document document = documentBuilder.newDocument();
document.adoptNode(element);
Element envelope = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Envelope");
Element body = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Body");
envelope.appendChild(body);
body.appendChild(element);
document.appendChild(envelope);
return document;
}
示例2: createSameTree
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Create the same tree if it does not already exist.
*
* @return
* @throws Exception
*/
private static Node createSameTree(Document doc, Node externalNode, String xPath, TwsCachedXPathAPI xpathApi) throws Exception {
if (xPath.indexOf('/') < 0 || externalNode == null) {
// error root reached
}
Node internalNode = xpathApi.selectSingleNode(doc, xPath);
if (internalNode != null) {
return internalNode;
} else {
String xPathParent = xPath.substring(0, xPath.lastIndexOf('/'));
Node externalNodeParent = externalNode.getParentNode();
Node internalNodeParent = createSameTree(doc, externalNodeParent, xPathParent, xpathApi);
internalNode = doc.adoptNode(externalNode.cloneNode(false));
internalNodeParent.appendChild(internalNode);
return internalNode;
}
}
示例3: parseInputStream
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Parse the specified input stream in a DOM DocumentFragment, owned by the specified Document.
*
* @param input the InputStream to parse
* @param owningDocument the Document which will own the returned DocumentFragment
* @return a DocumentFragment
* @throws DecryptionException thrown if there is an error parsing the input stream
*/
private DocumentFragment parseInputStream(InputStream input, Document owningDocument) throws DecryptionException {
// Since Xerces currently seems not to handle parsing into a DocumentFragment
// without a bit hackery, use this to simulate, so we can keep the API
// the way it hopefully will look in the future. Obviously this only works for
// input streams containing valid XML instances, not fragments.
Document newDocument = null;
try {
newDocument = parserPool.parse(input);
} catch (XMLParserException e) {
log.error("Error parsing decrypted input stream", e);
throw new DecryptionException("Error parsing input stream", e);
}
Element element = newDocument.getDocumentElement();
owningDocument.adoptNode(element);
DocumentFragment container = owningDocument.createDocumentFragment();
container.appendChild(element);
return container;
}
示例4: testAdoptNode
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testAdoptNode() {
try {
DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
docBuilder = docBF.newDocumentBuilder();
Document doc1 = parse(data);
Document doc2 = docBuilder.newDocument();
Node element = doc2.adoptNode(doc1.getDocumentElement());
System.out.println("OK.");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Excpetion while adopting node: " + e.getMessage());
}
}
示例5: unpackageTests
import org.w3c.dom.Document; //导入方法依赖的package包/类
public static Document unpackageTests(Document dmnXML) throws ParserConfigurationException {
NodeList testCases = dmnXML.getElementsByTagNameNS(DMN_TCK_NS, "testCases");
if (testCases.getLength() == 0) {
return null;
}
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document tckTests = dBuilder.newDocument();
Node testCasesRoot = testCases.item(0);
Node adoptedNode = tckTests.adoptNode(testCasesRoot.cloneNode(true));
tckTests.appendChild(adoptedNode);
NamedNodeMap attributes = dmnXML.getDocumentElement().getAttributes();
// Copy XML Schema and XML Schema instance namespace prefix. Especially useful for qnames resolution of types
for (int i = 0; i < attributes.getLength(); i++) {
if (attributes.item(i).getNodeName().startsWith("xmlns:") && attributes.item(i).getNodeValue().startsWith("http://www.w3.org/")) {
tckTests.getDocumentElement().setAttribute(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue());
}
}
return tckTests;
}
示例6: assembleModule
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Assemble module.
*
* @return the document
* @throws TransformerException in case an error occurred during the transformation process.
* @throws DOMException in case of a DOM traversing problem.
* @throws ParserConfigurationException in case an error occurred during the parsing of the configuration
*/
private Document assembleModule()
throws TransformerException, DOMException, ParserConfigurationException {
// create empty document
DocumentBuilderFactory tFactory = DocumentBuilderFactory.newInstance();
Document tDoc = tFactory.newDocumentBuilder().getDOMImplementation().createDocument(null, null, null);
Element tRoot = tDoc.createElement("Configuration");
tDoc.appendChild(tRoot);
System.out.println("AsXmlTool: assemble AS configuration");
for (String tModule : getModuleLoadOrder()) {
System.out.println("...module " + tModule);
Document tC = mDocuments.get(tModule);
NodeList tElem = XPathAPI.selectNodeList(tC.getDocumentElement(), "/Configuration/*");
for (int j = 0; j < tElem.getLength(); j++) {
Element tNode = (Element) tElem.item(j);
if (tNode.getTagName().equals("Version")) {
continue;
}
if (tNode.getTagName().equals("inherits")) {
continue;
}
tRoot.appendChild(tDoc.createTextNode(tNode.getPreviousSibling().getNodeValue()));
tNode.setAttribute("module", tModule);
tNode.getParentNode().removeChild(tNode);
tDoc.adoptNode(tNode);
tRoot.appendChild(tNode);
}
}
return tDoc;
}
示例7: adoptElement
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Adopts an element into a document if the child is not already in the document.
*
* @param adoptee the element to be adopted
* @param adopter the document into which the element is adopted
*/
public static void adoptElement(Element adoptee, Document adopter) {
if (!(adoptee.getOwnerDocument().equals(adopter))) {
if (adopter.adoptNode(adoptee) == null) {
// This can happen if the adopter and adoptee were produced by different DOM implementations
throw new XMLRuntimeException("DOM Element node adoption failed");
}
}
}
示例8: adoptNode
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Makes a new document adopt a node from a different document, and correctly reassign namespace
* and prefix
* @param document the new document
* @param node the node to adopt.
* @return the adopted node.
*/
static Node adoptNode(Document document, Node node) {
Node newNode = document.adoptNode(node);
updateNamespace(newNode, document);
return newNode;
}
示例9: writeToDOM
import org.w3c.dom.Document; //导入方法依赖的package包/类
private synchronized void writeToDOM(Node target, short type) {
Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT) ?
target.getOwnerDocument() : (Document)target;
DOMParser parser = fGrammar.getDOMParser();
StringReader aReader = new StringReader(fData);
InputSource aSource = new InputSource(aReader);
try {
parser.parse(aSource);
}
catch (SAXException e) {
// this should never happen!
// REVISIT: what to do with this?; should really not
// eat it...
}
catch (IOException i) {
// ditto with above
}
Document aDocument = parser.getDocument();
parser.dropDocumentReferences();
Element annotation = aDocument.getDocumentElement();
Node newElem = null;
if (futureOwner instanceof CoreDocumentImpl) {
newElem = futureOwner.adoptNode(annotation);
// adoptNode will return null when the DOM implementations are not compatible.
if (newElem == null) {
newElem = futureOwner.importNode(annotation, true);
}
}
else {
newElem = futureOwner.importNode(annotation, true);
}
target.insertBefore(newElem, target.getFirstChild());
}
示例10: preProcess
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Override
protected void preProcess(Document doc, Node src, Node dest) throws DocTemplateException {
NodeList childElements = src.getChildNodes();
for (int i = 0; i < childElements.getLength(); i++) {
Node childElement = childElements.item(i);
Node result = null;
boolean field = false;
// OO Field Eigenschaft kann mit "variable-get" oder "variable-set" Knotename behandlen werden.
if (childElement.getNodeName().endsWith(ODT_BOOKMARK_TAG_SUFFIX)
|| (field = childElement.getNodeName().endsWith(OO_FIELD_GET_TAG_POSTFIX) || childElement.getNodeName().endsWith(OO_FIELD_SET_TAG_POSTFIX))) {
String key = "";
// In Fall des Fields key gleich mit dem "text:name" Eigenschaft
if (field) {
key = childElement.getAttributes().getNamedItem(TEXT_NAME).getTextContent();
} else {
key = childElement.getAttributes().item(0).getTextContent();
}
// mehrere gleiche Textmarken mit ALT-Suffix intern ohne ALT-Suffix anwenden
int altPos = key.indexOf(ALTERNATE_SUFFIX);
if (altPos > 0) {
key = key.substring(0, altPos);
}
if (key != null && (key.startsWith(getFieldPrefix()) || key.startsWith(SORTFIELD_PREFIX) || key.startsWith(CONDITION_BEGIN) || key.startsWith(CONDITION_END)
|| key.startsWith(ITERATION_BEGIN) || key.startsWith(ITERATION_END)) || field) {
result = doc.createElement(INTERNAL_BOOKMARK_TAG);
// In Fall des Fields wird ein Feld mit Prefix "FIELD_" generiert. Das bedautet, wir behandlen den
// OO Field ebenso, als "FIELD_" Bookmark
result.setTextContent(field ? (getFieldPrefix() + key) : key);
}
}
if (result == null) {
result = doc.adoptNode(childElement.cloneNode(false));
}
dest.appendChild(result);
preProcess(doc, childElement, result);
}
}
示例11: testCreateUserAccount
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* This will check if adoptNode works will adoptNode from
* @see <a href="content/userInfo.xml">userInfo.xml</a>
* @see <a href="content/accountInfo.xml">accountInfo.xml</a>. This is
* adopting a node from the XML file which is validated by a DTD and
* into an XML file which is validated by the schema This covers Row 5
* for the table
* http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed
* bug 4893745 because there was a difference in behavior.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCreateUserAccount() throws Exception {
String userXmlFile = XML_DIR + "userInfo.xml";
String accountXmlFile = XML_DIR + "accountInfo.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(userXmlFile);
Element user = (Element) document.getElementsByTagName("FirstName").item(0);
// Set schema after parsing userInfo.xml. Otherwise it will conflict
// with DTD validation.
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
DocumentBuilder docBuilder1 = dbf.newDocumentBuilder();
docBuilder1.setErrorHandler(eh);
Document accDocument = docBuilder1.parse(accountXmlFile);
Element firstName = (Element) accDocument
.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0);
Element adoptedAccount = (Element) accDocument.adoptNode(user);
Element parent = (Element) firstName.getParentNode();
parent.replaceChild(adoptedAccount, firstName);
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
MyDOMOutput mydomoutput = new MyDOMOutput();
mydomoutput.setByteStream(System.out);
writer.write(document, mydomoutput);
writer.write(accDocument, mydomoutput);
assertFalse(eh.isAnyError());
}
示例12: preProcess
import org.w3c.dom.Document; //导入方法依赖的package包/类
protected void preProcess(Document doc, Node src, Node dest) throws DocTemplateException {
NodeList childElements = src.getChildNodes();
for (int i = 0; i < childElements.getLength(); i++) {
Node childElement = childElements.item(i);
Node result = null;
String nodeName = childElement.getNodeName();
boolean field = nodeName.toUpperCase().endsWith(XML_FIELD);
if (NAMESPACE_URI.equals(childElement.getNamespaceURI())) {
String key = null;
// In Fall des Fields key gleich mit dem "text:name" Eigenschaft
if (field) {
key = getValueOfAttribute(XML_FIELD_PATH, childElement);
String formatter = getValueOfAttribute(XML_FIELD_FORMATTER, childElement);
if (!StringUtils.isEmpty(formatter)) {
String postFix = LdtConstants.FORMAT_SUFFIX + formatter;
key = key + postFix;
keyTranslationTable.put(postFix, postFix);
}
} else {
for (String blockElement : BLOCK_MARKERS) {
if (nodeName.toUpperCase().endsWith(blockElement)) {
key = blockElement + "_" + childElement.getAttributes().item(0).getTextContent();
break;
}
}
}
if (key != null) {
result = doc.createElement(INTERNAL_BOOKMARK_TAG);
result.setTextContent(field ? (getFieldPrefix() + key) : key);
String sort = null;
if ((sort = getValueOfAttribute(SORT, childElement)) != null) {
if (!sort.equalsIgnoreCase(ASC) && !sort.equalsIgnoreCase(DESC)) {
log.warn("Die Sortierung ist falsch: asc oder desc!");
} else {
dest.appendChild(result);
result = doc.createElement(INTERNAL_BOOKMARK_TAG);
String body = SORT.toUpperCase().concat("_").concat(getPfadOnly(key));
if (sort.equalsIgnoreCase(DESC)) {
body = body.concat("_").concat(sort.toUpperCase());
}
result.setTextContent(body);
}
}
Node attr = childElement.getAttributes().getNamedItem("attribute");
if (attr != null) {
String s = attr.getNodeValue();
Node srcAttr = src.getAttributes().getNamedItem(s);
if (srcAttr != null) {
srcAttr.setNodeValue(INTERNAL_BOOKMARK_XML_ATTR_START + result.getTextContent() + INTERNAL_BOOKMARK_XML_ATTR_END);
dest.getAttributes().setNamedItem(doc.adoptNode(srcAttr.cloneNode(false)));
continue;
}
}
if (!field) {
dest.appendChild(result);
preProcess(doc, childElement, dest);
result = doc.createElement(INTERNAL_BOOKMARK_TAG);
result.setTextContent("END".concat(key));
dest.appendChild(result);
continue;
}
}
}
if (result == null) {
result = doc.adoptNode(childElement.cloneNode(false));
}
dest.appendChild(result);
preProcess(doc, childElement, result);
}
}