-1

This is the xml that I am returning that I need to parse. The data I need to extract is the attributes 'id' and 'oldID' from CategoryMapping node.

<GetCategoryMappingsResponse 
  xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2017-05-26T13:51:34.492Z</Timestamp>
  <Ack>Success</Ack>
  <Version>989</Version>
  <Build>E989_CORE_API_18131074_R1</Build>
  <CategoryMapping id="183872" oldID="43010"/>
  <CategoryMapping id="183322" oldID="20483"/>
  <CategoryMapping id="1290" oldID="43014"/>
  <CategoryMapping id="183888" oldID="43015"/>
  <CategoryMapping id="48718" oldID="43016"/>
  <CategoryMapping id="183051" oldID="45064"/>

...

  <CategoryMapping id="183174" oldID="83983"/>
  <CategoryMapping id="184437" oldID="15"/>
  <CategoryMapping id="183185" oldID="83984"/>
<CategoryVersion>117</CategoryVersion>
</GetCategoryMappingsResponse>

This is the perl that I have tried but it is not returning all the data

foreach $key (keys $data->{CategoryMapping}) {

    print "($rownum) id=$key ";
    my %new_hash = %{$data->{CategoryMapping}{$key}};
    foreach $key2 (keys %new_hash) {
        print "$key2=$new_hash{$key2}<br/>";
    }
}
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
user3287677
  • 37
  • 2
  • 8
  • 1
    How are you calling XML::Simple? You excluded some code which might be important. – AKHolland May 26 '17 at 15:53
  • *"This is the xml that I am returning that I need to parse"* If you're returning it then you already have the data and there is no need to parse what you have just created? – Borodin May 26 '17 at 17:43
  • 1
    Please have the grace to indent your XML data and Perl code so that it's readable to those who will be answering you question free of charge. Please also read [*How to Edit*](https://stackoverflow.com/editing-help) to help you make sure that you are using Markdown correctly. – Borodin May 26 '17 at 17:49
  • 1
    I've cleaned up your Markdown and fixed your indentation. You're welcome, but please do it yourself next time. – Dave Cross May 27 '17 at 09:50

2 Answers2

-1
foreach my $key(keys $data->{'CategoryMapping'}) {
    print "ID = $key oldID = $data->{'CategoryMapping'}->{$key}->{'oldID'}\n";
}
Andrey
  • 1,808
  • 1
  • 16
  • 28
  • 1
    You can't expect a response to "why the downvote?" questions. Votes are anonymous for a reason, and it is not for you to break that privilege. I always downvote comments like yours as being "not constructive" and as far as I know they are always deleted. Here, I downvoted your answer firstly because the best advice you can give to those burdened with `XML::Simple` is to jettison it altogether, and secondly because any answer *must* include the configuration used. The best configuration isn't the default one, and it depends on the data to be parsed. And that's just one reason why it's awful. – Borodin May 26 '17 at 18:02
  • I am asking the reason for the downvote so that I can improve my answers. This question is not even about XML::Simple. It is about parsing a complicated data structure. It is up for a developer to decide to use or not to use XML::Simple. It is discouraged, but not forbidden. Downvoting a perfectly correct answer is your personal choice I guess. Thank you for explaining the downvote. – Andrey May 26 '17 at 18:42
  • 1
    Please improve your answers by writing what you think is a good solution and observing any criticisms in all questions. You won't improve if you read the response you get and try to rubbish it; that is precisely why no one likes to explain their votes. A question with the subject line **How to parse xml node with 2 attributes using XML::Simple and perl** is about using `XML:Simple`, as you well understood as you used it (implicitly) in your answer. – Borodin May 26 '17 at 18:53
  • 1
    In fact your "solution" is so brief, without any preamble or explanation, any code that will run in isolation, or any sample results, that it is *"very low quality"* anyway. – Borodin May 26 '17 at 18:55
  • Thanks for the answer. You are correct. This wasn't a question about whether or not I should use 'xmlSimple'. – user3287677 May 26 '17 at 19:49
-1

There is only one proper answer to any question that asks how to do something using XML::Simple, and that is that you mustn't even try if you value your life and your sanity

The module's own documentation recommends against using using it, and if you ignore all warnings like this one then no one will want to be responsible for the day, maybe not today, maybe not tomorrow, but soon, that it crashes and burns around your head because someone added an attribute to one of the elements

Please read Why is XML::Simple “Discouraged”?. If you find yourself in disagreement with what is laid out there then you need to form a recluse group of XML::Simple users that fear success more than they fear the module itself

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Thanks for your comment. I switched to xmlLib – user3287677 May 26 '17 at 19:47
  • @user3287677: Then why have you accepted the answer from **Andrey**, which is a poor answer anyway, and simply attempts to advise you how to use the inferior module. – Borodin May 27 '17 at 11:05