0

I have an xml file (client_23.xml) in which I need to add few lines at a particular section basis on what is there in other xml file (abc_lop.xml).

Here is my abc_lop.xml file in which you see I have lot of ClientField lines with name, pptype and dataType in it.

<Hello version="100">
 <DataHolder numberOfFields="67">
  <ClientField name="target" pptype="aligning" dataType="string">
   <Value value="panel_3646"/>
   <Value value="panel_3653"/>
  </ClientField>
  <ClientField name="category_3652_0_count" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.age" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.gender" pptype="aligning" dataType="string">
   <Value value=""/>
   <Value value="F   "/>
   <Value value="NA"/>
  </ClientField>
 </DataHolder>
</Hello>

I need to read this file and extract name from those ClientField lines and if its pptype is not aligning then I need to construct this line for each name: Below is an example for two names, only first value is different and apart from that other two will always be same.

<eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
<eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>

Now if its pptype is aligning, then construct line like this:

<eval>upsert("target", "NA", $calty_strings)</eval>
<eval>upsert("pme.gender", "NA", $calty_strings)</eval>

And all these changes I have to make in client_23.xml file and then make a new file with it so my new file will look like this as an example: I will have a function with name data_values in which I need to add above stuff in the <block> tag as shown below.

    <function>
        <name>data_values</name>
        <variables>
            <variable>
            <name>temp</name>
            <type>double</type>
            </variable>
        </variables>
        <block>
            <eval>temp = 1</eval>
            <eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
            <eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>
            <eval>upsert("target", "NA", $calty_strings)</eval>
            <eval>upsert("pme.gender", "NA", $calty_strings)</eval>             
        </block>
    </function>

This is what I have currently in client_23.xml file so after adding, it will look like above:

    <function>
        <name>data_values</name>
        <variables>
            <variable>
            <name>temp</name>
            <type>double</type>
            </variable>
        </variables>
        <block>
            <eval>temp = 1</eval>
        </block>
    </function>

I came up with below perl script but whenever I try to run it, it doesn't run and I am seeing some errors:

#!/usr/bin/perl
use strict;
use warnings;

use XML::LibXML;

# Open the main XML file and locate the
# <block> element that we need to insert into
#
my $doc = XML::LibXML->load_xml(
    location => 'client_23.xml',
    no_blanks => 1,
);
my $block = $doc->find('/function/block')->get_node(1);

# Open the secondary XML file and find all the <ClientField> elements
# that contain the data we need to insert
#
my $abc = XML::LibXML->load_xml(location => 'abc_lop.xml');

for my $field ( $abc->find('/Hello/DataHolder/ClientField')->get_nodelist ) {

    my ($name, $pptype) = map $field->getAttribute($_), qw/ name pptype /;

    my $text = $pptype eq 'aligning' ?
        sprintf q{upsert("%s", "NA", $calty_strings)}, $name :
        sprintf q{upsert("%s", 0, $calty_doubles)}, $name;

    $block->appendTextChild('eval' , $text);
}

print $doc->toString(2);

I made perl script executable and ran it like this and I got an error. What's wrong?

./tester.perl: line 1: use: command not found
./tester.perl: line 2: use: command not found
./tester.perl: line 4: use: command not found
./tester.perl: line 9: syntax error near unexpected token `('
./tester.perl: line 9: `my $doc = XML::LibXML->load_xml('

Update:-

Use error is fixed now. I am seeing this error:

Can't locate XML/LibXML.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ./tester.perl line 5.
BEGIN failed--compilation aborted at ./tester.perl line 5.

Do I need to install anything?

Borodin
  • 126,100
  • 9
  • 70
  • 144
john
  • 11,311
  • 40
  • 131
  • 251
  • You sure you have the `#!` line correct? It sounds like it's being run as a shell script instead of perl. – Mark Reed Sep 17 '15 at 18:18
  • sorry figure that one out. I was seeing other error after fixing that. Let me edit my question. – john Sep 17 '15 at 18:19
  • How you run your perl script? – Jens Sep 17 '15 at 18:19
  • I have updated my question, that use error was fixed. I run it like this `/testscript.pl` – john Sep 17 '15 at 18:20
  • 2
    Please don't change your question like that after you've already gotten answers; it invalidates the answers and can be confusing for future visitors, since some answers will refer to the original problem and others will refer to the new problem. In general, I would recommend asking a new question about the new issue, but in this case, see [What's the easiest way to install a missing Perl module?](http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module). – ThisSuitIsBlackNot Sep 17 '15 at 18:31
  • Ok got it. Sorry about that. I will make sure next time definitely. – john Sep 17 '15 at 18:44
  • 1
    @david: I've rolled your question back to the original version. I think that's the best way to reflect the history of this item. If you want to ask a different question then *open a new question* and don't edit one of your old ones. You must reaslise that Stack Overflow is a *knowledge base* and not a traditional forum. That means the *primary* purpose of your question and the associated answers is to provide help for *other people* who may be in a similar situation. However much you may feel that it's there just for your own benefit, it simply isn't, and you must treat it as public property – Borodin Sep 17 '15 at 18:54

2 Answers2

2
Can't locate XML/LibXML.pm 

So you need to install the XML::LibXML module. Try one of these commands.

If you have sudo access, and are on Debian Linux or Ubuntu, run this:

sudo apt-get install libxml-perl

If you have sudo access and are on Red Hat, CentOS, Fedora, or Amazon Linux:

sudo yum install perl-XML-LibXML

On other platforms with sudo (e.g. OS X), this should work, but you'll have to answer a lot of questions if you haven't run cpan before:

sudo cpan XML::LibXML

If you don't have sudo access, run the same command without sudo and select local::lib when it asks what approach you wish to take:

cpan XML::LibXML
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • just tried, It is asking this `What approach do you want? (Choose 'local::lib', 'sudo' or 'manual') [local::lib]` Being a begineer, I am not sure which one to choose? – john Sep 17 '15 at 18:26
  • @david, Since we're talking about your system, I recommend you actually use your system's package manager (`apt-get` or `yum`) if you can. If you want to proceed with `cpan` and you are able to `sudo`, then I recommend `sudo`. If not, go with local::lib. – ikegami Sep 17 '15 at 18:31
  • I can do sudo as well as I have root access. Is there any `sudo apt-get` command as well? You mean I should choose sudo right? – john Sep 17 '15 at 18:34
  • Yes it did work. But I have some other issues in my perl script so opened a new question [here](http://stackoverflow.com/questions/32637821/not-able-to-insert-few-lines-in-an-xml-file-at-a-particular-spot-using-perl) See if you can help out. – john Sep 17 '15 at 18:46
  • wow... been out of the game for a while... thanks learned something new. – ojblass Sep 17 '15 at 18:59
1

Instead of calling the script by its name run:

perl /path/to/perl/script

I am wondering if you have a hidden character in your first line. Did you write the file in DOS?

With your update you will have to install the Perl Module it cannot find. There are instructions on CPAN on how to install additional modules.

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • update your questions title it still reflects the old question? Maybe a new question? – ojblass Sep 17 '15 at 18:23
  • 1
    I'd recommend using the OS package manager over CPAN if the package is available (which it is: `libxml-perl`). – Kenney Sep 17 '15 at 18:23