List Info

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




Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package
country flaguser name
United States
2007-07-25 14:49:41
Hello John,
 
i recently started using XML::LibXML.  what i did was made a "deep" clone for a set of elements to repeatedly insert the clone onto the end after some modifications to the attributes.  try this:
 
my $cloned_node = $node->cloneNode( 1 );
...then...
my $last_node = ( $document->findnodes( '/root_node/plot[last()]' ) )[0];
$last_node->addSibling( $cloned_node );
 
reply if you need to insert some whitespace.
 
hth, jim woodworth

John Krewson <jkrooglegmail.com&gt; wrote:
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/

_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

;


Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
Re: Error when attempting to use cloneNode - Can't locate object method "cloneNode" via package
user name
2007-07-25 17:09:31
Thanks for this, but I had already headed down the XML:OM path and am so very close I can smell it, although I will check out XML::LibXML later.&nbsp; I'm not getting to the data from the source file, it seems, as I get the following error, Can't call method "setOwnerDocument" on an undefined value, and my test (if ($node->; getFirstChild->getData eq 'test&#39;) {) is failing.

I stole code from the following post, which is EXACTLy what I"m trying to do, hence I'm fine with using XMLOM at the moment:
http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/469159

And changed it a bit as posted:

my $xp=new XML:OM::Parser;
my $doc=$xp-&gt; parsefile("target_files/target.xml&quot;);
my $docFrom=$xp-> parsefile("source_files/0,,HGTV_1378264,00.xml &quot;);

my $nodes=$docFrom-> getElementsByTagName("Article");
my $snode;
for my $i (0..$nodes-> getLength-1) {
 my $node=$nodes-> item($i);
 if ($node->; getFirstChild->getData eq 'test&#39;) {
   ;  print "tis true!";;
 &nbsp;   print $node-> getFirstChild->getData; #Nothing, I am not hitting what I think should be the first child node from the source
&nbsp; $snode=$node-> getParentNode->cloneNode(&quot;deep";);
 ; last;
&nbsp;}
}

$snode-> setOwnerDocument($doc);
my $sitesnode=$doc-> getElementsByTagName("articles")->item(0);
$sitesnode-> appendChild($snode);

$doc-> printToFile("target_files/final_target.xml");

where my docfrom is this:
<?xml version=&#39;1.0' encoding=&#39;utf-8&#39; ?>
&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">

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

and doc is this:

<;?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">
<articles>
</articles>









On 7/25/07, James Woodworth < woodworthjamesyahoo.com">woodworthjamesyahoo.com> wrote:
Hello John,
 
i recently started using XML::LibXML.  what i did was made a "deep" clone for a set of elements to repeatedly insert the clone onto the end after some modifications to the attributes.  try this:
 
my $cloned_node = $node->cloneNode( 1 );
...then...
my $last_node = ( $document->findnodes( '/root_node/plot[last()]'; ) )[0];
$last_node->addSibling( $cloned_node );
 
reply if you need to insert some whitespace.
 
hth, jim woodworth


John Krewson < jkrooglegmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> jkrooglegmail.com> wrote:
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&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" 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


Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.


_______________________________________________
Perl-XML mailing list
listserv.ActiveState.com"> Perl-XMLlistserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


[1-2]

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