Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase created with perl stored as xml
    primarykey
    data
    text
    <p>I'm trying to save a complex array/hash structure to xml. As I'm new to both perl and xml I don't know what is the easiest way to do this.</p> <p>All the xml parser, writer, libxml, etc. modules don't give me what I want. E.g. DumpXML adds lots of tags. I have tried many different modules but none of them seem to do what I want or I don't know how to set them up so that they work as I want them to. Maybe I do have to write the xml part on a lower level? Or maybe it's best if I don't use the perl data structures but store it directly to xml?</p> <p>The idea is to create a php webpage from the xml data. Unfortunately I'm a php noob as well and therefore was just hoping doing so won't be a big problem. :-) I just wanted to have a logical layout of the xml file like in the examples below.</p> <p>Here's how I tried it with <code>Data::Diver</code> and <code>XML::Smart</code>.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Data::Diver qw( Dive DiveRef DiveVal DiveError ); use XML::Smart; my $content = {}; # Usage: add_content_entry(name, group, descr) sub add_content_entry { my $name = shift; my $group = shift; my $descr1 = shift; my $descr2 = shift; my $data = { DESCR1 =&gt; $descr1, DESCR2 =&gt; $descr2, }; my @pos = split('/', $group); push @pos, $name; DiveVal( $content, @pos ) = $data; } sub xml_read { my $xml = XML::Smart-&gt;new('file.xml'); $content = $xml-&gt;data; } sub xml_write { my $xml = XML::Smart-&gt;new( q` &lt;?xml version="1.0" encoding="iso-8859-1" ?&gt; &lt;content&gt;&lt;/content&gt; `); $xml-&gt;{content} = $content; $xml-&gt;('file.xml'); } # Main &amp;xml_read; # file.xml is empty &amp;add_content_entry( 'content.1', 'group.A', 'Hello', 'World' ); &amp;add_content_entry( 'content.2', 'group.B/group.x', 'Fred', 'Flintstone' ); &amp;add_content_entry( 'content.3', 'group.B/group.y', 'bla', 'blah' ); &amp;add_content_entry( 'content.4', 'group.B/group.y', '???', '!!!' ); &amp;add_content_entry( 'content.5', 'group.C/group.z', '...', '...' ); &amp;xml_write; # file.xml is written $content = {}; &amp;xml_read; # justify that file.xml can be read print Dumper $content; </code></pre> <p>The output should be:</p> <pre><code>$VAR1 = { 'group.A' =&gt; { 'content.1' =&gt; { 'DESCR2' =&gt; 'World', 'DESCR1' =&gt; 'Hello' } }, 'group.C' =&gt; { 'group.z' =&gt; { 'content.5' =&gt; { 'DESCR2' =&gt; '...', 'DESCR1' =&gt; '...' } } }, 'group.B' =&gt; { 'group.y' =&gt; { 'content.3' =&gt; { 'DESCR2' =&gt; 'blah', 'DESCR1' =&gt; 'bla' }, 'content.4' =&gt; { 'DESCR2' =&gt; '!!!', 'DESCR1' =&gt; '???' } }, 'group.x' =&gt; { 'content.2' =&gt; { 'DESCR2' =&gt; 'Flintstone', 'DESCR1' =&gt; 'Fred' } } } }; </code></pre> <p>My problem is that the number of levels is not the same for the different contents.</p> <p>In the xml file there should be something like (I know the sorting is arbitrary. I kept the same as in the output of <code>print Dumper</code>).</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1" ?&gt; &lt;content&gt; &lt;group.A&gt; &lt;content.1&gt; &lt;DESCR2&gt;World&lt;/DESCR&gt; &lt;DESCR1&gt;Hello&lt;/DESCR1&gt; &lt;/content.1&gt; &lt;/group.A&gt; &lt;group.C&gt; &lt;group.z&gt; &lt;content.5&gt; &lt;DESCR2&gt;...&lt;/DESCR&gt; &lt;DESCR1&gt;...&lt;/DESCR1&gt; &lt;/content.5&gt; &lt;/group.z&gt; &lt;/group.C&gt; &lt;group.B&gt; &lt;group.y&gt; &lt;content.3&gt; &lt;DESCR2&gt;blah&lt;/DESCR&gt; &lt;DESCR1&gt;bla&lt;/DESCR1&gt; &lt;/content.3&gt; &lt;content.4&gt; &lt;DESCR2&gt;!!!&lt;/DESCR&gt; &lt;DESCR1&gt;???&lt;/DESCR1&gt; &lt;/content.4&gt; &lt;/group.y&gt; &lt;group.x&gt; &lt;content.2&gt; &lt;DESCR2&gt;Flintstone&lt;/DESCR&gt; &lt;DESCR1&gt;Fred&lt;/DESCR1&gt; &lt;/content.2&gt; &lt;/group.x&gt; &lt;/group.B&gt; &lt;/content&gt; </code></pre> <p>or</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1" ?&gt; &lt;content&gt; &lt;group.A&gt; &lt;content DESCR2="World" DESCR1="Hello"&gt;content.1&lt;/content&gt; &lt;/group.A&gt; &lt;group.C&gt; &lt;group.z&gt; &lt;content DESCR2="..." DESCR1="..."&gt;content.5&lt;/content&gt; &lt;/group.z&gt; &lt;/group.C&gt; &lt;group.B&gt; &lt;group.y&gt; &lt;content DESCR2="blah" DESCR1="bla"&gt;content.3&lt;/content&gt; &lt;content DESCR2="!!!" DESCR1="???"&gt;content.4&lt;/content&gt; &lt;/group.y&gt; &lt;group.x&gt; &lt;content DESCR2="Flintstone" DESCR1="Fred"&gt;content.2&lt;/content&gt; &lt;/group.x&gt; &lt;/group.B&gt; &lt;/content&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload