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 Igor Vrdoljak <wi...@fly.srk.fer.hr> on 2001/11/27 19:07:32 UTC

Writing XML to a file

Hi.

I,m using xerces-p to parse and manage Xml files, but I can't write output
to a file.
I tried to change STDOUT to a file pointer (in example scripts), but it does
not work.

If someone could help me with this (I suppose rather trivial) problem, I
would be grateful.

Igor


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


RE: Writing XML to a file

Posted by Igor Vrdoljak <wi...@fly.srk.fer.hr>.
This works, thanks for your swift answer... :))))

-----Original Message-----
From: Jason E. Stewart [mailto:jason@openinformatics.com]
Sent: 27. studeni 2001 20:05
To: xerces-p-dev@xml.apache.org
Subject: Re: Writing XML to a file


"Derek Wueppelmann" <dw...@newswire.ca> writes:

> On Tue, 27 Nov 2001, Igor Vrdoljak wrote:
> 
> > sub PrintSTOUT  {
> >     my ($doc) = @_;
> >     XML::Xerces::DOMParse::unformat ($doc);
> >     XML::Xerces::DOMParse::format ($doc);
> >     XML::Xerces::DOMParse::print (\*STDOUT, $doc);
> > }
> > 
> > sub PrintFile   {
> >     my ($doc) = @_;
> >     my $string = $doc->serialize();
> >     open FP, $XML_TEST_FILE;
> >     print FP $string;
> > 
> > }
> > 
> > Now, PrintSTOUT works, but PrintFile does not.
> > I would be grateful if you helped...
> 
> Why don't you do someting like this inside of PrintFile instead...
> 
> sub PrintFile {
>    my ($doc) = @_;
>    my (*FP);
> 
>    open FP, $XML_TEST_FILE;
>    XML::Xerces::DOMParse::print(\*FP, $doc);
> }

Yes, that should work. 

Also you are not checking the return value of open(), this is a
Very-Bad-Idea (TM). You might be getting an error and just tossing it
off trying to use a non-existing file handle.

And it just so happens that you are opening the filehandle READONLY
which means that print isn't likely to do very much. Try:

   open(FP, ">$XML_TEST_FILE")
        or die "Couldn't open $XML_TEST_FILE for writing";

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: Writing XML to a file

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Derek Wueppelmann" <dw...@newswire.ca> writes:

> On Tue, 27 Nov 2001, Igor Vrdoljak wrote:
> 
> > sub PrintSTOUT  {
> >     my ($doc) = @_;
> >     XML::Xerces::DOMParse::unformat ($doc);
> >     XML::Xerces::DOMParse::format ($doc);
> >     XML::Xerces::DOMParse::print (\*STDOUT, $doc);
> > }
> > 
> > sub PrintFile   {
> >     my ($doc) = @_;
> >     my $string = $doc->serialize();
> >     open FP, $XML_TEST_FILE;
> >     print FP $string;
> > 
> > }
> > 
> > Now, PrintSTOUT works, but PrintFile does not.
> > I would be grateful if you helped...
> 
> Why don't you do someting like this inside of PrintFile instead...
> 
> sub PrintFile {
>    my ($doc) = @_;
>    my (*FP);
> 
>    open FP, $XML_TEST_FILE;
>    XML::Xerces::DOMParse::print(\*FP, $doc);
> }

Yes, that should work. 

Also you are not checking the return value of open(), this is a
Very-Bad-Idea (TM). You might be getting an error and just tossing it
off trying to use a non-existing file handle.

And it just so happens that you are opening the filehandle READONLY
which means that print isn't likely to do very much. Try:

   open(FP, ">$XML_TEST_FILE")
        or die "Couldn't open $XML_TEST_FILE for writing";

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: Writing XML to a file

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Igor Vrdoljak" <wi...@fly.srk.fer.hr> writes:

> It does not work. 
> Here's error I get:
> 
> Can't declare ref-to-glob cast in "my" at sms_lib.pl line 256, near ");"

Woops. Overlooked that little detail. Globs like *FP are global
symbols, you can't declare them lexical using 'my'. Use this instead:

  local *FP;

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: Writing XML to a file

Posted by Igor Vrdoljak <wi...@fly.srk.fer.hr>.
It does not work. 
Here's error I get:

Can't declare ref-to-glob cast in "my" at sms_lib.pl line 256, near ");"


-----Original Message-----
From: Derek Wueppelmann [mailto:dwueppel@newswire.ca]
Sent: 27. studeni 2001 19:52
To: xerces-p-dev@xml.apache.org
Subject: RE: Writing XML to a file


On Tue, 27 Nov 2001, Igor Vrdoljak wrote:

> sub PrintSTOUT  {
>     my ($doc) = @_;
>     XML::Xerces::DOMParse::unformat ($doc);
>     XML::Xerces::DOMParse::format ($doc);
>     XML::Xerces::DOMParse::print (\*STDOUT, $doc);
> }
> 
> sub PrintFile   {
>     my ($doc) = @_;
>     my $string = $doc->serialize();
>     open FP, $XML_TEST_FILE;
>     print FP $string;
> 
> }
> 
> Now, PrintSTOUT works, but PrintFile does not.
> I would be grateful if you helped...

Why don't you do someting like this inside of PrintFile instead...

sub PrintFile {
   my ($doc) = @_;
   my (*FP);

   open FP, $XML_TEST_FILE;
   XML::Xerces::DOMParse::print(\*FP, $doc);
}

Would that not have the same effect? At least the desired effect you are
looking for?

-- 
 0/ Derek Wueppelmann      derek.wueppelmann@newswire.ca
/D   Canada NewsWire Ltd.   http://www.newswire.ca 
/ )   Work: (416) 863-2107


---------------------------------------------------------------------
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: Writing XML to a file

Posted by Derek Wueppelmann <dw...@newswire.ca>.
On Tue, 27 Nov 2001, Igor Vrdoljak wrote:

> sub PrintSTOUT  {
>     my ($doc) = @_;
>     XML::Xerces::DOMParse::unformat ($doc);
>     XML::Xerces::DOMParse::format ($doc);
>     XML::Xerces::DOMParse::print (\*STDOUT, $doc);
> }
> 
> sub PrintFile   {
>     my ($doc) = @_;
>     my $string = $doc->serialize();
>     open FP, $XML_TEST_FILE;
>     print FP $string;
> 
> }
> 
> Now, PrintSTOUT works, but PrintFile does not.
> I would be grateful if you helped...

Why don't you do someting like this inside of PrintFile instead...

sub PrintFile {
   my ($doc) = @_;
   my (*FP);

   open FP, $XML_TEST_FILE;
   XML::Xerces::DOMParse::print(\*FP, $doc);
}

Would that not have the same effect? At least the desired effect you are
looking for?

-- 
 0/ Derek Wueppelmann      derek.wueppelmann@newswire.ca
/D   Canada NewsWire Ltd.   http://www.newswire.ca 
/ )   Work: (416) 863-2107


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


RE: Writing XML to a file

Posted by Igor Vrdoljak <wi...@fly.srk.fer.hr>.
Hi,This is the code I used:

#main script
#!/usr/bin/perl
########################################

require "sms_lib.pl";

$doc=&MakeParser();
&MakeUser("1234",$doc);
&GetPasswd("1234",$doc);
&SetPasswd("1234","bla",$doc);

&PrintSTOUT($doc);
&PrintFile($doc);

#sms_lib.pl
#!/usr/bin/perl
#####################################################################
use XML::Xerces;
use XML::Xerces::DOMParse;

$XML_FILE="sms.xml";
$XML_NODE_ROOT="sms";
$XML_NODE_STUDENT="student";
$XML_NODE_MBR="mbr";
$XML_NODE_PASSWD="passwd";
$XML_NODE_NAME="ime";
$XML_NODE_SURNAME="prezime";
$XML_NODE_DEPARTMENT="smjer";
$XML_NODE_YEAR="godina";
$XML_NODE_BONUS="izborni";
$XML_NODE_TELEPHONE="tel";
$XML_NODE_SERVICE="sms_service";
$XML_TEST_FILE="out.xml";
$UNDEF="UNDEF";

sub GetPasswd   {
    my($mbr,$doc) = @_;
    my $root= $doc->getDocumentElement();
    my $studenti= $doc->getElementsByTagName($XML_NODE_STUDENT);
    my $n= $studenti->getLength();
    for (my $i=0; $i<$n; $i++){
        my $student = $studenti->item($i);
        my $passwd_l= $student->getElementsByTagName($XML_NODE_PASSWD);
        my $mbr_loc_l= $student->getElementsByTagName($XML_NODE_MBR);
        my $passwd= $passwd_l->item(0);
        my $mbr_loc= $mbr_loc_l->item(0);
        my $passwd_text= ($passwd->getFirstChild())->getNodeValue();
        my $mbr_loc_text= ($mbr_loc->getFirstChild())->getNodeValue();
        if ($mbr_loc_text == $mbr){
            return $passwd_text
        }
    }
    return 0;
}

sub SetPasswd {
    my($mbr,$passwd,$doc) = @_;
    my $root= $doc->getDocumentElement();
    my $studenti= $doc->getElementsByTagName($XML_NODE_STUDENT);
    my $n= $studenti->getLength();
    for (my $i=0; $i<$n; $i++){
        my $student = $studenti->item($i);
        my $mbr_loc_l= $student->getElementsByTagName($XML_NODE_MBR);
        my $mbr_loc= $mbr_loc_l->item(0);
        my $mbr_loc_text= ($mbr_loc->getFirstChild())->getNodeValue();
        my $passwd_loc=
($student->getElementsByTagName($XML_NODE_PASSWD))->item(0);
        if ($mbr_loc_text == $mbr){
            ($passwd_loc->getFirstChild())->setNodeValue($passwd);
            return ($passwd_loc->getFirstChild())->getNodeValue();
        }
    }
    return 0;
}

sub MakeUser    {
    my($mbr, $doc) = @_;
    my $root =$doc->getDocumentElement();
    my $user = $doc->createElement($XML_NODE_STUDENT);
    &_MakeMBR($doc,$user,$mbr);
    $root->appendChild($user);
    #test
    &PrintSTOUT($doc);
    }

sub MakeParser  {
    my $parser = XML::Xerces::DOMParser->new();
    $parser->setToCreateXMLDeclTypeNode(1);
    $parser->parse(XML::Xerces::LocalFileInputSource->new($XML_FILE));
    my $doc = $parser->getDocument();
    return $doc
}

sub PrintSTOUT  {
    my ($doc) = @_;
    XML::Xerces::DOMParse::unformat ($doc);
    XML::Xerces::DOMParse::format ($doc);
    XML::Xerces::DOMParse::print (\*STDOUT, $doc);
}

sub PrintFile   {
    my ($doc) = @_;
    my $string = $doc->serialize();
    open FP, $XML_TEST_FILE;
    print FP $string;

}

Now, PrintSTOUT works, but PrintFile does not.
I would be grateful if you helped...

---------------------------------------------------------------------
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: Writing XML to a file

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Igor Vrdoljak" <wi...@fly.srk.fer.hr> writes:

> I,m using xerces-p to parse and manage Xml files, but I can't write
> output to a file.  I tried to change STDOUT to a file pointer (in
> example scripts), but it does not work.
> 
> If someone could help me with this (I suppose rather trivial)
> problem, I would be grateful.

Hi Igor,

You'll have to be a bit more explicit. If you're having trouble,
you're probably using DOM with the DOMParse class? I don't know
how well DOMParse handles filehandles. The code is old and getting
rather crufty. It hasn't been update much since Tom Watson wrote it
for the 1.0 port.

You can always use:

  my $string = $dom_document->serialize();
  print HANDLE $string;

If this isn't what you're looking for, please be more specific, a
short example of what you're trying would help.

jas.

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