Create and writean XML file
Christian Orsatti <[email protected]>
| Newsgroups | gmane.text.xml.xerces-p.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
Sorry to bother you again.
I am trying to create from sratch an XML file.
Base on the Xerces perl exemple (whichi seems to be old and not really
running) and Xerces C++ ducumentation I write the following code:
#### Create a document
my $impl =
XML::Xerces::DOMImplementationRegistry::getDOMImplementation("CORE");
my $serializer = $impl->createDOMWriter();
my $dt = eval{$impl->createDocumentType('contributors', '',
'contributors.dtd')};
error($@) if $@;
my $doc = eval{$impl->createDocument('contributors', 'contributors',$dt)};
error($@) if $@;
$serializer->setFeature("$XML::Xerces::fgDOMWRTFormatPrettyPrint", 1);
This line give me an error:
XML::Xerces::DOMException=HASH(0x8469214)
I can live without that but I wanted to try to have a nice ouput file.
my $root = $doc->getDocumentElement();
$root->appendChild(main::CreatePerson(
$doc,
'Mike Pogue',
'manager',
'[email protected]'
));
$root->appendChild(main::CreatePerson(
$doc,
'Tom Watson',
'developer',
'[email protected]'
));
$root->appendChild(main::CreatePerson(
$doc,
'Susan Hardenbrook',
'tech writer',
'[email protected]'
));
my $str = $serializer->writeToString($root);
Here I would like to write to a file directly but I haven't found the
equivanlent in Perl of the LocalFileFormatTarget C++ class to use in
writeNode() function.
But I can write my string to a file with Perl features.
By my string is all on one line:
<contributors xmlns="contributors"><person Role="manager"><name>Mike
Pogue</name><email>[email protected]</email></person><person
Role="developer"><name>Tom
Watson</name><email>[email protected]</email></person><person
Role="tech writer"><name>Susan
Hardenbrook</name><email>[email protected]</email></person></contributors>
How can I format it with Xerces Perl in order to have an indented XML
(has if is was written through emacs for example) ?
Is there better way to do what I want than the code I did ?
Thanks for you help
Take care
Christian