Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use org.w3c.xml.Document#importNode

Tags:

java

xml

Please note, SOAPHeader is extends Node and Element interfaces:

 Document docToAppend= getDoc();
 final SOAPHeader soapHeader = getSoapHeader();
 final Node importNode = soapHeader.getOwnerDocument().importNode(docToAppend.cloneNode(true), true);
 soapHeader.appendChild(importNode);

i.e. i want to append docToAppendto soapHeader node.

But it fails with exception:

Caused by: org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.

I think my code is incorrect.

like image 643
WelcomeTo Avatar asked Apr 26 '14 23:04

WelcomeTo


People also ask

What is org w3c DOM Document?

Package org. w3c. dom Description. Provides the interfaces for the Document Object Model (DOM) which is a component API of the Java API for XML Processing. The Document Object Model Level 2 Core API allows programs to dynamically access and update the content and structure of documents.

How are documents accessed and manipulated in XML?

The XML DOM defines a standard way for accessing and manipulating XML documents. It presents an XML document as a tree-structure. Understanding the DOM is a must for anyone working with HTML or XML.

What do you use the XML DOM to do?

The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access.

How DOM based XML processing is performed?

The XML Document Object Model (DOM) treats XML data as a standard set of objects and is used to process XML data in memory. The System. Xml namespace provides a programmatic representation of XML documents, fragments, nodes, or node-sets.


2 Answers

Had the same error NOT_SUPPORTED_ERR.

DOMResult dom = new DOMResult(); 
getTransformer().transform(new StAXSource(xmlr), dom);
Node node = dom.getNode();
document.appendChild(document.importNode(node, true));  // <---- Error

Cause

Found that trying to add document instead of element by checking the type of the node.

System.out.println("Node type is [" + dom.getNode().getNodeType() + "]");
----
Node type is [9]  <---- DOCUMENT_NODE

Fix

Get the first child of the document node.

node = dom.getNode().getFirstChild();
System.out.println("Node type is [" + node.getNodeType() + "]");
document.appendChild(document.importNode(node, true));
----
Node type is 1 <---- ELEMENT_NODE

Reference

DOCUMENT_NODE and ELEMENT_NODE values are specified in JAVA API Constant Field Values.

like image 193
mon Avatar answered Sep 28 '22 09:09

mon


SOAPHeader object can have only SOAPHeaderElement objects as its immediate children.

like image 37
Developer Marius Žilėnas Avatar answered Sep 28 '22 07:09

Developer Marius Žilėnas