The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

XML::Xerces::BagOfTricks - A library to make XML:Xerces easier and more perl-ish

SYNOPSIS

  use XML::Xerces::BagOfTricks qw(:all);

  # get a nice (empty) DOM Document
  my $DOMDocument = getDocument($namespace,$root_tag);

  # get a DOM Document from an XML file
  my $DOMDocument = getDocumentFromXML (file=>$file);

  # get a DOM Document from an XML file
  my $DOMDocument = getDocumentFromXML(xml=>$xml);

  # get a nice Element containing a text node (i.e. <foo>bar</foo>)
  my $foo_elem = getTextElement($DOMDocument,'Foo','Bar');

  # get a nice element with attributes (i.e '<Foo isBar='0' isFoo='1'/>')
  my $foo_elem = getElement($DOMDocument,'Foo','isBar'=>0, 'isFoo'=>1);

  # get a nice element with attributes that contains a text node
  my $foo_elem = getElementwithText($DOMDocument,'Foo','Bar',isFoo=>1,isBar=>0);
  # (i.e. <Foo isFoo='1' isBar='0'>Bar</Foo>)

  # if node is not of type Element then append its data to $contents
  # based on examples in article by P T Darugar.
  if ( $NodeType[$node->getNodeType()] ne 'Element' ) {
            $contents .= $node->getData();
  }
  # or the easier..
  my $content = getTextContents($node);

  # get the nice DOM Document as XML
  my $xml = getXML($DOMDocument);

DESCRIPTION

This module is designed to provide a bag of tricks for users of XML::Xerces DOM API. It provides some useful variables for looking up xerces-c enum values.

It also provides functions that make dealing with, creating and printing DOM objects much easier.

getTextContents() from 'Effective XML processing with DOM and XPath in Perl' by Parand Tony Darugar, IBM Developerworks Oct 1st 2001

EXPORT

':all' tag exports the following :

%NodeType @NodeType $schemaparser $dtdparser $plainparser

&getTextContents &getDocument &getDocumentFromXML &getXML

&getTextElement &getElement &getElementwithText

FUNCTIONS

getTextContents($node)

returns the text content of a node (and its subnodes)

my $content = getTextContents($node);

Function by P T Darugar, published in IBM Developerworks Oct 1st 2001

getTextElement($doc,$name,$value)

    This function returns a nice XML::Xerces::DOMNode representing an element
    with an appended Text subnode, based on the arguments provided.

    In the example below $node would represent '<Foo>Bar</Foo>'

    my $node = getTextElement($doc,'Foo','Bar');

    More useful than a pocketful of bent drawing pins! If only the Chilli Peppers
    made music like they used to 'Zephyr' is no equal of 'Fight Like A Brave' or
    'Give it away'

getElement($doc,$name,%attributes)

    This function returns a nice XML::Xerces::DOMNode representing an element
    with an appended Text subnode, based on the arguments provided.

    In the example below $node would represent '<Foo isBar='0' isFoo='1'/>'

    my $node = getElement($doc,'Foo','isBar'=>0, 'isFoo'=>1);

getElementwithText($DOMDocument,$node_name,$text,$attr_name=>$attr_value);

  # get a nice element with attributes that contains a text node ( i.e. <Foo isFoo='1' isBar='0'>Bar</Foo> )
  my $foo_elem = getElementwithText($DOMDocument,'Foo','Bar',isFoo=>1,isBar=>0);

getDocument($namespace,$root_tag)

This function will return a nice XML:Xerces::DOMDocument object.

It requires a namespace, a root tag, and a list of tags to be added to the document

the tags can be scalars :

my $doc = getDocument('http://www.some.org/schema/year foo.xsd', 'Foo', 'Bar', 'Baz');

or a hashref of attributes, and the tags name :

my $doc = getDocument($ns,{name=>'Foo', xmlns=>'http://www.other.org/namespace', version=>1.3}, 'Bar', 'Baz');

getDocumentFromXML

Returns an XML::Xerces::DOMDocument object based on the provided input

my $DOMDocument = getDocumentFromXML(xml=>$xml);

uses the XML::Xerces DOM parser to build a DOM Tree of the given xml

my $DOMDocument = getDocumentFromXML (file=>$file);

uses the XML::Xerces DOM parser to build a DOM Tree of the given xml

getXML($DOMDocument)

getXML is exported in the ':all' tag and will return XML in a string generated from the DOM Document passed to it

my $xml = getXML($doc);

SEE ALSO

XML::Xerces

AUTHOR

Aaron Trevena, <teejay@droogs.org>

COPYRIGHT AND LICENSE

Copyright (C) 2003 by Aaron Trevena, Surrey Technologies, Ltd.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.