473,567 Members | 3,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Document.import Node(Node,boole an) - what supports it?

The DOM API has included public Node importNode(Node ,boolean) as a method
of the Document interface for a long time. Does anything actually
implement it? Xerces 2 is giving me:

org.w3c.dom.DOM Exception: NOT_SUPPORTED_E RR: The implementation does not
support the requested type of object or operation.
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown
Source)
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown
Source)
at
uk.co.weft.domu til.MaybeParseG enerator.maybeP arse(MaybeParse Generator.java: 183)

This is so whether the node I'm trying to import is an
org.apache.xerc es.dom.Deferred ElementImpl (i.e. parsed with Xerces) or a
org.apache.crim son.tree.Elemen tNode (i.e. parsed with Crimson).

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
Ye hypocrites! are these your pranks? To murder men and give God thanks?
Desist, for shame! Proceed no further: God won't accept your thanks for
murther
-- Robert Burns, 'Thanksgiving For a National Victory'

Mar 16 '07 #1
10 8781
Simon Brooke wrote:
The DOM API has included public Node importNode(Node ,boolean) as a method
of the Document interface for a long time. Does anything actually
implement it?
Certainly should work; I wrote Xerces' first implementation of that
function, and in fact was one of those who lobbied the DOM WG to include
it in the standard. If the node being imported properly implements the
DOM APIs, and the implementation being imported into doesn't have some
reason for blocking this (eg, that it's specifically a read-only DOM,
such as the DOM view of Xalan's internal data model), the function
should work. It isn't rocket science, after all; it's just a tree-walker
feeding a tree-builder.

I have to believe the problem resides in something you haven't told us.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 16 '07 #2
in message <iJ************ *************** ***@comcast.com >, Joe Kesselman
('k************ *@comcast.net') wrote:
Simon Brooke wrote:
>The DOM API has included public Node importNode(Node ,boolean) as a
method of the Document interface for a long time. Does anything actually
implement it?

Certainly should work; I wrote Xerces' first implementation of that
function, and in fact was one of those who lobbied the DOM WG to include
it in the standard. If the node being imported properly implements the
DOM APIs, and the implementation being imported into doesn't have some
reason for blocking this (eg, that it's specifically a read-only DOM,
such as the DOM view of Xalan's internal data model), the function
should work. It isn't rocket science, after all; it's just a tree-walker
feeding a tree-builder.

I have to believe the problem resides in something you haven't told us.
OK, then I have to believe that, too. Furthermore, this is another of the
bits of my code that have been around for a long time (since 2003 in this
case), and I'm sure it used to work (but it may only ever have worked with
Crimson). I have had occasions in the past where I have inadvertently
depended on bugs in a library, and when that library has been fixed all my
code broke.

If this class fails, it returns a text node with a 'flat' representation of
the embedded markup. Looking at the production server logs I see that it
has been intermittently failing in this way for some time, but that the
failure simply has not been noticed. The failure on the production servers
is different from the failure on the development server, I'll detail that
difference below. The production severs use Crimson to parse, but Xerces
to construct documents - I can't remember why, but probably just an
oversight.

The class in question is:

//*************** *************** *************** *************** ***********\
// *
// MaybeParseGener ator.java *
// *
// Author: Simon Brooke *
// Created: 17th January 2003 *
// $Revision: 1.7.4.3 $; $Date: 2006/09/04 13:45:54 $ *
// *
//*************** *************** *************** *************** ***********/
package uk.co.weft.domu til;

import org.w3c.dom.Doc ument;
import org.w3c.dom.Nod e;

import org.xml.sax.Inp utSource;

import java.io.StringR eader;

import javax.xml.parse rs.DocumentBuil der;

import uk.co.weft.htfo rm.ResourceCons umerImpl;
/*
* $Log: MaybeParseGener ator.java,v $
* Revision 1.7.4.3 2006/09/04 13:45:54 simon
* Added more debugging output. Have an intermittent bug in PRES which may
originate here.
*
* Revision 1.7.4.2 2005/12/30 16:54:00 simon
* EkitWidget now working remarkably well. Still some tidying up to do.
*
* Revision 1.7.4.1 2005/12/23 10:48:33 simon
* Brute force tidy up after CVS server crash: this time it should work.
*
* Revision 1.7 2005/02/05 17:40:17 simon
* Improved diagnostics on failure
*
* Revision 1.6 2004/07/14 12:52:34 simon
* Final commit for 1.10.0
*
* Revision 1.5 2004/06/17 15:10:38 simon
* Extends ResourceConsume rImpl to gain access to grs, etc
*
* Revision 1.4 2003/10/30 12:40:21 simon
* Added debug flag in domutil classes
*
* Revision 1.3 2003/08/20 09:38:35 simon
* Code cleanup with eclipse; mostly removal of exccessive includes
*
* Revision 1.2 2003/07/09 09:32:07 simon
* Initial work on HTML generation of widgets.
*
* Revision 1.1 2003/02/06 11:22:26 simon
* New superclass for node generators which may want to parse XML text.
*/

/**
* Abstract superclass for TextNodeGenerat or and ElementGenerato r, which
may
* want to parse their content. Parsing is potentially expensive, so if
* you're confident the value won't contain XML markup it may be worth
* setting allowEmbeddeMar kup( false).
*
* @author Simon Brooke
* @version $Revision: 1.7.4.3 $ This revision: $Author: simon $
*/
public abstract class MaybeParseGener ator extends ResourceConsume rImpl
{
//~ Instance fields -----------------------------------------------------

/**
* whether or not I'm in debug mode; if I am I may print debugging
* messages to System.err
*/
protected boolean debug = false;

/** By default we allow embedded markup in children */
protected boolean embeddedMarkup = true;

//~ Constructors --------------------------------------------------------

/**
* Creates a new MaybeParseGener ator object.
*/
public MaybeParseGener ator( )
{
// ...nothing...
}

//~ Methods -------------------------------------------------------------

/**
* whether or not to set debugging mode. If true, the generator _may_
* write debugging messages to System.err
*
* @param debug whether or not to set debugging mode
*
* @since Jacquard 1.10
*/
public void setDebug( boolean debug )
{
this.debug = debug;
}

/**
* Do we allow (and parse for) embedded markup within the value of this
* node? default is we do.
*
* @param allow if true, then allow embedded markup within my value
*/
public void allowEmbeddedMa rkup( boolean allow )
{
embeddedMarkup = allow;
}

/**
* Construct a node representing this value. It's perfectly possible (and
* possibly legitimate) that the value of a child should contain embedded
* markup. If so, try to parse a node out of it.
*
* @param doc the document in which the node is to be created
* @param unparsed the string, possibly with embedded markup, to parse
*
* @exception GenerationExcep tion if parsing fails
*/
protected Node maybeParse( Document doc, String unparsed )
throws GenerationExcep tion
{
Node val = doc.createTextN ode( unparsed ); // safe default

if ( debug )
{
System.err.prin tln( "MaybeParseGene rator.maybePars e: parsing [" +
unparsed + "]" );
}

if ( unparsed != null ) // defensive
{
if ( embeddedMarkup && (
// if we allow embedded markup
unparsed.indexO f( "<" ) -1 ) ) // it looks like markup
{
if ( !unparsed.trim( ).startsWith( "<" ) )
{
// nasty: if it contains markup, but
// isn't contained in markup, the
// parser will barf.
unparsed = "<parsed>" + unparsed + "</parsed>";
}

try
{
DocumentBuilder parser = DOMStub.getPars er( );

if ( parser == null )
{
System.err.prin tln( "Could not initialise XML parser" );
}

InputSource i =
new InputSource( new StringReader( unparsed ) );

// i.setCharacterS tream( new StringReader( unparsed ) );
Document parsed = parser.parse( i );

if ( debug )
{
System.err.prin tln( "Parsed document: " +
parsed.toString ( ) );

if ( parsed != null )
{
Node root = parsed.getDocum entElement( );

if ( root != null )
{
System.err.prin tln( "Root node: (" +
root.getClass( ).getName( ) + "): " +
root.toString( ) );
}
}
}

val = doc.importNode( parsed, true );

if ( debug )
{
System.err.prin tln(
"MaybeParseGene rator.maybePars e: parse successful" );
new Printer( ).print( val, System.err );
}
}
catch ( Exception e )
{
System.err.prin tln(
"MaybeParseGene rator.maybePars e(): Could not parse '" +
unparsed + "'as XML" );
e.printStackTra ce( System.err );
}
}
}

return val;
}
}

/* [end of file] */
What I'm getting in the error stream on the development server is (with
parser unconfigured, i.e. using Tomcat's default, which is Xerces; see
below for Crimson):

