List Info

Thread: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package "XML




Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package "XML
user name
2007-07-25 13:35:09
Greetings,

I'm pulling my hair out with this one as it seems to me it should work based upon the examples I have found.  I'm worried that I need to update a module but I don't have the rights to do so - it would take longer than the timeline on this particular task )-:  I wonder if someone might have a sample, full script that uses cloneNode so I can check to see if I might be missing something?

I am running the following version of Perl:
This is perl, version 5.005_03 built for sun4-solaris

I am getting the following error when attempting to use cloneNode, in order to clone a node and then paste this node into a target file:
Can't locate object method "cloneNode" via package "XML:OM::NodeList"

Here are the related modules I am including:

use XML::LibXSLT;
use XML::LibXML;
use XML:OM;
use XML::Parser;

And here is the tiny code block that is driving me nuts:

#### Source
    my $parser = XML:OM::Parser->new();  
    my $source_xml  = $parser->parsefile($article_xml);
   ; my $target_xml  = $parser->parsefile($article_xml);
   
    #### Try the cloning technique
  ;  my $article = $source_xml->getElementsByTagName('Article');
    my $clone = $article->cloneNode("deep"); # do a deep clone

Here is the source XML:
?xml version='1.0' encoding='utf-8' ?>
&nbsp; &nbsp; &nbsp; &nbsp; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"; "http://w
ww.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

&nbsp; &nbsp; &nbsp;   <Article>
 ; &nbsp; &nbsp; &nbsp; <id>1378264</id><section>AH RECIPES OTHER</section><create_date>Sep 05 2
002 05:28:12:000AM</create_date&gt;<last_updated_date>May 07 2007 06:41:54:000PM</l
ast_updated_date><publication_date&gt;May 24, 2002</publication_date><;headline&gt;Cook
ing with Garlic</headline&gt;<url&gt;http://www.hgtv.com/hgtv/ah_recipes_other/article
/0,1801,HGTV_3186_1378264,00.html&lt;/url><copyright>Copyright 2007, </copyright>
&nbsp; &nbsp;   ;  </Article>

Here is the target:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"; "http://www.w3.or
g/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<articles>
&lt;article version=&quot; 1.1&quot; article_xml_create_date="July 09 2007 04:00:00:000PM"&gt;
</articles>




Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package
country flaguser name
Germany
2007-07-25 13:40:19
* John Krewson wrote:
>I am getting the following error when attempting to use
cloneNode, in order
>to clone a node and then paste this node into a target
file:
>Can't locate object method "cloneNode" via
package "XML:OM::Node
List"

If you have XML::LibXML I don't think there is any reason to
use
XML:OM. As
for your problem, getElementsByTagName returns a node
list (zero or more nodes); you are trying a method for a
single
node on a list of nodes, you have to clone the nodes one by
one.
Not that I am sure why you are trying to clone them in the
first
place.
-- 
Björn Höhrmann · mailto:bjoernhoehrmann.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/ 
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package
user name
2007-07-25 13:53:39
Bjoern,

This is my first time using Perl to work with XML in this way, so my reasoning for using XML:OM is simply based upon some examples I have found. Do you think it would be better to use XML::LibXML when making a copy of data in one file to paste into another?

My thought was that cloning the node would be easiest way to simple grab all the content between <Article></Article> so that this data could be pasted into another XML file. ; Basically, I have a lot of 'stand alone' files that contain content, and I need to consolidate that content into one main XML file.

Ex:
a = clone of <Article>nodes between<;/Article&gt;
then
paste in between <Articles></Articles>; in another file.

Thanks very much for your response.

On 7/25/07, Bjoern Hoehrmann < derhoermigmx.net">derhoermigmx.net&gt; wrote:
* John Krewson wrote:
>;I am getting the following error when attempting to use cloneNode, in order
>to clone a node and then paste this node into a target file:
>Can't locate object method "cloneNode" via package "XML:OM::NodeList"

If you have XML::LibXML I don't think there is any reason to use
XML:OM. As for your problem, getElementsByTagName returns a node
list (zero or more nodes); you are trying a method for a single
node on a list of nodes, you have to clone the nodes one by one.
Not that I am sure why you are trying to clone them in the first
place.
--
Björn Höhrmann · mailto: bjoernhoehrmann.de">bjoernhoehrmann.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/

Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package
user name
2007-07-25 18:19:48
Thanks to all that replied to this thread.&nbsp; Basic stuff for some I'm sure, but at first it's frustrating to be 'almost' there.

