Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

How do you create sub-nodes with XML::Smart

by Anonymous Monk
on Mar 03, 2009 at 16:22 UTC ( #747784=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use XML::Smart to create XML files. Here is my code: my $XML = XML::Smart->new(); $XML->{JOB}{NAME} = $job_name; $XML->{JOB}{DESC} = $desc; $XML->{JOB}{NUM} = $num; Here is the output: <JOB NAME="Name goes here" DESC="Description" NUM="Num goes here"/> I want it to look like this: <JOB NAME="Name goes here"> <DESC>Description</DESC> <NUM>Description</NUM> </JOB> The problem is that I can't create single nodes. The data is all contained in the main node. Any advice would be appreciated. Thanks!
  • Comment on How do you create sub-nodes with XML::Smart

Replies are listed 'Best First'.
Re: How do you create sub-nodes with XML::Smart
by ramrod (Curate) on Mar 03, 2009 at 16:41 UTC
    Well first off, next time please consider using the code tags. It will help the appearance of your post.

    Next, did you check out CPAN? Here I was able to find some sample code.

    Looks like you should use something like

    $XML->{JOB}{DESC}[0] = $desc;
      Duly noted on the code tags. I did try the code you posted but it still have me this output:
      $XML->{JOB}{NAME} = $job_name; $XML->{JOB}{DESC}[0] = $desc; $XML->{JOB}{DATA}[0] = $data; $XML->{JOB}{WHO}[0] = $who; <JOB NAME="Jonname" DESC="Description" DATA="Data..."/>
      I would have thought that it would have given me something like this:
      <JOB NAME="Name"> <DESC>Description</DESC> <DATA>Data</DATA> ...... </JOB>
      ...but it did not. Any other ideas? I have definitely looked over the CPAN site and I didn't see anything that answered my question. Thanks!
        According to the CREATING XML DATAsection, this is the expected behavior.

        So something closer to what you are looking for is this:

        use strict; use XML::Smart; my $job_name = "a job name"; my $desc = "a description"; my $desc2 = "another description"; my $num = "a number"; my $XML = XML::Smart->new(); $XML->{JOB}{NAME}[0] = $job_name; $XML->{JOB}{DESC}[0] = $desc; $XML->{JOB}{DESC}[1] = $desc2; $XML->{JOB}{NUM}[0] = $num; $XML->save('output.xml');
        Which produces:
        <JOB NAME="a job name" NUM="a number"> <DESC>a description</DESC> <DESC>another description</DESC> </JOB>
        So the presence of multiple entries is what makes it a nested element. If this behavior is not what you desire, perhaps XML::Twig or XML::libXML would better work for you. I have got great results from libXML in the past. Update: I checked the tutorial and found what you're looking for.
        $XML->{JOB}{DESC}= $desc; $XML->{JOB}{DESC}->set_node(1) ;
        This code will nest the description as an element. Results:
        <JOB NAME="a job name" NUM="a number"> <DESC>a description</DESC> </JOB>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://747784]
Approved by Corion
[Corion]: Meh. I have a very simple mechanism to scrape+track prices, and even to send me mail if something changes (via cron), but SQLite doesn't support window functions, so my simple SQL to determine a price change won't work :-( Maybe I should store the DB ...
[Corion]: ... in Pg, but that would mean that I'd have to deal with credentials and stuff :)
[Corion]: In fact, the whole thing is just four simple programs, one App::scrape, another being DBIx::RunSQL, and one some glue to convert a JSON object into an SQL INSERT statement (+DBI connect/execute), so it would be an incredibly simple solution...
[Corion]: ... but that simplicity falls down due to SQLite letting me down and me being too lazy to move to a real DB. Maybe DBD::CSV can handle window functions...
[hippo]: Pg has the "trust" mechanism if you don't fancy using credentials for a trivial db.
[Corion]: hippo: Oooh, that sounds quite nice - let me read the documentation on that :)
[hippo]: https://www. docs/current/ static/auth- methods.html#AUTH- TRUST
[erix]: or compile & run an instance as yourself (not user 'postgres')
[erix]: easiest setup is to run with PG* env variables set, and a custom .pgpass files (can be a $PGPASSFILE)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2018-02-23 15:08 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.