You are viewing a plain text version of this content. The canonical link for it is here.
Posted to p-dev@xerces.apache.org by "Lars Preben S. Arnesen" <l....@usit.uio.no> on 2001/06/18 10:35:30 UTC

Altering XML with Xerces

I guess this is a newbie question, but anyway:

I want to read a XML file and add some to it. From the Xerces-C FAQ I
understand that this isn't implemented straight forward and that I can
use the code from the DOMCreate example.

Below is my modified DOMCreate example. I only added two lines, but
when I run it I only get "Aborted".

Please reply direct to me since I'm currently not on the mailinglist (yet...)


use strict;
# use blib;
use XML::Xerces;
use XML::Xerces::DOMParse;
use Getopt::Std;
use vars qw($opt_v $opt_n);

#
# Read and validate command line args
#

my $USAGE = "USAGE: $0 [-v][-n] file\n";

getopts ('vn') and ($#ARGV == 0)	or die "$USAGE";
-f $ARGV[0] or die "File '$ARGV[0]' does not exist!\n";

my $validate = $opt_v || 0;
my $namespace = $opt_n || 0;
my $file = $ARGV[0];


#
# Parse and print
#

my $file = $ARGV[0];
my $parser = XML::Xerces::DOMParser->new();
$parser->setDoNamespaces ($namespace);
$parser->setDoValidation ($validate);
$parser->setToCreateXMLDeclTypeNode(1);
my $ERROR_HANDLER = XML::Xerces::perlErrorHandler->new();
$ERROR_HANDLER->setWarningFunction(\&XML::Xerces::warning);
$ERROR_HANDLER->setErrorFunction(\&XML::Xerces::error);
$ERROR_HANDLER->setFatalErrorFunction(\&XML::Xerces::fatal_error);
$parser->setErrorHandler($ERROR_HANDLER);
$parser->parse (XML::Xerces::LocalFileInputSource->new($file));
my $doc = $parser->getDocument ();

# My modification
my $foo = $doc->createElement ("bar");
$doc->appendChild ($foo);
# End of my modification

XML::Xerces::DOMParse::unformat ($doc);
XML::Xerces::DOMParse::format ($doc);
XML::Xerces::DOMParse::print (\*STDOUT, $doc);



__END__



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org


Re: Altering XML with Xerces

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Lars Preben S. Arnesen" <l....@usit.uio.no> writes:

> # My modification
> my $foo = $doc->createElement ("bar");
> $doc->appendChild ($foo);
> # End of my modification

The problem here is two-fold:

1) appending a child element directly to a document that already has a
   root element is illegal since a document can only (legally) have a
   single top-level element, so appendChild() throws an exception.
2) appendChild is throwing a DOMException but Xerces.pm is not set up
   to catch that exception, so you see 'Aborted'. Exception handling
   is the weakest supported feature of Xerces at the moment. I
   currently have to hand-code each function that needs handling (SWIG
   has it's limitations).

what you want to do is something like:

# My modification
my $root = $doc->getDocumentElement();
my $foo = $doc->createElement ("bar");
$root->appendChild ($foo);
# End of my modification

That way you will be inserting <bar/> into the existing top-level
element. 

Thanks for the error report, I'll be happy to fix all the DOM
functions so that they catch exceptions. Look for a new release next
week some time.

jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org