ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package "XML::DOM::NodeList"
by John Krewson other posts by this author
Jul 25 2007 4:20PM messages near this date
view in the new Beta List Site
Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package "XML::DOM::NodeList" | Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package "XML::DOM::NodeList"
& XSLT Thanks to all that replied to this thread.  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.  I ended up
changing my last code version based upon your XML::DOM example and it works
like a charm.  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.  I have to be very careful about what
gets updated, as it could break more than it fixes;  that's the biggest
reason I kept trying to stick with XML::DOM 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 <rrathmann@[...].net>  wrote:
> 
>   As Bjoern was alluding to, using XML::LibXML is much preferred over using
>  XML::DOM (which hasn't been updated in a couple of years, unlike
>  XML::LibXML).  However, both provide you DOM-based parsing (XML::LibXMLalso supports some 
additional XPath functionality with its findXXX()
>  methods, which is another bonus over XML::DOM), 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
>      # taking a node from one XML document and placing in another
>      $target_xml->importNode($article);
> 
>      # add the "imported" node as the last child of the "articles" node (
>  i.e. "document node")
>      $target_xml->documentElement()->appendChild($article);
>  }
> 
>  # output your final results...again I leave this to you
>  ### end code ###
> 
>  If you MUST use XML::DOM (e.g., you can't get XML::LibXML installed, but,
>  judging by the reference in your previous post, I'm assuming that isn't the
>  case for you), you can achieve the same with similar but slightly different
>  code:
> 
>  ### begin code ####
>  #!/usr/bin/perl
>  use XML::DOM;
> 
>  # 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->cloneNode(1);
>      # need to use setOwnerDocument() to transfer the clone to the target
>  doc
>      $clone->setOwnerDocument($target_xml);
> 
>      # add the "imported" clone as the last child of the "articles" node (
>  i.e. "document node")
>      $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::LibXMLas well...importN
ode() just saves you some typing.)
> 
>  HTH,
>  Richard
> 
> 
> 
>  John Krewson wrote:
> 
>  Bjoern,
> 
>  This is my first time using Perl to work with XML in this way, so my
>  reasoning for using XML::DOM 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>
>  then
>  paste in between <Articles></Articles> in another file.
> 
>  Thanks very much for your response.
> 
>  On 7/25/07, Bjoern Hoehrmann <derhoermi@[...].net> 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::DOM::NodeList"
>  >
>  > If you have XML::LibXML I don't think there is any reason to use
>  > XML::DOM. 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:bjoern@[...].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-XML@[...].com
>  To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
> 
> 
Thread:
John Krewson
Bjoern Hoehrmann
John Krewson
Richard E. Rathmann
John Krewson
James Woodworth
John Krewson

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved