XML-Smart

 view release on metacpan or  search on metacpan

lib/XML/Smart.pm  view on Meta::CPAN

#############

sub set_node_type {

    my $this = shift ;

    my ( $type, $bool ) = @_ ;
    if( $#_ < 1 ) { 
	$bool = 1 ;
    }
    
    my $key  = $this->key  ;
    my $back = $this->back ;
    
    $back->{'/nodes'} = {} if( $back->{'/nodes'}->null );
    my $nodes = $back->{'/nodes'}->pointer ;
    
    if( $bool ) {

	if( $$nodes{$key} && $$nodes{$key} =~ /^\w+,\d+,(\d*)/ ) {
	    my $val = $1                   ;
	    $$nodes{$key} = "$type,1,$val" ;
	} else { 
	    my $existing_node_data = ( $$nodes{$key} ) ? $$nodes{$key} : "" ;
	    $$nodes{$key} = "$type,1," . $existing_node_data ;
	}
	
	if( !$this->{CONTENT} ) {
	    my $content = $this->content ;
	    $this->{CONTENT} = $content if $content ne '' ;
	}

    } else {

	if( !$$nodes{$key} ) {
	    my $tp = _data_type( $back->{$key} ) ;
	    if ( $tp > 2 ) { $$nodes{$key} = "$type,0," ;}
	} elsif( $$nodes{$key} eq '1' ) { 
	    $$nodes{$key} = "$type,0,1" ;
	} elsif( $$nodes{$key} =~ /^\w+,\d+,1/ ) { 
	    $$nodes{$key} = "$type,0,1" ;
	} elsif( $$nodes{$key} =~ /^\w+,\d+,0?$/ ) {

	    delete $$nodes{$key} ;
	    my @keys = keys %$this ;

	    if( $#keys == 0 && $keys[0] eq 'CONTENT') {
		my $content = $this->{CONTENT}('.') ;
		$this->back->pointer->{$key} = $content ;
	    }

	}
    }
    
}

#############
# SET_CDATA #
#############

sub set_cdata {
    my $this = shift ;
    $this->set_node_type('cdata',@_) ;
}

##############
# SET_BINARY #
##############

sub set_binary {
    my $this = shift ;
    $this->set_node_type('binary',@_) ;
}

#################
# SET_AUTO_NODE #
#################

sub set_auto_node {

    my $this = shift ;
    
    my $key  = $this->key  ;
    my $back = $this->back ;
    
    $back->{'/nodes'} = {} if( $back->{'/nodes'}->null );
    my $nodes = $back->{'/nodes'}->pointer ;
    
    if( !$$nodes{$key} || $$nodes{$key} eq '1' ) { 
	# Do nothing. ; 
    } elsif( $$nodes{$key} =~ /^\w+,\d+,1/   ) { 
	$$nodes{$key} = 1 ;
    } elsif( $$nodes{$key} =~ /^\w+,\d+,0?$/ ) {

	delete $$nodes{$key} ;
	my @keys = keys %$this ;

	if( $#keys == 0 && $keys[0] eq 'CONTENT') {
	    my $content = $this->{CONTENT}('.') ;
	    $this->back->pointer->{$key} = $content ;
	}

    }

}

############
# SET_AUTO #
############

