I have a chunk of xml code with a bunch of elements in it. I need to replace some of the data in the xml with some other values. I can make XML::Simple do this, but it mangles the order. I am now trying to get XML::LibXML to work, but I am having a great deal of trouble.

The part of the xml I am wanting to change looks like this:

<remote name="korg">

I need to be able to change the 'korg' value to something else without totally making a hash out of the document.

What I have tried looks like:

my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); my $root = $doc->getDocumentElement; print $doc->toString; my $query1 = '//remote[@name="korg"]/'; my ($node1) = $doc->findnodes($query1); $node1->setData('other');

This was pulled from a XML::LibXML sample. unfortunatly the sample errors out saying it cannot find the function setData in XML::LibXML::Element.

Any ideas how to make this work? I am not a total Perl newbie, but I am a total XPath newbie.

Thanks!


In reply to How do I get LibXML to replace attribute values? by alanpdx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.