ElementGenerato r.generate: attempting to parse <div class="Intro">
Here be dragons!
</div>
MaybeParseGener ator.maybeParse : parsing [<div class="Intro">
Here be dragons!
</div>]
Parsed document: [#document: null]
Root node: (org.apache.xer ces.dom.Deferre dElementImpl): [div: null]
MaybeParseGener ator.maybeParse (): Could not parse '<div class="Intro">
Here be dragons!
</div>'as XML
org.w3c.dom.DOM Exception: NOT_SUPPORTED_E RR: The implementation does not
support the requested type of object or operation.
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown Source)
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown Source)
at
uk.co.weft.domu til.MaybeParseG enerator.maybeP arse(MaybeParse Generator.java: 183)
(with parser configured as org.apache.crim son.tree.DOMImp lementationImpl ):

ElementGenerato r.generate: attempting to parse <div class="Intro">
Here be dragons!
</div>
MaybeParseGener ator.maybeParse : parsing [<div class="Intro">
Here be dragons!
</div>]
Parsed document: org.apache.crim son.tree.XmlDoc ument@e9a0e9a
Root node: <div class="Intro">
Here be dragons!
</div>
MaybeParseGener ator.maybeParse (): Could not parse '<div class="Intro">
Here be dragons!
</div>'as XML
org.w3c.dom.DOM Exception: NOT_SUPPORTED_E RR: The implementation does not
support the requested type of object or operation.
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown Source)
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown Source)
at
uk.co.weft.domu til.MaybeParseG enerator.maybeP arse(MaybeParse Generator.java: 173)
What's showing up in the production server logs is:
(Firstly, evidence that it sometimes does work):
ElementGenerato r.generate: attempting to parse <div
class="Introduc tion"><p>Copies of documentation issued to licensees is
available in this section.</p></div>
ElementGenerato r.generate: attempting to parse Cockle Bags - further
information
(Secondly, evidence that it sometimes doesn't):
ElementGenerato r.generate: attempting to parse <div class="Introduc tion">
Ayrshire and Dumfrieshire Cyclists Association is a regional
association
of cycling clubs within the structure of Scottish Cycling.
</div>
MayberParseGene rator.maybePars e(): Could not parse '<div
class="Introduc tion">
Ayrshire and Dumfrieshire Cyclists Association is a regional
association
of cycling clubs within the structure of Scottish Cycling.
</div>'as XML
java.lang.NullP ointerException
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown
Source)
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown
Source)
at org.apache.xerc es.dom.CoreDocu mentImpl.import Node(Unknown
Source)
at
uk.co.weft.domu til.MaybeParseG enerator.maybeP arse(MaybeParse Generator
..java:163)

