XML-Bare
view release on metacpan or search on metacpan
my $obb = $objs->{ $b };
my $posa = 0;
my $posb = 0;
$oba = $oba->[0] if( ref( $oba ) eq 'ARRAY' );
$obb = $obb->[0] if( ref( $obb ) eq 'ARRAY' );
if( ref( $oba ) eq 'HASH' ) { $posa = $oba->{'_pos'} || 0; }
if( ref( $obb ) eq 'HASH' ) { $posb = $obb->{'_pos'} || 0; }
return $posa <=> $posb;
} keys %$objs;
if( $objs->{'_cdata'} ) {
my $val = $objs->{'value'};
$val =~ s/^(\s*\n)+//;
$val =~ s/\s+$//;
$val =~ s/&/&/g;
$val =~ s/</</g;
$objs->{'value'} = $val;
#$xml = "$less![CDATA[<div class='node'><div class='cdata'>$val</div></div>]]$more";
$cd0 = "$less![CDATA[<div class='node'><div class='cdata'>";
$cd1 = "</div></div>]]$more";
}
for my $i ( @dex ) {
my $obj = $objs->{ $i } || '';
my $type = ref( $obj );
if( $type eq 'ARRAY' ) {
$imm = 0;
my @dex2 = sort {
if( !$a ) { return 0; }
}
else {
$imm = 0;
$xml .= obj2html( $obj , $i, $pad.' ', $level+1, $#dex );
}
}
else {
if( $i eq 'comment' ) { $xml .= "$less!--" . $obj . "--$more" . "<br>\n"; }
elsif( $i eq 'value' ) {
if( $level > 1 ) {
if( $obj && $obj =~ /[<>&;]/ && ! $objs->{'_cdata'} ) { $xml .= "$less![CDATA[$obj]]$more"; }
else { $xml .= $obj if( $obj =~ /\S/ ); }
}
}
elsif( $i =~ /^_/ ) {}
else { $xml .= "$less$tn0$i$tn1$more$obj$less/$tn0$i$tn1$more"; }
}
}
my $pad2 = $imm ? '' : $pad;
if( substr( $name, 0, 1 ) ne '_' ) {
if( $name ) {
#endif
// node without children
if( !length ) {
if( curnode->vallen ) {
SV * sv = newSVpvn( curnode->value, curnode->vallen );
SvUTF8_on(sv);
hv_store( output, "value", 5, sv, vhash );
if( curnode->type ) {
SV *svi = newSViv( 1 );
hv_store( output, "_cdata", 6, svi, cdhash );
}
}
if( curnode->comlen ) {
SV * sv = newSVpvn( curnode->comment, curnode->comlen );
SvUTF8_on(sv);
hv_store( output, "comment", 7, sv, chash );
}
}
// node with children
else {
if( curnode->vallen ) {
SV *sv = newSVpvn( curnode->value, curnode->vallen );
SvUTF8_on(sv);
hv_store( output, "value", 5, sv, vhash );
if( curnode->type ) {
SV *svi = newSViv( 1 );
hv_store( output, "_cdata", 6, svi, cdhash );
}
}
if( curnode->comlen ) {
SV *sv = newSVpvn( curnode->comment, curnode->comlen );
SvUTF8_on(sv);
hv_store( output, "comment", 7, sv, chash );
}
// loop through child nodes
curnode = curnode->firstchild;
hv_delete( output, curnode->name, curnode->namelen, 0 );
hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
av_push( newarray, cxml2obj_simple( parser, curnode ) );
}
}
if( i != ( length - 1 ) ) curnode = curnode->next;
}
curnode = curnode->parent;
}
else {
if( curnode->type ) { // store cdata value under content, even if empty or spaces
SV * sv = newSVpvn( curnode->value, curnode->vallen );
SvUTF8_on(sv);
hv_store( output, "content", 7, sv, content_hash );
}
else {
int hasval = 0;
for( i=0;i<curnode->vallen;i++ ) {
char let = curnode->value[ i ];
if( let != ' ' && let != 0x0d && let != 0x0a ) {
hasval = 1;
return outputref;
}
void init_hashes() {
PERL_HASH(vhash, "value", 5);
PERL_HASH(ahash, "_att", 4);
PERL_HASH(chash, "comment", 7);
PERL_HASH(phash, "_pos", 4);
PERL_HASH(ihash, "_i", 2 );
PERL_HASH(zhash, "_z", 2 );
PERL_HASH(cdhash, "_cdata", 6 );
}
MODULE = XML::Bare PACKAGE = XML::Bare
SV *
xml2obj( parsersv )
SV *parsersv
CODE:
struct parserc *parser;
parser = INT2PTR( struct parserc *, SvUV( parsersv ) );
bench/large.xml
bench/license.txt
t/Basic.t
t/Pod_Coverage.t
t/Pod.t
t/UTF8_Values.t
t/UTF8_Attributes.t
t/test.xml
t/Bug_node_named_value.t
t/Bug_simple_empty_is_one.t
t/Bug_simple_with_cdata.t
t/Func_find_by_perl.t
t/Func_find_node.t
t/Multithreading.t
META.json Module JSON meta-data (added by MakeMaker)
//#define DEBUG
#define ST_val_1 1
#define ST_val_x 2
#define ST_comment_1dash 3
#define ST_comment_2dash 4
#define ST_comment 5
#define ST_comment_x 6
#define ST_pi 7
#define ST_bang 24
#define ST_cdata 8
#define ST_name_1 9
#define ST_name_x 10
#define ST_name_gap 11
#define ST_att_name1 12
#define ST_att_space 13
#define ST_att_name 14
#define ST_att_nameqs 15
#define ST_att_nameqsdone 16
#define ST_att_eq1 17
#define ST_att_eqx 18
att_has_val = self->att_has_val;
switch( self->last_state ) {
case ST_val_1: goto val_1;
case ST_val_x: goto val_x;
case ST_comment_1dash: goto comment_1dash;
case ST_comment_2dash: goto comment_2dash;
case ST_comment: goto comment;
case ST_comment_x: goto comment_x;
case ST_pi: goto pi;
case ST_bang: goto bang;
case ST_cdata: goto cdata;
case ST_name_1: goto name_1;
case ST_name_x: goto name_x;
case ST_name_gap: goto name_gap;
case ST_att_name1: goto att_name1;
case ST_att_space: goto att_space;
case ST_att_name: goto att_name;
case ST_att_nameqs: goto att_nameqs;
case ST_att_nameqsdone: goto att_nameqsdone;
case ST_att_eq1: goto att_eq1;
case ST_att_eqx: goto att_eqx;
case '!':
if( *(cpos+2) == '[' ) { // <![
//if( !strncmp( cpos+3, "CDATA", 5 ) ) {
if( *(cpos+3) == 'C' &&
*(cpos+4) == 'D' &&
*(cpos+5) == 'A' &&
*(cpos+6) == 'T' &&
*(cpos+7) == 'A' ) {
cpos += 9;
curnode->type = 1;
goto cdata;
}
else {
cpos++; cpos++;
goto val_x;//actually goto error...
}
}
else if( *(cpos+2) == '-' && // <!--
*(cpos+3) == '-' ) {
cpos += 4;
goto comment;
bang:
let = *cpos;
if( let == '>' ) {
cpos++;
goto val_1;
}
if( !let ) { last_state = ST_bang; goto done; }
cpos++;
goto bang;
cdata:
let = *cpos;
if( !let ) { last_state = ST_cdata; goto done; }
if( let == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) {
cpos += 3;
goto val_1;
}
if( !curnode->numvals ) {
curnode->value = cpos;
curnode->vallen = 0;
curnode->numvals = 1;
}
if( curnode->numvals == 1 ) curnode->vallen++;
cpos++;
goto cdata;
name_1:
#ifdef DEBUG
printf("name_1: %c\n", *cpos);
#endif
let = *cpos;
switch( let ) {
case 0: last_state = ST_name_1; goto done;
case ' ':
case 0x0d:
case '<':
if( *(cpos+1) == '!' &&
*(cpos+2) == '[' &&
*(cpos+3) == 'C' &&
*(cpos+4) == 'D' &&
*(cpos+5) == 'A' &&
*(cpos+6) == 'T' &&
*(cpos+7) == 'A' ) {
cpos += 9;
curnode->type = 1;
goto u_cdata;
}
tagname_len = 0; // for safety
cpos++;
goto u_name_1;
}
if( curnode->numvals == 1 ) curnode->vallen++;
cpos++;
goto u_val_x;
u_cdata:
if( *cpos == ']' && *(cpos+1) == ']' && *(cpos+2) == '>' ) {
cpos += 3;
goto u_val_1;
}
if( !curnode->numvals ) {
curnode->value = cpos;
curnode->vallen = 0;
curnode->numvals = 1;
}
if( curnode->numvals == 1 ) curnode->vallen++;
cpos++;
goto u_cdata;
u_name_1: // node name
#ifdef DEBUG
printf("name_1: %c\n", *cpos);
#endif
switch( *cpos ) {
case '/': // regular closing tag
tagname_len = 0; // needed to reset
cpos++;
goto u_ename_1;
struct attc *firstatt;
struct attc *lastatt;
int numchildren;
int numatt;
char *name;
int namelen;
char *value;
char *comment;
int vallen;
int comlen;
int type;// cdata or normal
int numvals;
int numcoms;
int pos;
int err;
int z;
};
struct nodec *nodec_addchildr( struct nodec *self, char *newname, int newnamelen );//, char *newval, int newvallen, int newtype );
//struct nodec *nodec_addchild( struct nodec *self, char *newname, int newnamelen );
struct attc *nodec_addattr ( struct nodec *self, char *newname, int newnamelen );//, char *newval, int newvallen );
( $xml, $root, $simple ) = reparse( "<xml><node att=\"12\">val</node></xml>" );
is( $root->{xml}->{node}->{att}->{value}, '12', 'reading of " surrounded attribute value' );
is( $simple->{node}{att}, '12', 'simple - reading of " surrounded attribute value' );
( $xml, $root, $simple ) = reparse( "<xml><node att>val</node></xml>" );
is( $root->{xml}{node}{att}{value}, '1', "reading of value of standalone attribute" );
is( $simple->{node}{att}, '1', "simple - reading of value of standalone attribute" );
( $xml, $root, $simple ) = reparse( "<xml><node><![CDATA[<cval>]]></node></xml>" );
is( $root->{xml}->{node}->{value}, '<cval>', 'reading of cdata' );
is( $simple->{node}, '<cval>', 'simple - reading of cdata' );
( $xml, $root, $simple ) = reparse( "<xml><node>a</node><node>b</node></xml>" );
is( $root->{xml}->{node}->[1]->{value}, 'b', 'multiple node array creation' );
is( $simple->{node}[1], 'b', 'simple - multiple node array creation' );
( $xml, $root, $simple ) = reparse( "<xml><multi_node/><node>a</node></xml>" );
is( $root->{xml}->{node}->[0]->{value}, 'a', 'use of multi_' );
is( $simple->{node}[0], 'a', 'simple - use of multi_' );
# note output of this does not work
is( $root->{xml}->{comment}, 'test', 'loading a comment' );
# test node addition
( $xml, $root ) = new XML::Bare( text => "<xml></xml>" );
$xml->add_node( $root, 'item', name => 'bob' );
is( ref( $root->{'item'}[0]{'name'} ), 'HASH', 'node addition' );
is( $root->{'item'}[0]{'name'}{'value'}, 'bob', 'node addition' );
# test cyclic equalities
cyclic( "<xml><b><!--test--></b><c/><c/></xml>", 'comment' );
cyclic( "<xml><a><![CDATA[cdata]]></a></xml>", 'cdata' ); # with cdata
my $text = '<xml><node>checkval</node></xml>';
( $xml, $root ) = new XML::Bare( text => $text );
my $i = $root->{'xml'}{'node'}{'_i'}-1;
my $z = $root->{'xml'}{'node'}{'_z'}-$i+1;
#is( substr( $text, $i, $z ), '<node>checkval</node>', '_i and _z vals' );
# saving test
( $xml, $root ) = XML::Bare->new( file => 't/test.xml' );
$xml->save();
t/Bug_simple_with_cdata.t view on Meta::CPAN
#!/usr/bin/perl -w
use strict;
use Test::More qw(no_plan);
use_ok( 'XML::Bare', qw/xmlin/ );
my ( $ob, $root ) = XML::Bare->simple( text => "<node att='2'><![CDATA[cdata contents]]></node>" );
ok( $root, "Got some root" );
my $attval = $root->{'node'}{'att'};
is( $attval, '2', "Got the right attribute value" );
my $cdataval = $root->{'node'}{'content'};
is( $cdataval, 'cdata contents', "Got the right cdata value" );
( run in 0.655 second using v1.01-cache-2.11-cpan-454fe037f31 )