Thanks Richard for taking the time to provide a killer example.&nbsp; I ended up changing my last code version based upon your XML:OM example and it works like a charm.&nbsp; I'm comparing now to see exactly where I was going wrong.

I kept getting some errors when attempting to use XML::LibXML, so after this project is finished I will go back and figure out if it was my stuff or possibly some out of date modules.&nbsp; I have to be very careful about what gets updated, as it could break more than it fixes;&nbsp; that's the biggest reason I kept trying to stick with XML:OM for now, but I will implement use of LibXML, and several others such as Twig, in the future.

Thanks again so very much!

On 7/25/07, Richard E. Rathmann < rrathmanncomcast.net">rrathmanncomcast.net> wrote:
As Bjoern was alluding to, using XML::LibXML is much preferred over using XML:OM (which hasn't been updated in a couple of years, unlike XML::LibXML).  However, both provide you DOM-based parsing (XML::LibXML also supports some additional XPath functionality with its findXXX() methods, which is another bonus over XML:OM), and for the most part can be used interchangeably.

To accomplish with XML::LibXML what I believe you're trying to do:

### begin code ####
#!/usr/bin/perl
use XML::LibXML;

# setup parser and parse your respective documents...I'll leave this to you

my articles = $source_xml->getElementsByTagName('Article');
for my $article (articles) {
   ; # importNode() will implicitly deep-clone the node, but is required for
 &nbsp;  # taking a node from one XML document and placing in another
  ;  $target_xml->importNode($article);   ;

&nbsp; &nbsp; # add the "imported" node as the last child of the "articles" node (i.e. "document node")
 &nbsp;  $target_xml->documentElement()->appendChild($article);
}

# output your final results...again I leave this to you
### end code ###

If you MUST use XML:OM (e.g., you can't get XML::LibXML installed, but, judging by the reference in your previous post, I'm assuming that isn&#39;t the case for you), you can achieve the same with similar but slightly different code:

### begin code ####
#!/usr/bin/perl
use XML:OM;

# setup parser and parse your respective documents...I'll leave this to you

# getElementsByTagName() returns a node list, so you need to
# loop through the results, even if there is only one
my articles = $source_xml->getElementsByTagName('Article');
for my $article (articles) {
   ; my $clone = $article-&gt;cloneNode(1);
&nbsp; &nbsp; # need to use setOwnerDocument() to transfer the clone to the target doc
&nbsp; &nbsp; $clone->;setOwnerDocument($target_xml);

 &nbsp;  # add the "imported" clone as the last child of the "articles" node (i.e. "document node")
 &nbsp;  $target_xml->getDocumentElement()->appendChild($clone);
}

# output your final results...again I leave this to you
### end code ###

(BTW, you can actually use cloneNode(1)/setOwnerDocument() in XML::LibXML as well...importNode() just saves you some typing.)

HTH,
Richard



John Krewson wrote:
mail.gmail.com" type="cite">
Bjoern,

This is my first time using Perl to work with XML in this way, so my reasoning for using XML:OM is simply based upon some examples I have found. Do you think it would be better to use XML::LibXML when making a copy of data in one file to paste into another?

My thought was that cloning the node would be easiest way to simple grab all the content between <Article></Article> so that this data could be pasted into another XML file. ; Basically, I have a lot of 'stand alone' files that contain content, and I need to consolidate that content into one main XML file.

Ex:
a = clone of <Article>nodes between<;/Article&gt;
then
paste in between <Articles></Articles>; in another file.

Thanks very much for your response.

On 7/25/07, Bjoern Hoehrmann < derhoermigmx.net" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">derhoermigmx.net > wrote:
* John Krewson wrote:
&gt;I am getting the following error when attempting to use cloneNode, in order
&gt;to clone a node and then paste this node into a target file:
&gt;Can't locate object method "cloneNode" via package "XML:OM::NodeList"

If you have XML::LibXML I don't think there is any reason to use
XML:OM. As for your problem, getElementsByTagName returns a node
list (zero or more nodes); you are trying a method for a single
node on a list of nodes, you have to clone the nodes one by one.
Not that I am sure why you are trying to clone them in the first
place.
--
Björn Höhrmann · mailto: bjoernhoehrmann.de" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">bjoernhoehrmann.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/


_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Perl-XMLlistserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )