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 Christian Orsatti <or...@istar.fr> on 2003/10/30 15:25:38 UTC

Create and writean XML file

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',
                  'mpogue@us.ibm.com'
                 ));

$root->appendChild(main::CreatePerson(
                  $doc,
                  'Tom Watson',
                  'developer',
    'rtwatson@us.ibm.com'
                 ));

$root->appendChild(main::CreatePerson(
                  $doc,
                  'Susan Hardenbrook',
                  'tech writer',
                  'susanhar@us.ibm.com'
                 ));

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>mpogue@us.ibm.com</email></person><person 
Role="developer"><name>Tom 
Watson</name><email>rtwatson@us.ibm.com</email></person><person 
Role="tech writer"><name>Susan 
Hardenbrook</name><email>susanhar@us.ibm.com</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



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


Re: Create and writean XML file

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
Christian Orsatti <or...@istar.fr> writes:

> It work properly and I have for first line:
> 
> <?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>

Good.

> But then if I replace the 2 last lines and that I use instead the
> function writeToString:
> 
>  my $str = $writer->writeToString($doc);
>  print STDERR "$str\n";
> 
> then  the first line is:
> 
> <?xml version="1.0" encoding="UTF-16" standalone="no" ?>
> 
> where the eoncoding is not right. I found that pretty strange.
> I hope that help you.

Yeah - strange. Could you report this as a bug at:

  http://nagoya.apache.org/bugzilla/

You'll need to create an account first, but it will help not lose
this. 

> PS: Let me know when your new version is available and I will let me
> know my feedbak.

Will do. Thanks again,
jas.

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


Re: Create and writean XML file

Posted by Christian Orsatti <or...@istar.fr>.
Hi,

Thanks for your help.
I figure, with all the peaces of code you sent, to do exactly what I 
want to do for now.

But I want just let you know a strange behavior that you may want to 
have a look at while you are fixing some other problems
if I run the following code:

  my $impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
  my $writer = $impl->createDOMWriter();
  if ($writer->canSetFeature('format-pretty-print',1)) {
    $writer->setFeature('format-pretty-print',1);
  }
  $writer->setEncoding('ISO-8859-1');
  my $target = XML::Xerces::StdOutFormatTarget->new();
  $writer->writeNode($target,$doc);

It work properly and I have for first line:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>

But then if I replace the 2 last lines and that I use instead the 
function writeToString:

 my $str = $writer->writeToString($doc);

 print STDERR "$str\n";


then  the first line is:

<?xml version="1.0" encoding="UTF-16" standalone="no" ?>

where the eoncoding is not right. I found that pretty strange.
I hope that help you.

Thanks again

Christian

PS: Let me know when your new version is available and I will let me 
know my feedbak.



Jason E. Stewart wrote:

