Thanks for attention to my novice self-educational questions that bare no practical sence. I attach my code and jar. I have no source (perhaps, you never include it together with jars, and I need to compile the hole tree myself?). I know not if it preserves line number after decompilation.
The essential lines of my code are ... NodeContainer nodeContainer = (NodeContainer) documentImpl; ... try { newChild = nodeContainer.appendChild (newChild); // here it fails !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } catch (DOMException e) { show (e); } println ("newChild = " + newChild); deep = false; // fails for true !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cloneNode = nodeContainer.cloneNode (deep); println ("cloneNode = " + cloneNode); ... -------------------------------- Jeffrey Rodriguez wrote: > Mr. Garbuzov, a question first. > What is your nodeContainer variable? I know that it is a class that extends > abstract class NodeContainer but which one: DocumentImpl, ElementImpl,etc. > > I am not able to reproduce your problem with the information that you > provided. > > A ClassCastException is a runtime exception thrown when a program > attemts to cas an instance of a class to another and that cast is > not allowed. -- Boris Garbuzov. Mailing address: Box 715, Seattle, Washington, 98111-0715, USA. E-mail: [EMAIL PROTECTED], [EMAIL PROTECTED] Telephone: 1(206)781-5165 (home), 1(206)576-4549 (office). Resedential address: 139 NW 104 Street, Seattle, 98177, Wa, USA
package com.keystrokenet.loanproduct.xml.test; import java.io.File; import java.io.Reader; import java.io.FileReader; import java.io.Writer; import java.io.FileWriter; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.StringWriter; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.InputSource; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Element; import org.w3c.dom.DocumentFragment; import org.w3c.dom.Text; import org.w3c.dom.Comment; import org.w3c.dom.CDATASection; import org.w3c.dom.DOMException; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Attr; import org.w3c.dom.EntityReference; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.NamedNodeMap; import org.apache.xerces.dom.DocumentImpl; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.apache.xml.serialize.BaseMarkupSerializer; import org.apache.xml.serialize.Serializer; import org.apache.xml.serialize.DOMSerializer; import org.xml.sax.DocumentHandler; import org.xml.sax.ContentHandler; import org.w3c.dom.traversal.TreeWalker; import org.apache.xerces.dom.TreeWalkerImpl; import org.w3c.dom.traversal.NodeFilter; import org.apache.xerces.dom.NodeIteratorImpl; import org.apache.xerces.dom.NodeContainer; import com.keystrokenet.testutil.Test; /** @author Boris @since 14Mar2000 @genesis My first XML code made out of Peter's. */ public class Lab extends Test { // fields /** Document object can be formed out of HTML */ private static final String HTML_FILE = "data/name.html"; // methods /** Entry point. */ public void executeTestBody() throws Exception { File file = new File (XmlFileBasic.DEFAULT_XML_FILE_NAME); //File file = new File (HTML_FILE); Reader reader = new FileReader (file); InputSource inputSource = new InputSource (reader); DOMParser parser = new DOMParser(); /* parser.setFeature ( "http://xml.org/sax/features/validation", true ); */ parser.parse (inputSource); Document document = parser.getDocument(); println ("document = " + document); // document methods DocumentType documentType = document.getDoctype(); println ("documentType = " + documentType); DOMImplementation domImplementation = document.getImplementation(); println ("domImplementation = " + domImplementation); Element rootElement = document.getDocumentElement(); println ("rootElement = " + rootElement); String tagName = "huynya"; Element createdElement = document.createElement (tagName); println ("createdElement = " + createdElement); println ("Can I reparse the document somehow? Parser can take document argument? Automatically reparsed?"); DocumentFragment documentFragment = document.createDocumentFragment(); println ("documentFragment = " + documentFragment); String data = "dataForTheNode"; Text text = document.createTextNode (data); println ("text = " + text); String commentString = "commentString"; Comment comment = document.createComment (commentString); println ("comment = " + comment); String cdataSectionString = "cdataSectionString"; try { CDATASection cdataSection = document.createCDATASection (cdataSectionString); println ("cdataSection = " + cdataSection); } catch (DOMException e) { println ("Perhaps, document is HTML."); e.printStackTrace(); } println ("Strange. Id does not throw exception for HTML document."); String target = "ProcessingInstructionTarget"; String processingInstructionData = "processingInstructionData"; ProcessingInstruction processingInstruction = document.createProcessingInstruction (target, processingInstructionData); println ("processingInstruction = " + processingInstruction); String attributeName = "attributeName"; println ("Before creating attrubute: document = " + document); Attr attr = document.createAttribute (attributeName); println ("attr = " + attr); String entityName = "entityName"; EntityReference entityReference = document.createEntityReference (entityName); println ("entityReference = " + entityReference); tagName = "tagName"; NodeList nodes = document.getElementsByTagName (tagName); println ("nodes = " + nodes); println ("Node"); println ("Fields of Node"); println ("Node.ATTRIBUTE_NODE = " + Node.ATTRIBUTE_NODE); println ("Node.CDATA_SECTION_NODE = " + Node.CDATA_SECTION_NODE); println ("Node.COMMENT_NODE = " + Node.COMMENT_NODE); println ("Node.DOCUMENT_FRAGMENT_NODE = " + Node.DOCUMENT_FRAGMENT_NODE); println ("Node.DOCUMENT_NODE = " + Node.DOCUMENT_NODE); println ("Node.DOCUMENT_TYPE_NODE = " + Node.DOCUMENT_TYPE_NODE); Node documentNode = (Node) document; println ("documentNode = " + documentNode); println ("Methods of Node"); String nodeName = documentNode.getNodeName(); println ("nodeName = " + nodeName); String nodeValue = documentNode.getNodeValue(); println ("nodeValue = " + nodeValue); String newNodeValue = "newNodeValue"; try { documentNode.setNodeValue (newNodeValue); } catch (DOMException e) { println ("Lowful error: " + e); e.printStackTrace(); } short nodeType = documentNode.getNodeType(); println ("nodeType = " + nodeType); Node parentNode = documentNode.getParentNode(); println ("parentNode = " + parentNode); NodeList nodeList = documentNode.getChildNodes(); println ("nodeList = " + nodeList); Node firstChild = documentNode.getFirstChild(); println ("firstChild = " + firstChild); Node lastChild = documentNode.getLastChild(); println ("lastChild = " + lastChild); Node previousSibling = documentNode.getPreviousSibling(); println ("previousSibling = " + previousSibling); Node nextSibling = documentNode.getNextSibling(); println ("nextSibling = " + nextSibling); NamedNodeMap attributes = documentNode.getAttributes(); println ("attributes = " + attributes); println ("Before getOwnerDocument():"); println ("document = " + document); Document ownerDocument = documentNode.getOwnerDocument(); println ("ownerDocument = " + ownerDocument); Node refChild = firstChild; Node newChild = text; println ("newChild = " + newChild); try { newChild = documentNode.insertBefore (newChild, refChild); } catch (DOMException e) { println ("Lawful failure: " + e); } Node oldChild = firstChild; try { newChild = documentNode.replaceChild (newChild, oldChild); } catch (DOMException e) { show (e); } try { newChild = documentNode.appendChild (newChild); } catch (DOMException e) { show (e); } boolean hasChildNodes = documentNode.hasChildNodes(); println ("hasChildNodes = " + hasChildNodes); boolean deep = true; Node cloneNode = documentNode.cloneNode (deep); println ("cloneNode = " + cloneNode); println ("Attr's API"); println ("attr = " + attr); String attrName = attr.getName(); println ("attrName = " + attrName); boolean isSpecified = attr.getSpecified(); println ("isSpecified = " + isSpecified); String attrValue = attr.getValue(); println ("attrValue = " + attrValue); String newAttrValue = "newAttrValue"; attr.setValue (newAttrValue); println ("attr = " + attr); println ("DOMImplementation's API"); println ("domImplementation = " + domImplementation); String feature = "feature"; String version = "version"; boolean hasFeature = domImplementation.hasFeature (feature, version); println ("hasFeature = " + hasFeature); println ("NodeList's API"); tagName = "product"; println ("document = " + document); NodeList productNodes = document.getElementsByTagName (tagName); println ("productNodes = " + productNodes); int length = productNodes.getLength(); println ("length = " + length); int extra = 2; println ("extra = " + extra); for (int itemIndex = 0; itemIndex < length + extra; itemIndex++) { Node item = productNodes.item (itemIndex); println ("item [" + itemIndex + "] = " + item); } println ("NamedNodeMap's API"); Node firstProductItem = productNodes.item (0); NamedNodeMap namedNodeMap = attributes = firstProductItem.getAttributes(); println ("namedNodeMap = " + namedNodeMap); String attrubuteName = "id"; Node namedItem = namedNodeMap.getNamedItem (attrubuteName); println ("namedItem = " + namedItem); /* java.lang.ClassCastException: org.apache.xerces.dom.TextImpl at org.apache.xerces.dom.NamedNodeMapImpl.setNamedItem(NamedNodeMapImpl.java:304) at com.keystrokenet.loanproduct.xml.test.Lab.executeTestBody(Lab.java:265) */ println ("The following failure is not totally lawful. I do not understand it."); try { Node existingNode = namedNodeMap.setNamedItem (newChild); println ("existingNode = " + existingNode); } catch (Exception e) { show (e); } nodeName = namedItem.getNodeName(); Node removedNode = namedNodeMap.removeNamedItem (nodeName); println ("removedNode = " + removedNode); length = namedNodeMap.getLength(); println ("length = " + length); extra = 2; println ("extra = " + extra); for (int itemIndex = 0; itemIndex < length + extra; itemIndex++) { Node itemByIndex = namedNodeMap.item (itemIndex); println ("itemByIndex [" + itemIndex + "] = " + itemByIndex); } println ("Node's API = DocumentFragment's"); println ("DocumentType's API"); println ("documentType = " + documentType); String documentTypeName = documentType.getName(); println ("documentTypeName = " + documentTypeName); NamedNodeMap entities = documentType.getEntities(); println ("entities = " + entities); NamedNodeMap notations = documentType.getNotations(); println ("notations = " + notations); println ("Node's API = EntityReference's"); println ("Entity API"); int entitiesLength = entities.getLength(); println ("entitiesLength = " + entitiesLength); println ("Since it is empty, I do not know where to take an instance."); /* public java.lang.String getPublicId() public java.lang.String getSystemId() public java.lang.String getNotationName() */ println ("ProcessingInstruction's API"); println ("processingInstruction = " + processingInstruction); String processingInstructionTarget = processingInstruction.getTarget(); println ("processingInstructionTarget = " + processingInstructionTarget); processingInstructionData = processingInstruction.getData(); println ("processingInstructionData = " + processingInstructionData); processingInstruction.setData (processingInstructionData); println ("processingInstruction = " + processingInstruction); println ("Start testing to create and print the document"); File newFile = new File ("data/newFile.xml"); Writer writer = new FileWriter (newFile); OutputFormat outputFormat = new OutputFormat(); println ("Created default outputFormat = " + outputFormat); Document newDocunent = new DocumentImpl(); println ("Created default newDocunent = " + newDocunent); XMLSerializer xmlSerializer = new XMLSerializer (writer, outputFormat); println ("xmlSerializer = " + xmlSerializer); println ("Upcast the serializer and test the methods of the super first."); BaseMarkupSerializer baseMarkupSerializer = (BaseMarkupSerializer) xmlSerializer; println ("baseMarkupSerializer = " + baseMarkupSerializer); println ("Upcast again and test just interface's methods. Start with Serializer."); Serializer serializer = (Serializer) baseMarkupSerializer; println ("serializer = " + serializer); File newByteFile = new File ("data/newByteFile.xml"); OutputStream output = new FileOutputStream (newByteFile); println ("output = " + output); serializer.setOutputByteStream (output); println ("serializer = " + serializer); File newCharFile = new File ("data/newCharFile.xml"); Writer charWriter = new FileWriter (newCharFile); serializer.setOutputCharStream (charWriter); println ("serializer.setOutputCharStream (charWriter); OK"); serializer.setOutputFormat (outputFormat); println ("serializer.setOutputFormat (outputFormat); OK"); DocumentHandler documentHandler = serializer.asDocumentHandler(); println ("documentHandler = " + documentHandler); ContentHandler contentHandler = serializer.asContentHandler(); println ("contentHandler = " + contentHandler); println ("(serializer == documentHandler) = " + (serializer == documentHandler)); println ("(contentHandler == documentHandler) = " + (contentHandler == documentHandler)); DOMSerializer domSerializer = serializer.asDOMSerializer(); println ("domSerializer = " + domSerializer); println ("(serializer == domSerializer) = " + (serializer == domSerializer)); println ("Test DOMSerializer's API"); //domSerializer.serialize (rootElement); /* java.lang.ClassCastException: org.apache.xerces.dom.TextImpl at org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:577) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:827) at org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:608) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:827) at org.apache.xml.serialize.BaseMarkupSerializer.serialize(BaseMarkupSerializer.java:373) at com.keystrokenet.loanproduct.xml.test.Lab.executeTestBody(Lab.java:397) */ domSerializer.serialize (createdElement); println ("newFile.length() = " + newFile.length()); println ("newByteFile.length() = " + newByteFile.length()); println ("newCharFile.length() = " + newCharFile.length()); // fails likewise //domSerializer.serialize (document); domSerializer.serialize (documentFragment); println ("serialize newly created documentFragment - OK"); println ("Test org.xml.sax.DocumentHandler's API"); println ("documentHandler = " + documentHandler); documentHandler.startDocument(); println ("documentHandler.startDocument() - OK"); documentHandler.endDocument(); println ("documentHandler.endDocument() - OK"); char [] ch = {'a', 'b'}; int start = 0; length = 1; documentHandler.characters (ch, start, length); println ("documentHandler.characters (ch, start, length); - OK"); documentHandler.ignorableWhitespace (ch, start, length); println ("documentHandler.ignorableWhitespace (ch, start, length); - OK"); documentHandler.processingInstruction (target, data); println ("documentHandler.processingInstruction (target, data); - OK"); String unexistingName = "unexistingName"; // in new version of Xerces the bug is fixed. //documentHandler.endElement (unexistingName); println ("documentHandler.endElement (unexistingName); - OK"); // Now that the document is created we need to *serialize* it OutputFormat format = new OutputFormat (document, "8859_1", true); format.setLineSeparator ("\r\n"); format.setLineWidth (72); format.setDoctype (null, "fibonacci.dtd"); xmlSerializer = new XMLSerializer (System.out, format); Element root = document.createElement ("Pizda"); xmlSerializer.serialize (root); /* public void setDocumentLocator(Locator locator) public void startElement(java.lang.String name, AttributeList atts) */ format = new OutputFormat (document); StringWriter stringOut = new StringWriter(); XMLSerializer serial = new XMLSerializer (stringOut, format); //serial.serialize (document.getDocumentElement()); serial.serialize (root); println ("stringOut.toString() = \n" + stringOut.toString()); println ("org.w3c.dom.traversal package."); println ("org.w3c.dom.traversal.NodeFilter's API"); println ("NodeFilter.FILTER_ACCEPT = " + NodeFilter.FILTER_ACCEPT); println ("NodeFilter.FILTER_REJECT = " + NodeFilter.FILTER_REJECT); println ("NodeFilter.FILTER_SKIP = " + NodeFilter.FILTER_SKIP); println ("NodeFilter.SHOW_ALL = " + NodeFilter.SHOW_ALL); println ("NodeFilter.SHOW_ATTRIBUTE = " + NodeFilter.SHOW_ATTRIBUTE); println ("NodeFilter.SHOW_CDATA_SECTION = " + NodeFilter.SHOW_CDATA_SECTION); println ("NodeFilter.SHOW_COMMENT = " + NodeFilter.SHOW_COMMENT); println ("NodeFilter.SHOW_DOCUMENT = " + NodeFilter.SHOW_DOCUMENT); println ("NodeFilter.SHOW_DOCUMENT_FRAGMENT = " + NodeFilter.SHOW_DOCUMENT_FRAGMENT); println ("NodeFilter.SHOW_DOCUMENT_TYPE = " + NodeFilter.SHOW_DOCUMENT_TYPE); println ("NodeFilter.SHOW_ELEMENT = " + NodeFilter.SHOW_ELEMENT); println ("NodeFilter.SHOW_ENTITY = " + NodeFilter.SHOW_ENTITY); println ("NodeFilter.SHOW_ENTITY_REFERENCE = " + NodeFilter.SHOW_ENTITY_REFERENCE); println ("NodeFilter.SHOW_NOTATION = " + NodeFilter.SHOW_NOTATION); println ("NodeFilter.SHOW_PROCESSING_INSTRUCTION = " + NodeFilter.SHOW_PROCESSING_INSTRUCTION); println ("org.w3c.dom.traversal.NodeFilter's API"); NodeFilter nodeFilter = new MyNodeFilter(); int whatToShow = NodeFilter.SHOW_ALL; short accept = nodeFilter.acceptNode (root); println ("org.w3c.dom.traversal.TreeWalker's API"); println ("org.apache.xerces.dom.TreeWalkerImplr's API"); println ("Constructor?"); boolean entityReferenceExpansion = false; TreeWalkerImpl treeWalkerImpl = new TreeWalkerImpl (root, whatToShow, nodeFilter, entityReferenceExpansion); Node walkedRoot = treeWalkerImpl.getRoot(); println ("walkedRoot = " + walkedRoot); whatToShow = treeWalkerImpl.getWhatToShow(); println ("whatToShow = " + whatToShow); nodeFilter = treeWalkerImpl.getFilter(); println ("nodeFilter = " + nodeFilter); nodeFilter = treeWalkerImpl.getFilter(); println ("nodeFilter = " + nodeFilter); boolean isExpandEntityReferences = treeWalkerImpl.getExpandEntityReferences(); println ("isExpandEntityReferences = " + isExpandEntityReferences); Node currentNode = treeWalkerImpl.getCurrentNode(); println ("currentNode = " + currentNode); treeWalkerImpl.setCurrentNode (currentNode); println ("setCurrentNode() OK"); parentNode = treeWalkerImpl.parentNode(); println ("parentNode = " + parentNode); firstChild = treeWalkerImpl.firstChild(); println ("firstChild = " + firstChild); lastChild = treeWalkerImpl.lastChild(); println ("lastChild = " + lastChild); previousSibling = treeWalkerImpl.previousSibling(); println ("previousSibling = " + previousSibling); nextSibling = treeWalkerImpl.nextSibling(); println ("nextSibling = " + nextSibling); Node previousNode = treeWalkerImpl.previousNode(); println ("previousNode = " + previousNode); Node nextNode = treeWalkerImpl.nextNode(); println ("nextNode = " + nextNode); println ("NodeIteratorImpl's API."); println ("Constructor"); DocumentImpl documentImpl = (DocumentImpl) document; NodeIteratorImpl nodeIteratorImpl = new NodeIteratorImpl ( documentImpl, root, whatToShow, nodeFilter, entityReferenceExpansion ); println ("nodeIteratorImpl = " + nodeIteratorImpl); Node rootNode = nodeIteratorImpl.getRoot(); println ("root = " + root); whatToShow = nodeIteratorImpl.getWhatToShow(); println ("whatToShow = " + whatToShow); nodeFilter = nodeIteratorImpl.getFilter(); println ("nodeFilter = " + nodeFilter); boolean expandEntityReferences = nodeIteratorImpl.getExpandEntityReferences(); println ("expandEntityReferences = " + expandEntityReferences); nextNode = nodeIteratorImpl.nextNode(); println ("nextNode = " + nextNode); nextNode = nodeIteratorImpl.previousNode(); println ("previousNode = " + previousNode); nodeIteratorImpl.removeNode (previousNode); println ("previousNode = " + previousNode); nodeIteratorImpl.detach(); println ("nodeIteratorImpl = " + nodeIteratorImpl); println ("NodeContainer's API"); NodeContainer nodeContainer = (NodeContainer) documentImpl; println ("nodeContainer = " + nodeContainer); try { newChild = nodeContainer.appendChild (newChild); } catch (DOMException e) { show (e); } println ("newChild = " + newChild); deep = false; // fails for true cloneNode = nodeContainer.cloneNode (deep); println ("cloneNode = " + cloneNode); hasChildNodes = nodeContainer.hasChildNodes(); println ("hasChildNodes = " + hasChildNodes); NodeList childNodes = nodeContainer.getChildNodes(); println ("childNodes = " + childNodes); firstChild = nodeContainer.getFirstChild(); println ("firstChild = " + firstChild); lastChild = nodeContainer.getLastChild(); println ("lastChild = " + lastChild); length = nodeContainer.getLength(); println ("length = " + length); nodeContainer.normalize(); println ("normalize - OK"); boolean readOnly = true; deep = false; nodeContainer.setReadOnly (readOnly, deep); println ("setReadOnly - OK"); /* */ } }