sub set_auto {
    
    my $this = shift ;
    
    my $key  = $this->key  ;
    my $back = $this->back ;
    
    $back->{'/nodes'} = {} if $back->{'/nodes'}->null ;
    my $nodes = $back->{'/nodes'}->pointer ;
    
    delete $$nodes{$key} ;
    my @keys = keys %$this ;
    if( $#keys == 0 && $keys[0] eq 'CONTENT') {

lib/XML/Smart.pm  view on Meta::CPAN


B<Also can be used with multiple contents:>

For this XML data:

  <root>
  content0
  <tag1 arg="1"/>
  content1
  </root>

Getting all the content:

  my $all_content = $XML->{root}->content ;
  print "[$all_content]\n" ;

Output:

  [
  content0
  
  content1
  ]

Getting in parts:

  my @contents = $XML->{root}->content ;
  print "[@contents[0]]\n" ;
  print "[@contents[1]]\n" ;

Output

  [
  content0
  ]
  [
  content1
  ]

B<Setting multiple contents:>

  $XML->{root}->content(0,"aaaaa") ;
  $XML->{root}->content(1,"bbbbb") ;

Output now will be:

  [aaaaa]
  [bbbbb]

And now the XML data generated will be:

  <root>aaaaa<tag1 arg="1"/>bbbbb</root>

=head2  copy()

Return a copy of the XML::Smart object (pointing to the base).

** This is good when you want to keep 2 versions of the same XML tree in the memory,
since one object can't change the tree of the other!

B<WARNING:> set_node(), set_cdata() and set_binary() changes are not persistant over copy - 
Once you create a second copy these states are lost.

b<warning:> do not copy after apply_dtd() unless you have checked for dtd errors.

=head2  cut_root()

Cut the root key:

  my $srv = $XML->{rootx}{host}{server} ;
  
  ## Or if you don't know the root name:
  $XML = $XML->cut_root() ;
  my $srv = $XML->{host}{server} ;

** Note that this will cut the root of the pointer in the tree.
So, if you are in some place that have more than one key (multiple roots), the
same object will be retuned without cut anything.

=head2 data (OPTIONS)

Return the data of the XML object (rebuilding it).

B<Options:>

=over 11

=item nodtd

Do not add in the XML content the DTD applied by the method I<apply_dtd()>.

=item noident

If set to true the data isn't idented.

=item nospace

If set to true the data isn't idented and doesn't have space between the
tags (unless the CONTENT have).

=item lowtag

Make the tags lower case.

=item lowarg

Make the arguments lower case.

=item upertag

Make the tags uper case.

=item uperarg

Make the arguments uper case.

=item length

If set true, add the attribute 'length' with the size of the data to the xml header (<?xml ...?>).
This is useful when you send the data through a socket, since the socket can know the total amount
of data to read.

lib/XML/Smart.pm  view on Meta::CPAN

Return the key of the value.

If wantarray return the index too: return(KEY , I) ;

=head2  nodes()

Return the nodes (objects) in the pointer (keys that aren't arguments).

=head2  nodes_keys()

Return the nodes names (not the object) in the pointer (keys that aren't arguments).

=head2  null()

Return I<true> if the XML object has a null tree or if the pointer is in some place that doesn't exist.

=head2  order()

Return the order of the keys. See I<set_order()>.

=head2  path()

Return the path of the pointer.

I<Example>:

  /hosts/server[1]/address[0]

B<Note that the index is 0 based and 'address' can be an attribute or a node, what is not compatible with XPath.>

B<** See I<path_as_xpath()>.>

=head2  path_as_xpath()

Return the path of the pointer in the XPath format.

=head2  pointer

Return the HASH tree from the pointer.

=head2  pointer_ok

Return a copy of the tree of the object, B<from the pointer>, but without internal keys added by I<XML::Smart>.

=head2 root

Return the ROOT name of the XML tree (main key).

** See also I<key()> for sub nodes.

=head2 save (FILEPATH , OPTIONS)

Save the XML data inside a file.

Accept the same OPTIONS of the method B<I<data()>>.

=head2  set_auto

Define the key to be handled automatically. Soo, data() will define automatically if it's a node, content or attribute.

I<** This method is useful to remove set_node(), set_cdata() and set_binary() changes.>


=head2  set_auto_node

Define the key as a node, and data() will define automatically if it's CDATA or BINARY.

I<** This method is useful to remove set_cdata() and set_binary() changes.>

=head2  set_binary(BOOL)

Define the node as a BINARY content when TRUE, or force to B<not> handle it as a BINARY on FALSE.

Example of node handled as BINARY:

  <root><foo dt:dt="binary.base64">PGgxPnRlc3QgAzwvaDE+</foo></root>

Original content of foo (the base64 data):

  <h1>test \x03</h1>

=head2  set_cdata(BOOL)

Define the node as CDATA when TRUE, or force to B<not> handle it as CDATA on FALSE.

Example of CDATA node:

  <root><foo><![CDATA[bla bla bla <tag> bla bla]]></foo></root>

=head2  set_node(BOOL)

Set/unset the current key as a node (tag).

** If BOOL is not defined will use I<TRUE>.

B<WARNING:> You cannot set_node, copy the object and then set_node( 0 ) [ Unset node ]

=head2  set_order(KEYS)

Set the order of the keys (nodes and attributes) in this point.

=head2  set_tag

Same as set_node.

=head2  tree()

Return the HASH tree of the XML data.

** Note that the real HASH tree is returned here. All the other ways return an
object that works like a HASH/ARRAY through tie.

=head2  tree_pointer()

Same as I<pointer()>.

=head2  tree_ok()

Return a copy of the tree of the object, but without internal keys added by I<XML::Smart>, like I</order> and I</nodes>.

=head2  tree_pointer_ok()

Return a copy of the tree of the object, B<from the pointer>, but without internal keys added by I<XML::Smart>.

=head2  xpath() || XPath()

Return a XML::XPath object, based in the XML root in the tree.

  ## look from the root:
  my $data = $XML->XPath->findnodes_as_string('/') ;

I<** Need XML::XPath installed, but only load when is needed.>

=head2  xpath_pointer() || XPath_pointer() 

Return a XML::XPath object, based in the XML::Smart pointer in the tree.

  ## look from this point, soo XPath '/' actually starts at /server/:
  
  my $srvs = $XML->{server} ;
  my $data = $srvs->XPath_pointer->findnodes_as_string('/') ;



( run in 0.762 second using v1.01-cache-2.11-cpan-39bf76dae61 )