>Christian Orsatti <or...@istar.fr> writes:
>
>  
>
>>Sorry to bother you again.
>>    
>>
>
>No bother - this is the place for questions - and besides things have
>been far too quiet around here lately.
>
>  
>
>>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
>>    
>>
>
>Yes, some examples are out of date, my appologies. The tests located
>in the t/ directory should *always* be up-to-date, however.
>
>  
>
>>$serializer->setFeature("$XML::Xerces::fgDOMWRTFormatPrettyPrint", 1);
>>
>>This line give me an error:
>>XML::Xerces::DOMException=HASH(0x8469214)
>>    
>>
>
>If it can throw an exception, you probably want to wrap it with an
>eval (like the other calls) and call error() if $@ is set. That will
>tell you why you can't set pretty print.
>
>Also, the symbol is:
>
>   $XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint
>
>not:
>
>  $XML::Xerces::fgDOMWRTFormatPrettyPrint
>
>So:
>
>  eval {$serializer->setFeature($XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint, 1)}; 
>  XML::Xerces::error($@) if $@
>
>Produces:
>
>  Error in eval: Type error in argument 2 of $name, Expected
>  perl-string. 
>
>if we look why:
>
>  DB<12> p $XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint
>
>     _p_XMLCh=SCALAR(0x1069f9bc)
>
>Aha! The unicode constants are being exported as UTF-16 characters and
>*not* as Perl strings like they should. That _p_FOO garbage is simply
>SWIG's default wrapping of types it does not understand (SWIG is the
>tool I use to create the Perl layer on top of the C++ layer).
>
>So you won't be able to use the fgFOO constants until I fix that (the
>same is true for the ISO-8859-1 problem you are having. In the
>meantime, you simply use Perl strings. 
>
>Anytime the Xerces-C API tells you to use a UTF-16 string (an XMLCh*,
>you can simply use a perl string - the XML::Xerces code will
>automatically convert it for you).
>
>Looking at samples/DOMPrint.pl:
>
>  my $impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
>  my $writer = $impl->createDOMWriter();
>  if ($writer->canSetFeature('format-pretty-print',1)) {
>    $writer->setFeature('format-pretty-print',1);
>  }
>  my $target = XML::Xerces::StdOutFormatTarget->new();
>  $writer->writeNode($target,$doc);
>  
>we can use the string 'format-pretty-print' instead.                                   
>
>  
>
>>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.
>>    
>>
>
>Oops! Looks like I missed this declaration. I've added it now, so it
>will be in the next release (which I can make in a day or two after
>I've fixed the string constant problems). In the meantime, using the
>MemBuf target and simply calling Perl's print function works just as
>well. 
>
>  
>
>>But I can write my string to a file with Perl features.
>>By my string is all on one line:
>>
>>How can I format it with Xerces Perl in order to have an indented XML
>>(has if is was written through emacs for example) ?
>>    
>>
>
>setting pretty-print does that fairly well.
>
>  
>
>>Is there better way to do what I want than the code I did ?
>>    
>>
>
>check samples/DOMPrint.pl or t/DOMWriter.t neither are very advanced,
>but they are correct and functional. I'm happy to take better examples
>from anyone who has them.
>
>  
>
>>$serializer->$serializer->setEncoding("$XML::Xerces::XMLUni::fgISO88591EncodingString");
>>    
>>
>
>Yes, this constant is broken as well. It will be fixed in the next
>release. For now, just using the perl string 'ISO-8859-1' will work. 
>
>  
>
>>my $dt = eval{$impl->createDocumentType('PhysicalEntity', 'no',
>>'PhysicalEntity.dtd')};
>>
>>($@) and die ($@);
>>
>>my $doc = eval{$impl->createDocument('', 'PhysicalEntity',$dt)};
>>($@) and die ($@);
>>
>>$doc->setEncoding("\*XML::Xerces::XMLUni::fgISO88591EncodingString");
>>    
>>
>
>You can do either $doc->setEncoding or $serializer->setEncoding
>whichever you prefer.
>
>Thanks again for the feedback,
>jas.
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-p-dev-help@xml.apache.org
>
>
>
>  
>


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


Re: Create and writean XML file

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
Christian Orsatti <or...@istar.fr> writes:

> Sorry to bother you again.

No bother - this is the place for questions - and besides things have
been far too quiet around here lately.

> 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

Yes, some examples are out of date, my appologies. The tests located
in the t/ directory should *always* be up-to-date, however.

> $serializer->setFeature("$XML::Xerces::fgDOMWRTFormatPrettyPrint", 1);
> 
> This line give me an error:
> XML::Xerces::DOMException=HASH(0x8469214)

If it can throw an exception, you probably want to wrap it with an
eval (like the other calls) and call error() if $@ is set. That will
tell you why you can't set pretty print.

Also, the symbol is:

   $XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint

not:

  $XML::Xerces::fgDOMWRTFormatPrettyPrint

So:

  eval {$serializer->setFeature($XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint, 1)}; 
  XML::Xerces::error($@) if $@

Produces:

  Error in eval: Type error in argument 2 of $name, Expected
  perl-string. 

if we look why:

  DB<12> p $XML::Xerces::XMLUni::fgDOMWRTFormatPrettyPrint

     _p_XMLCh=SCALAR(0x1069f9bc)

Aha! The unicode constants are being exported as UTF-16 characters and
*not* as Perl strings like they should. That _p_FOO garbage is simply
SWIG's default wrapping of types it does not understand (SWIG is the
tool I use to create the Perl layer on top of the C++ layer).

So you won't be able to use the fgFOO constants until I fix that (the
same is true for the ISO-8859-1 problem you are having. In the
meantime, you simply use Perl strings. 

Anytime the Xerces-C API tells you to use a UTF-16 string (an XMLCh*,
you can simply use a perl string - the XML::Xerces code will
automatically convert it for you).

Looking at samples/DOMPrint.pl:

  my $impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
  my $writer = $impl->createDOMWriter();
  if ($writer->canSetFeature('format-pretty-print',1)) {
    $writer->setFeature('format-pretty-print',1);
  }
  my $target = XML::Xerces::StdOutFormatTarget->new();
  $writer->writeNode($target,$doc);
  
we can use the string 'format-pretty-print' instead.                                   

> 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.

Oops! Looks like I missed this declaration. I've added it now, so it
will be in the next release (which I can make in a day or two after
I've fixed the string constant problems). In the meantime, using the
MemBuf target and simply calling Perl's print function works just as
well. 

> But I can write my string to a file with Perl features.
> By my string is all on one line:
> 
> How can I format it with Xerces Perl in order to have an indented XML
> (has if is was written through emacs for example) ?

setting pretty-print does that fairly well.

> Is there better way to do what I want than the code I did ?

check samples/DOMPrint.pl or t/DOMWriter.t neither are very advanced,
but they are correct and functional. I'm happy to take better examples
from anyone who has them.

>
> $serializer->$serializer->setEncoding("$XML::Xerces::XMLUni::fgISO88591EncodingString");

Yes, this constant is broken as well. It will be fixed in the next
release. For now, just using the perl string 'ISO-8859-1' will work. 

> my $dt = eval{$impl->createDocumentType('PhysicalEntity', 'no',
> 'PhysicalEntity.dtd')};
> 
> ($@) and die ($@);
> 
> my $doc = eval{$impl->createDocument('', 'PhysicalEntity',$dt)};
> ($@) and die ($@);
> 
> $doc->setEncoding("\*XML::Xerces::XMLUni::fgISO88591EncodingString");

You can do either $doc->setEncoding or $serializer->setEncoding
whichever you prefer.

Thanks again for the feedback,
jas.

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