I've checked the libraries and the two instances above use the same
versions of the same libraries with the same configuration, so why

<div class="Introduc tion"><p>Copies of documentation issued to licensees is
available in this section.</p></div>

parses successfully and

<div class="Introduc tion">
Ayrshire and Dumfrieshire Cyclists Association is a regional
association
of cycling clubs within the structure of Scottish Cycling.
</div>

fails to parse is frankly baffling me.

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; Let's have a moment of silence for all those Americans who are stuck
;; in traffic on their way to the gym to ride the stationary bicycle.
;; Rep. Earl Blumenauer (Dem, OR)

Mar 16 '07 #3
Just a quick observation: Your "sometimes works" and "sometimes doesn't"
are significantly different:
(Firstly, evidence that it sometimes does work):
ElementGenerato r.generate: attempting to parse <div
class="Introduc tion"><p>Copies of documentation issued to licensees is
available in this section.</p></div>
<divhas a <pchild.

(Secondly, evidence that it sometimes doesn't):
ElementGenerato r.generate: attempting to parse <div class="Introduc tion">
Ayrshire and Dumfrieshire Cyclists Association is a regional
association
of cycling clubs within the structure of Scottish Cycling.
</div>
<divcontains only text. Haven't looked at the code yet, but are you
sure you aren't doing something simple like trying to import the string
value rather than a TextNode object?

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 16 '07 #4
Also: You didn't show us the implementation of DOMStub... but with that
name, I wouldn't be at all surprised if you've got a subset
implementation there.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 17 '07 #5
Well, I've reproduced the error message under Eclipse. Lemme see if I
can reproduce it with a current version of Xerces...

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 17 '07 #6
Oh. That's stupid; I should have remembered this:

http://www.w3.org/TR/2000/REC-DOM-Le...ent-importNode

You're attempting to import a Document node. That's forbidden. Import
its root element instead.

Yes, the error message could have been more helpful. I'd suggest posting
that as a suggestion on the Xerces users mailing list, since I'm not
sure any of the current Xerces maintainers are reading this list.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 17 '07 #7
* Joe Kesselman wrote in comp.text.xml:
>You're attempting to import a Document node. That's forbidden. Import
its root element instead.
Heh, I actually had a quick look into the Xerces source code when I
looked at the question, but that case was the only where the specific
claimed exception would be raised, and Simon said he tried to import
element nodes, so I concluded the issue is too weird to investigate
further...
--
Björn Höhrmann · mailto:bj****@h oehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Mar 17 '07 #8
in message <96************ *************** ***@comcast.com >, Joe Kesselman
('k************ *@comcast.net') wrote:
Oh. That's stupid; I should have remembered this:

http://www.w3.org/TR/2000/REC-DOM-Le...ent-importNode
>
You're attempting to import a Document node. That's forbidden. Import
its root element instead.

Yes, the error message could have been more helpful. I'd suggest posting
that as a suggestion on the Xerces users mailing list, since I'm not
sure any of the current Xerces maintainers are reading this list.
Thank you. I was going to say indignantly 'oh no I don't', but on reading
through my code I see I get the root node of the document... and then
don't use it. Having fixed that, /this/ problem is solved, and I can now
replace vintage Crimson with current Xerces and my code still works.

Still can't get it to work with current Xalan, but that's another set of
problems...

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; Good grief, I can remember when England won the Ashes.
Mar 17 '07 #9
in message <H7************ *************** ***@comcast.com >, Joe Kesselman
('k************ *@comcast.net') wrote:
Also: You didn't show us the implementation of DOMStub... but with that
name, I wouldn't be at all surprised if you've got a subset
implementation there.
No, it just allows me to select and configure the DOMImplementati on I use:

/**
* Should be called before DOMStub is used, but perfectly safe to call
* more than once. If I've already been initialised, don't intialise me
* again.
*
* @param config my configuration
*
* @exception InitialisationE xception if requested DOM implementation
* can't be found
*/
public static void init( Context config ) throws InitialisationE xception
{
String s = config.getValue AsString( "dom_implementa tion_class" );

if ( domImp == null )
{
/* i.e., I have not already been initialised */
try
{
if ( s != null )
{
domImpName = s;
}

domImp =
(DOMImplementat ion) Class.forName( domImpName )
.newInstance( );
}
catch ( Exception any )
{
throw new InitialisationE xception( "Could not find DOM " +
"implementa tion " + domImpName );
}
}

Boolean b = config.getValue AsBoolean( "dom_coalescing " );

if ( b != null )
{
dbf.setCoalesci ng( b.booleanValue( ) );
}

b = config.getValue AsBoolean( "dom_expand_ent ity_references" );

if ( b != null )
{
dbf.setExpandEn tityReferences( b.booleanValue( ) );
}

b = config.getValue AsBoolean( "dom_ignore_com ments" );

if ( b != null )
{
dbf.setIgnoring Comments( b.booleanValue( ) );
}

b = config.getValue AsBoolean( "dom_ignore_whi tespace" );

if ( b != null )
{
dbf.setIgnoring ElementContentW hitespace( b.booleanValue( ) );
}

b = config.getValue AsBoolean( "dom_namespace_ aware" );

if ( b != null )
{
dbf.setNamespac eAware( b.booleanValue( ) );
}

b = config.getValue AsBoolean( "dom_validating " );

if ( b != null )
{
dbf.setValidati ng( b.booleanValue( ) );
}
}
}
--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

