2

I have an XML string like this:

<DATA>
   <CHILD_DATA ATVAL="value1"/>
   <CHILD_DATA />
</DATA>

The final output I want is:

<DATA>
   <CHILD_DATA ATVAL="value1"/>
   <CHILD_DATA ATVAL="value2"/>
</DATA>

My twig $t is at <DATA>. Now I want to add an attribute to the second <CHILD_DATA />. The attribute is ATVAL="value2". I tried the following:

$t->last_child('CHILD_DATA')->set_att{"ATVAL","value2"};

This didn't work. What's wrong with this code? Is there another way to do this?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ninja
  • 5,082
  • 6
  • 37
  • 59
  • 1
    Did you know that you can edit your own questions? – innaM Aug 12 '09 at 06:29
  • 3
    OK, so... let me let you in on a little secret. "$object->method{"list", "items"}" is not valid Perl syntax. – jrockway Aug 12 '09 at 06:35
  • Could you please tell me how can I do this then? – Ninja Aug 12 '09 at 06:41
  • 1
    By now, you have asked two very closely related questions about XML::Twig and you have received correct answers to both. Yet, you have not accepted an answer for either of your questions. It is good manners to accept an answer (by clicking the check mark next to it) that solves your problem. – Sinan Ünür Aug 12 '09 at 15:09
  • Also, a little patience helps. Maybe you don't like Jon's hint, but just wait: someone else will wake up, grab some coffee, then check Stackoverflow. :) – brian d foy Aug 12 '09 at 16:25

2 Answers2

6

As Jon hinted to you, you have a syntax error in the code you posted. You should have seen a compile error like:

syntax error at test line 18, near "->set_att{" Execution of program.pl aborted due to compilation errors.

However, you might have typed the code into your answer so that code doesn't match what you are actually doing. Always put the actual code into your question rather than re-typing it, and always post a full program when you can. When you post your program, I don't have to start from scratch to debug what I think you might be doing. :)

Here's a program that does what you want:

use XML::Twig;

my $xml = <<'XML';
<DATA>
   <CHILD_DATA ATVAL="value1"/>
   <CHILD_DATA />
</DATA>
XML

my $twig= XML::Twig->new( keep_spaces => 1 );

$twig->parse( $xml );

$twig
    ->root
    ->last_child('CHILD_DATA')
    ->set_att("ATVAL" => "value2");

$twig->flush;
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • I may have misunderstood here but I though you had said in the beginning that the portion of my code: "$t->last_child('CHILD_DATA')->set_att{"ATVAL","value2"};" is syntactically incorrect? But you have used the same bit of code in your solution? – Ninja Aug 12 '09 at 09:25
  • 1
    @Ninja: There is a slight difference... brian is using set_att() but your code and comment shows set_att{} ie. U're using curly braces {} instead of parenthesis () – draegtun Aug 12 '09 at 09:48
  • 3
    @Ninja, I don't know how you are looking at your code, but if you are new I suspect you aren't using a good programmer font that makes it apparent the slight difference between { and (. There are fonts that make the difference very pronounced and easy to spot from a couple feet away. :) – brian d foy Aug 12 '09 at 09:58
  • 1
    Font recommendations: http://stackoverflow.com/questions/4689/recommended-fonts-for-programming – Nifle Aug 12 '09 at 10:58
  • 1
    A style comment: I like to use the fat comma with set_att: set_att(ATVAL => "value2"); Maybe that's just me, but it looks nicer, after all attributes are very similar to hashes (keys must be unique,they are not considered ordered). – mirod Aug 13 '09 at 07:26
2

Just a few thoughts:

  1. Posting the same question multiple times is not going to endear anyone to helping you.

  2. Your code isn't even syntactically correct, so I'm not surprised you're experiencing problems.

  3. Why not include the errors you are getting? Perhaps that might shed some light on the problem?

Ether
  • 53,118
  • 13
  • 86
  • 159
  • 1
    Hi Ether. I had asked a similar question but it's not the same. I want to know a way by which we can set the attributes of an already existing child element as opposed to inserting a new element. In this case, already exists and I need to add the attribute to only the second tag. I also tried the following: $elt = $t->root->last_child('CHILD_DATA'); $elt->set_att("ATVAL","value2"); but it did not help. Please do help me out – Ninja Aug 12 '09 at 06:37
  • "it did not help" is not very specific. What did you try and what were the results? – Ether Aug 12 '09 at 06:52
  • Okay...here is the picture. My twig root variable $t is pointing to ``. I just want to access it's child and set it's attribute as I've said in the question. Please shed light on how this can be done. Forget about what I did, that was wrong anyway. – Ninja Aug 12 '09 at 07:09