Re: Create and writean XML file

Christian Orsatti <[email protected]>
Newsgroups gmane.text.xml.xerces-p.devel
Message-ID <[email protected]>
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 <[email protected]> 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: [email protected]
>For additional commands, e-mail: [email protected]
>
>
>
>  
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.