X-no-archive: No, I'm not *that* naive.

Mar 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3653
by: Daniel Frey | last post by:
Hello, I've got a problem. Either I have missed the most important point or it really does not work: I want to use the document function with an absolut path on WindowsXP Professional and Saxon 8.1. However, retrieving a subnode of the documents content is not possible. Inserting a whole document result set is functional, subset not....
4
2030
by: Dante | last post by:
Hello. I have a Javascript that gets data from an XML document and displays it through javascript. The problem is that when I do dcfile.getElementsByTagName("subhead").firstChild.nodeName all I get is #text. Can you tell me why? Here is my code: /* The Dante-Cubed Standardized Markup Language Javascript API ------ Written by Sean M....
1
1442
by: Omkar Singh | last post by:
I have want to read a xml file node by node. How can I do it? I am using following code to load xml file. XmlDocument doc = new XmlDocument(); doc.Load(fileName);
6
8383
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete event using: Dim mNewDoc As mshtml.IHTMLDocument3 mNewDoc = NewDoc(axWebBrowser1.Document) Private Function NewDoc(ByVal mInputDoc As...
1
1388
by: ppcguy | last post by:
can getElementById() be called off a non-document node. on page load temp = document.body; document.body = set-to-blank... var elem = temp.getElementById("my_id");
3
8114
by: bhu | last post by:
Hi i am trying some things in XML for webservices but got stuck with the xml Creation. This is the First Sample C# Code XmlDocument oInvDocument = new XmlDocument(); oNode= oInvDocument.CreateNode(XmlNodeType.Element,"InventoryUpdateBatch",""); oInvDocument.AppendChild(oNode);
1
1743
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, Is there an easy way to get an XPath expression (in string format) to an arbitrary node in a XmlDocument. E.g.: string s = node.XpathToString(). s = "myRoot/foo/myChild/text()"
0
1804
by: jazzygirl | last post by:
I'm fairly new to vb.net (self-taught and realizing how much I DON'T know). I need to create an XML output file (I'm quite ignorant in this area as well!). I used an XMLDataDocument and Xml.XmlTextWriter, creating each element node since the agency I'm sending the file to is very specific about the format. How do I specify an...
0
7686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.